QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#194062 | #7513. Palindromic Beads | ucup-team1447# | WA | 3ms | 16700kb | C++14 | 4.4kb | 2023-09-30 18:42:56 | 2023-09-30 18:42:58 |
Judging History
answer
// This Code was made by Chinese_zjc_.
#include <bits/stdc++.h>
#define lson (now << 1)
#define rson (lson | 1)
#define lmid ((l + r) >> 1)
#define rmid (lmid + 1)
int n, c[200005], d[200005], fa[200005], dfn[200005], idfn[200005], end[200005], tim, max[1 << 19], siz[200005], heavy[200005],
top[200005], depth[200005], dp[200005], ans = 1;
std::vector<int> to[200005];
void pushup(int now) { max[now] = std::max(max[lson], max[rson]); }
void add(int pos, int val, int now = 1, int l = 1, int r = n)
{
if (l == r)
{
max[now] = std::max(max[now], val);
return;
}
if (pos <= lmid)
add(pos, val, lson, l, lmid);
else
add(pos, val, rson, rmid, r);
pushup(now);
}
void del(int pos, int now = 1, int l = 1, int r = n)
{
if (l == r)
{
max[now] = 0;
return;
}
if (pos <= lmid)
del(pos, lson, l, lmid);
else
del(pos, rson, rmid, r);
pushup(now);
}
int query(int L, int R, int now = 1, int l = 1, int r = n)
{
if (R < l || r < L)
return 0;
if (L <= l && r <= R)
return max[now];
return std::max(query(L, R, lson, l, lmid), query(L, R, rson, rmid, r));
}
void init1(int now)
{
idfn[dfn[now] = ++tim] = now;
siz[now] = 1;
for (auto i : to[now])
{
if (i == fa[now])
continue;
fa[i] = now;
depth[i] = depth[now] + 1;
init1(i);
siz[now] += siz[i];
if (siz[i] > siz[heavy[now]])
heavy[now] = i;
}
end[now] = tim;
}
void init2(int now)
{
if (!top[now])
top[now] = now;
if (int t = heavy[now])
{
top[t] = top[now];
init2(t);
}
for (auto i : to[now])
{
if (i == fa[now] || i == heavy[now])
continue;
init2(i);
}
}
int LCA(int A, int B)
{
while (top[A] != top[B])
{
if (depth[top[A]] > depth[top[B]])
A = fa[top[A]];
else
B = fa[top[B]];
}
return depth[A] < depth[B] ? A : B;
}
bool in(int A, int B) { return dfn[A] <= dfn[B] && end[B] <= end[A]; }
void add1(int now)
{
int v = now ^ d[c[now]];
if (v && dfn[v] < dfn[now] && !in(v, now))
add(dfn[v], dp[c[now]]);
for (auto i : to[now])
if (i != fa[now])
add1(i);
}
void del1(int now)
{
int v = now ^ d[c[now]];
if (v && dfn[v] < dfn[now] && !in(v, now))
del(dfn[v]);
for (auto i : to[now])
if (i != fa[now])
del1(i);
}
void dfs1(int now)
{
for (auto i : to[now])
{
if (i == fa[now] || i == heavy[now])
continue;
dfs1(i);
del1(i);
}
if (heavy[now])
dfs1(heavy[now]);
for (auto i : to[now])
{
if (i == fa[now] || i == heavy[now])
continue;
add1(i);
}
int v = now ^ d[c[now]];
if (v && dfn[v] < dfn[now] && !in(v, now))
add(dfn[v], dp[c[v]] = query(dfn[v], end[v]) + 1);
}
void add2(int now)
{
int v = now ^ d[c[now]];
if (v)
add(depth[LCA(now, v)], dp[c[now]]);
for (auto i : to[now])
if (i != fa[now])
add2(i);
}
void del2(int now)
{
int v = now ^ d[c[now]];
if (v)
del(depth[LCA(now, v)]);
for (auto i : to[now])
if (i != fa[now])
del2(i);
}
void dfs2(int now)
{
for (auto i : to[now])
{
if (i == fa[now] || i == heavy[now])
continue;
dfs2(i);
del2(i);
}
if (heavy[now])
dfs2(heavy[now]);
for (auto i : to[now])
{
if (i == fa[now] || i == heavy[now])
continue;
add2(i);
}
int v = now ^ d[c[now]];
if (v && dfn[v] < dfn[now] && in(v, now))
add(depth[v], dp[c[v]] = query(1, depth[v]) + 1);
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin >> n;
for (int i = 1; i <= n; ++i)
std::cin >> c[i], d[c[i]] ^= i;
for (int i = 1, u, v; i != n; ++i)
{
std::cin >> u >> v;
to[u].push_back(v);
to[v].push_back(u);
}
depth[1] = 1;
init1(1);
init2(1);
dfs1(1);
dfs2(1);
for (int i = 1; i <= n; ++i)
{
int t = i ^ d[c[i]];
if (dfn[i] < dfn[t])
ans = std::max(ans, dp[c[i]] << 1 | (fa[t] != i));
}
std::cout << ans << std::endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15064kb
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: 0ms
memory: 16696kb
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: 0ms
memory: 16700kb
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: 2ms
memory: 13996kb
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: -100
Wrong Answer
time: 3ms
memory: 14996kb
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:
7
result:
wrong answer 1st lines differ - expected: '5', found: '7'