QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#239842 | #793. Palindromic Partitions | SSH_automaton | 0 | 4ms | 19920kb | C++14 | 3.9kb | 2023-11-04 23:50:00 | 2023-11-04 23:50:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int SIZE = 1800005;
const int SIZEY = 32400005;
int n, c[N];
vector<int> pos[N], tr[N];
int dep[N], fa[N], st[N], ed[N], cnt;
void dfs(int u, int pre) {
st[u] = ++cnt;
dep[u] = dep[pre] + 1;
fa[u] = pre;
for (int v : tr[u])
if (v != pre)
dfs(v, u);
ed[u] = cnt;
}
int spt[18][N];
inline int LCA(int x, int y) {
if (x == y) return x;
int l = st[x], r = st[y];
if (l > r) swap(l, r);
++l;
int k = __lg(r - l + 1);
x = spt[k][l], y = spt[k][r - (1 << k) + 1];
return fa[dep[x] < dep[y] ? x : y];
}
inline int son(int x, int y) {
int l = st[x], r = st[y];
++l;
int k = __lg(r - l + 1);
x = spt[k][l], y = spt[k][r - (1 << k) + 1];
return (dep[x] < dep[y] ? x : y);
}
inline int dist(int x, int y) { return dep[x] + dep[y] - 2 * dep[LCA(x, y)]; }
inline bool isanc(int x, int y) { return st[y] >= st[x] && st[y] <= ed[x]; }
int dis[N], id[N], m;
inline bool cmp(int i, int j) { return dis[i] > dis[j]; }
int rt, ls[SIZE], rs[SIZE], tot;
int rty[SIZE], lsy[SIZEY], rsy[SIZEY], val[SIZEY], toty;
void updatey(int &z, int y, int v, int l = 1, int r = n) {
if (!z) z = ++toty;
val[z] = max(val[z], v);
if (l == r) return;
int mid = (l + r) >> 1;
if (y <= mid) updatey(lsy[z], y, v, l, mid);
else updatey(rsy[z], y, v, mid + 1, r);
}
void update(int &z, int x, int y, int v, int l = 1, int r = n) {
if (!z) z = ++tot;
updatey(rty[z], y, v);
if (l == r) return;
int mid = (l + r) >> 1;
if (x <= mid) update(ls[z], x, y, v, l, mid);
else update(rs[z], x, y, v, mid + 1, r);
}
int queryy(int z, int L, int R, int l = 1, int r = n) {
if (!z) return 0;
if (L <= l && r <= R) return val[z];
int mid = (l + r) >> 1, res = 0;
if (L <= mid) res = max(res, queryy(lsy[z], L, R, l, mid));
if (R > mid) res = max(res, queryy(rsy[z], L, R, mid + 1, r));
return res;
}
int query(int z, int U, int D, int L, int R, int l = 1, int r = n) {
if (!z) return 0;
if (U <= l && r <= D) return queryy(rty[z], L, R);
int mid = (l + r) >> 1, res = 0;
if (U <= mid) res = max(res, query(ls[z], U, D, L, R, l, mid));
if (D > mid) res = max(res, query(rs[z], U, D, L, R, mid + 1, r));
return res;
}
inline int ask(int U, int D, int L, int R) {
if (U > D || L > R) return 0;
return query(rt, U, D, L, R);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> c[i], pos[c[i]].push_back(i);
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
tr[u].push_back(v);
tr[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= n; ++i) spt[0][st[i]] = i;
for (int i = 1; (1 << i) <= n; ++i)
for (int j = 1; j + (1 << i) - 1 <= n; ++j) {
int x = spt[i - 1][j], y = spt[i - 1][j + (1 << (i - 1))];
spt[i][j] = (dep[x] < dep[y] ? x : y);
}
for (int i = 1; i <= n; ++i) {
if (pos[i].size() < 2u) continue;
id[++m] = i;
dis[i] = dist(pos[i][0], pos[i][1]);
if (st[pos[i][0]] > st[pos[i][1]]) swap(pos[i][0], pos[i][1]);
}
sort(id + 1, id + m + 1, cmp);
int ans = 0;
for (int o = 1; o <= m; ++o) {
int i = id[o];
if (isanc(pos[i][0], pos[i][1])) {
int s = son(pos[i][0], pos[i][1]);
int res = max(ask(1, st[s] - 1, st[pos[i][1]], ed[pos[i][1]]), ask(st[pos[i][1]], ed[pos[i][1]], ed[s] + 1, n)) + 2;
update(rt, st[pos[i][0]], st[pos[i][1]], res);
ans = max(ans, res);
} else {
int res = ask(st[pos[i][0]], ed[pos[i][0]], st[pos[i][1]], ed[pos[i][1]]) + 2;
update(rt, st[pos[i][0]], st[pos[i][1]], res);
ans = max(ans, res);
}
}
for (int u = 1; u <= n; ++u) {
for (int v : tr[u])
if (v != fa[u])
ans = max(ans, ask(st[u] + 1, st[v] - 1, st[v], ed[v]) + 1);
ans = max(ans, ask(1, st[u] - 1, st[u] + 1, ed[u]) + 1);
ans = max(ans, ask(st[u] + 1, ed[u], ed[u] + 1, n) + 1);
}
cout << ans << '\n';
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 19920kb
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaabaaaaaaaaaaaaaab aaaaaaaaaaaaabxaaaaaaaaaaaaab baaaaaaaaaaaaaabaaaaaaaaaaaaaa aabbaabbaabbaaaaaaaabbaabbaabb ababababababababababababababab abcabcabcabcabcabcabcabcabcabc abcabcabcabcabccbacbacbacbacba qntshryhcojkqnlmqeob...
output:
1
result:
wrong answer 1st lines differ - expected: '30', found: '1'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%