QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#238680 | #7513. Palindromic Beads | zzy0922 | WA | 1658ms | 441280kb | C++14 | 4.8kb | 2023-11-04 17:15:01 | 2023-11-04 17:15:01 |
Judging History
answer
#include <bits/stdc++.h>
inline void chkmax(int &x, const int &v) {
x = std::max(x, v);
}
const int N = 200005;
int n;
int c[N];
std::vector<int> t[N];
int pre[N];
namespace Seg_Seg {
struct node {
int ls, rs, val;
}tree[40000005];
#define ls(p) tree[p].ls
#define rs(p) tree[p].rs
#define val(p) tree[p].val
int rt[N << 2], tot;
void build(int p, int l, int r) {
rt[p] = ++tot;
if (l == r) return;
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
inline void pushup(int p) {
val(p) = 0;
if (ls(p)) chkmax(val(p), val(ls(p)));
if (rs(p)) chkmax(val(p), val(rs(p)));
}
void _ins(int p, int l, int r, int x, int v) {
if (l == x && r == x) {
val(p) = std::max(val(p), v);
return;
}
int mid = (l + r) >> 1;
if (x <= mid) {
if (!ls(p)) ls(p) = ++tot;
_ins(ls(p), l, mid, x, v);
}
else {
if (!rs(p)) rs(p) = ++tot;
_ins(rs(p), mid + 1, r, x, v);
}
pushup(p);
}
int _qry(int p, int l, int r, int ll, int rr) {
if (!p) return 0;
if (ll > r || rr < l) return 0;
if (ll <= l && r <= rr) return val(p);
int mid = (l + r) >> 1;
return std::max(_qry(ls(p), l, mid, ll, rr), _qry(rs(p), mid + 1, r, ll, rr));
}
void ins(int p, int l, int r, int x, int y, int v) {
if (x > r || x < l) return;
// std::cout << rt[p] << '\n';
_ins(rt[p], 1, n, y, v);
if (x == l && x == r) return;
int mid = (l + r) >> 1;
ins(p << 1, l, mid, x, y, v);
ins(p << 1 | 1, mid + 1, r, x, y, v);
}
int qry(int p, int l, int r, int l1, int r1, int l2, int r2) {
if (l1 > r || r1 < l) return 0;
if (l1 <= l && r <= r1) return _qry(rt[p], 1, n, l2, r2);
int mid = (l + r) >> 1;
return std::max(qry(p << 1, l, mid, l1, r1, l2, r2), qry(p << 1 | 1, mid + 1, r, l1, r1, l2, r2));
}
}
using Seg_Seg::ins;
using Seg_Seg::qry;
int dep[N], fa[N], top[N], siz[N], son[N], dfn[N], out[N], tot;
void dfs1(int u) {
siz[u] = 1;
for (const int &v : t[u]) {
if (v == fa[u]) continue;
fa[v] = u;
dep[v] = dep[u] + 1;
dfs1(v);
siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
out[u] = tot;
}
void dfs2(int u, int tp) {
dfn[u] = ++tot;
top[u] = tp;
if (son[u]) dfs2(son[u], tp);
for (const int &v : t[u]) {
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
out[u] = tot;
}
inline int lca(int x, int y) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] > dep[fy]) x = fa[fx], fx = top[x];
else y = fa[fy], fy = top[y];
}
return dep[x] < dep[y] ? x : y;
}
inline int dis(const int &u, const int &v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
std::vector<std::pair<int, int> > pr;
int main() {
std::cin >> n;
Seg_Seg::build(1, 1, n);
// ins(1, 1, n, 1, 5, 2);
// std::cout << "qry: " << qry(1, 1, n, 1, 2, 1, 5) << '\n';
for (int i = 1; i <= n; i++) std::cin >> c[i];
for (int i = 1, u, v; i <= n - 1; i++) {
std::cin >> u >> v;
t[u].push_back(v);
t[v].push_back(u);
}
dfs1(1);
dfs2(1, 1);
// for (int i = 1; i <= n; i++) std::cout << top[i] << '\n';
// return 0;
for (int i = 1; i <= n; i++) {
if (pre[c[i]]) {
int u = pre[c[i]], v = i;
pr.emplace_back(u, v);
// std::cout << u << ' ' << v << ' ' << dis(u, v) << '\n';
}
pre[c[i]] = i;
}
std::sort(pr.begin(), pr.end(), [&](std::pair<int, int> a, std::pair<int, int> b) {
return dis(a.first, a.second) > dis(b.first, b.second);
});
int max = 1;
for (const auto &p : pr) {
int u = p.first, v = p.second;
if (dfn[u] > dfn[v])
std::swap(u, v);
int res = 2;
if (dfn[u] < dfn[v] && out[u] >= out[v]) {
res = std::max(qry(1, 1, n, 1, dfn[u] - 1, dfn[v] + 1, out[v]), qry(1, 1, n, out[u] + 1, n, dfn[v] + 1, out[v])) + 2;
// std::cout << "qry : " << 1 << ' ' << dfn[u] - 1 << ' ' << dfn[v] + 1 << ' ' << out[v] << '\n';
// std::cout << "qry : " << out[u] + 1 << ' ' << n << ' ' << dfn[v] + 1 << ' ' << out[v] << '\n';
} else {
res = qry(1, 1, n, dfn[u] + 1, out[u], dfn[v] + 1, out[v]) + 2;
// std::cout << "qry : " << dfn[u] + 1 << ' ' << out[u] << ' ' << dfn[v] + 1 << ' ' << n << '\n';
}
ins(1, 1, n, dfn[u], dfn[v], res);
ins(1, 1, n, dfn[v], dfn[u], res);
// std::cout << "ins : " << dfn[u] << ' ' << dfn[v] << '\n';
chkmax(max, res + (dis(u, v) != 1));
}
std::cout << max << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 11960kb
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: 2ms
memory: 11832kb
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: 2ms
memory: 11904kb
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: 10592kb
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: 5ms
memory: 12928kb
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: 0
Accepted
time: 1509ms
memory: 429712kb
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:
5
result:
ok single line: '5'
Test #7:
score: 0
Accepted
time: 1369ms
memory: 429672kb
input:
200000 13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...
output:
7
result:
ok single line: '7'
Test #8:
score: 0
Accepted
time: 1404ms
memory: 430056kb
input:
200000 38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...
output:
45
result:
ok single line: '45'
Test #9:
score: 0
Accepted
time: 1444ms
memory: 431548kb
input:
200000 36449 57574 3145 38591 832 17710 66613 78947 27635 83275 89878 48329 94614 584 96832 9321 72046 44873 5396 61452 63224 63740 26579 13706 108490 19092 89439 85884 12016 5105 48638 74004 41569 35006 22276 45609 25350 49906 35479 15875 68938 77699 48828 21628 11242 77040 70838 45771 27704 64865 ...
output:
135
result:
ok single line: '135'
Test #10:
score: 0
Accepted
time: 1658ms
memory: 435992kb
input:
200000 13528 65006 30352 8565 36687 6748 5507 44320 7189 17847 46996 82728 102722 4727 36914 74228 21460 87970 11733 47170 67282 104558 66436 64504 57055 88619 42995 25569 101298 90984 76491 51994 62257 103424 8221 69668 99170 6808 29043 73058 5277 26614 23654 25152 64939 38418 78518 5330 37531 4305...
output:
425
result:
ok single line: '425'
Test #11:
score: 0
Accepted
time: 1565ms
memory: 441280kb
input:
200000 92279 5566 62695 50240 45387 51097 57743 94873 53220 29260 72584 38043 86335 33441 12946 30267 12932 18258 4560 8896 64393 39608 53183 34285 36518 18501 51940 8658 101018 48522 21336 104735 25785 73132 33489 81905 94563 18128 87872 24765 54563 57218 61869 50458 75919 63764 48155 4489 35212 44...
output:
839
result:
ok single line: '839'
Test #12:
score: 0
Accepted
time: 1302ms
memory: 392572kb
input:
200000 96438 84772 88103 81186 60908 116093 94738 28602 35022 91108 60687 75572 100094 50553 58445 117024 96154 53539 23185 112280 90369 32413 95244 11077 91008 109781 6404 2285 3544 111712 49414 10399 113626 81435 11321 52557 17023 113260 14225 66464 61352 98403 36521 110038 57172 42868 68512 69031...
output:
31
result:
ok single line: '31'
Test #13:
score: -100
Wrong Answer
time: 1212ms
memory: 384796kb
input:
200000 50750 86282 92049 114579 8296 28675 45880 47381 71400 43379 111535 32316 37104 35968 100241 6914 81284 48969 62890 63486 107557 80178 76322 31515 24682 85646 12681 106054 5167 50339 39004 16152 112081 10605 66750 51623 96332 77287 75452 50609 1549 1652 45229 73171 10015 66323 90164 97491 1007...
output:
8106
result:
wrong answer 1st lines differ - expected: '8108', found: '8106'