QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#283272 | #7513. Palindromic Beads | dayux | WA | 5ms | 20152kb | C++14 | 4.2kb | 2023-12-14 11:30:48 | 2023-12-14 11:30:48 |
Judging History
answer
#include <bits/stdc++.h>
const int maxn = 2e5, maxlg = 17;
int n, dfc;
std::array<int, maxn + 1> p, fa, dep, dfn, siz;
std::array<std::vector<int>, maxn + 1> g;
std::array<std::array<int, maxn + 1>, maxlg + 1> min;
void dfs(int x) {
min[0][dfn[x] = ++dfc] = x;
siz[x] = 1;
for (int v : g[x])
if (v != fa[x]) {
dep[v] = dep[fa[v] = x] + 1;
dfs(v);
siz[x] += siz[v];
}
}
void rmq() {
for (int lg = 0; lg < maxlg; ++lg)
for (int i = 1; i + (2 << lg) - 1 <= n; ++i)
min[lg + 1][i] = dep[min[lg][i]] <= dep[min[lg][i + (1 << lg)]] ? min[lg][i] : min[lg][i + (1 << lg)];
}
int query(int l, int r) {
int k = 31 ^ __builtin_clz(r - l + 1);
return dep[min[k][l]] <= dep[min[k][r - (1 << k) + 1]] ? min[k][l] : min[k][r - (1 << k) + 1];
}
int getlca(int x, int y) {
if (x == y)
return x;
else {
if (dfn[x] > dfn[y])
std::swap(x, y);
return fa[query(dfn[x] + 1, dfn[y])];
}
}
int getdis(int x, int y) {
return dep[x] + dep[y] - dep[getlca(x, y)] * 2;
}
namespace segt {
int tot;
std::array<int, maxn * 4> rt;
struct node {
int max, ls, rs;
node() {
max = ls = rs = 0;
}
};
std::vector<node> t(1);
int ls(int x) {
return x * 2;
}
int rs(int x) {
return x * 2 + 1;
}
void ins(int &x, int yl, int yr, int v, int l = 1, int r = n) {
if (x == 0) {
x = (int)t.size();
t.emplace_back();
}
if (yl <= l && r <= yr)
t[x].max = std::max(t[x].max, v);
else {
int mid = (l + r) / 2;
if (yl <= mid) {
int ls = t[x].ls;
ins(ls, yl, yr, v, l, mid);
t[x].ls = ls;
}
if (yr > mid) {
int rs = t[x].rs;
ins(rs, yl, yr, v, mid + 1, r);
t[x].rs = rs;
}
}
}
void mdf(int xl, int xr, int yl, int yr, int v, int x = 1, int l = 1, int r = n) {
if (xl <= l && r <= xr)
ins(rt[x], yl, yr, v);
else {
int mid = (l + r) / 2;
if (xl <= mid)
mdf(xl, xr, yl, yr, v, ls(x), l, mid);
if (xr > mid)
mdf(xl, xr, yl, yr, v, rs(x), mid + 1, r);
}
}
int get(int x, int p, int l = 1, int r = n) {
if (x == 0)
return 0;
else if (l == r)
return t[x].max;
else {
int mid = (l + r) / 2;
return std::max(p <= mid ? get(t[x].ls, p, l, mid) : get(t[x].rs, p, mid + 1, r), t[x].max);
}
}
int qry(int p, int y, int x = 1, int l = 1, int r = n) {
if (l == r)
return get(rt[x], y);
else {
int mid = (l + r) / 2;
return std::max(p <= mid ? qry(p, y, ls(x), l, mid) : qry(p, y, rs(x), mid + 1, r), get(rt[x], y));
}
}
}
int main() {
#ifndef ONLINE_JUDGE
std::freopen("B.in", "r", stdin);
std::freopen("B.out", "w", stdout);
#endif
std::cin.tie(nullptr)->sync_with_stdio(false);
std::cin >> n;
std::vector<std::pair<int, int>> vpr;
for (int i = 1, c; i <= n; ++i) {
std::cin >> c;
if (p[c] == 0)
p[c] = i;
else
vpr.emplace_back(p[c], i);
}
for (int i = 1, u, v; i < n; ++i) {
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
rmq();
std::sort(vpr.begin(), vpr.end(), [&](const std::pair<int, int> &lhs, const std::pair<int, int> &rhs) { return getdis(lhs.first, lhs.second) < getdis(rhs.first, rhs.second); });
int max = 1, f;
for (auto &pr : vpr) {
if (dfn[pr.first] > dfn[pr.second])
std::swap(pr.first, pr.second);
max = std::max(max, (f = std::max((int)(getdis(pr.first, pr.second) > 1), std::max(segt::qry(dfn[pr.first], dfn[pr.second]), segt::qry(dfn[pr.second], dfn[pr.first]))) + 2));
if (dfn[pr.second] <= dfn[pr.first] + siz[pr.first] - 1) {
if (1 <= dfn[pr.first] - 1)
segt::mdf(1, dfn[pr.first] - 1, dfn[pr.second], dfn[pr.second] + siz[pr.second] - 1, f);
if (dfn[pr.first] + siz[pr.first] <= n)
segt::mdf(dfn[pr.first] + siz[pr.first], n, dfn[pr.second], dfn[pr.second] + siz[pr.first] - 1, f);
}
else
segt::mdf(dfn[pr.first], dfn[pr.first] + siz[pr.first] - 1, dfn[pr.second], dfn[pr.second] + siz[pr.second] - 1, f);
}
std::cout << max << std::endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 15868kb
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: 3ms
memory: 13984kb
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: 3ms
memory: 14032kb
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: 0ms
memory: 15836kb
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: 5ms
memory: 20152kb
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'