QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#197797 | #7513. Palindromic Beads | ucup-team484 | WA | 863ms | 176664kb | C++17 | 3.3kb | 2023-10-02 19:57:50 | 2023-10-02 19:57:50 |
Judging History
answer
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define st first
#define nd second
#define pii pair<int, int>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
const int M = 1e7 + 5;
vector<int> adj[N];
int fi[N], la[N], par[N], curt = 0;
int root[N], n, L[M], R[M], seg[M], nodecnt = 0;
void dfs(int u, int p) {
par[u] = p;
fi[u] = curt++;
for (int v: adj[u]) {
if (v == p) continue;
dfs(v, u);
}
la[u] = curt - 1;
}
int qry2(int l, int r, int& k, int yl, int yr) {
if (r < yl || yr < l || !k) return 0;
if (yl <= l && r <= yr) return seg[k];
int m = (l + r) / 2;
return max(qry2(l, m, L[k], yl, yr), qry2(m + 1, r, R[k], yl, yr));
}
int qry(int l, int r, int k, int xl, int xr, int yl, int yr) {
if (r < xl || xr < l || xl > xr || yl > yr) return 0;
if (xl <= l && r <= xr) return qry2(0, n - 1, root[k], yl, yr);
int m = (l + r) / 2;
return max(qry(l, m, 2 * k, xl, xr, yl, yr), qry(m + 1, r, 2 * k + 1, xl, xr, yl, yr));
}
void upd2(int l, int r, int& k, int y, int v) {
if (r < y || y < l) return;
if (!k) k = ++nodecnt;
seg[k] = max(seg[k], v);
if (y <= l && r <= y) return;
int m = (l + r) / 2;
upd2(l, m, L[k], y, v);
upd2(m + 1, r, R[k], y, v);
}
void upd(int l, int r, int k, int x, int y, int v) {
if (r < x || x < l) return;
upd2(0, n - 1, root[k], y, v);
if (x <= l && r <= x) return;
int m = (l + r) / 2;
upd(l, m, k * 2, x, y, v);
upd(m + 1, r, k * 2 + 1, x, y, v);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
vector<int> c(n), deg(n), dp(n);
vector<vector<int>> pos(n);
for (int i = 0; i < n; i++)
cin >> c[i], c[i]--, pos[c[i]].push_back(i);
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int it = 0; it < 2; it++) {
deque<int> pq;
int ce = 0;
for (int i = 0; i < n; i++) {
deg[i] = sz(adj[i]);
if (deg[i] == 1)
pq.push_back(i);
}
while (!pq.empty()) {
int u = pq.front(); pq.pop_front();
for (int v: adj[u])
if (--deg[v] == 1)
pq.push_back(v);
ce = u;
if (sz(pos[c[u]]) == 1 || dp[c[u]] != 0 || it == 0)
continue;
int x = pos[c[u]][0], y = pos[c[u]][1];
if (fi[x] <= fi[y] && la[y] <= la[x] || fi[y] <= fi[x] && la[x] <= la[y]) {
if (fi[y] <= fi[x] && la[x] <= la[y])
swap(x, y);
for (int v: adj[x])
if (fi[v] <= fi[y] && la[y] <= la[v] && v != par[x]) {
int v1 = max(qry(0, n - 1, 1, 0, fi[v] - 1, fi[y], la[y]), qry(0, n - 1, 1, la[v] + 1, curt - 1, fi[y], la[y]));
int v2 = max(qry(0, n - 1, 1, fi[y], la[y], 0, fi[v] - 1), qry(0, n - 1, 1, fi[y], la[y], la[v] + 1, curt - 1));
dp[c[u]] = max(v1, v2) + 2;
}
} else
dp[c[u]] = max(qry(0, n - 1, 1, fi[x], la[x], fi[y], la[y]), qry(0, n - 1, 1, fi[y], la[y], fi[x], la[x])) + 2;
upd(0, n - 1, 1, fi[x], fi[y], dp[c[u]]);
}
if (it == 0)
dfs(ce, -1);
}
int ans = 1;
for (int i = 0; i < n; i++) {
ans = max(ans, dp[c[i]]);
// cout << i << " " << dp[c[i]] << "\n";
if (sz(pos[c[i]]) == 1)
continue;
int x = pos[c[i]][0], y = pos[c[i]][1], o = 0;
for (int v: adj[x])
o |= v == y;
if (o == 0)
ans = max(ans, dp[c[i]] + 1);
}
cout << ans << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 35348kb
input:
4 1 1 2 2 1 2 2 3 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 4ms
memory: 36940kb
input:
5 1 3 2 2 1 1 2 2 3 3 4 4 5
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 9ms
memory: 35788kb
input:
6 1 1 2 2 3 3 1 2 2 3 3 4 4 5 5 6
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 4ms
memory: 37156kb
input:
6 1 2 3 4 5 6 1 2 2 3 3 4 4 5 5 6
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 7ms
memory: 36068kb
input:
2000 845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...
output:
5
result:
ok single line: '5'
Test #6:
score: -100
Wrong Answer
time: 863ms
memory: 176664kb
input:
200000 48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...
output:
13003528
result:
wrong answer 1st lines differ - expected: '5', found: '13003528'