QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#198592 | #7513. Palindromic Beads | mendicillin2 | RE | 2ms | 4760kb | C++17 | 5.8kb | 2023-10-03 15:10:41 | 2023-10-03 15:10:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define per(i, a, b) for (int i = int(b)-1; i >= int(a); i--)
#define trav(a, x) for (auto& a : x)
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using ll = int64_t;
using vi = vc<int>;
using pii = pair<int, int>;
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
template <class F>
struct ycr {
F f;
template <class T> explicit ycr(T&& ff) : f(forward<T>(ff)) {}
template <class... A> decltype(auto) operator()(A&&... args) {
return f(ref(*this), forward<A>(args)...);
}
};
template <class F> decltype(auto) yc(F&& f) {
return ycr<decay_t<F>>(forward<F>(f));
}
struct GC {
char buf[1<<16];
size_t s=0,e=0;
char operator()(){
if(s>=e){
buf[0]=0;
s=0;
e=fread(buf,1,sizeof(buf),stdin);
}
return buf[s++];
}
} gc;
int read() {
char c;
while((c=gc())<40);
if(c==-'-')return -read();
int a=c-'0';
while(isdigit(c=gc()))a=a*10+c-'0';
return a;
}
struct hldecomp {
int n;
vi ord;
vc<array<int,2>> idx;
vc<pair<int,int>> heavy;
hldecomp():n(0){}
hldecomp(const vi&par):n(sz(par)),ord(n),idx(n),heavy(n){
vvc<int> ch(n);
for (int i = 0; i < n; i++) {
if (par[i] != -1) {
ch[par[i]].push_back(i);
}
}
vi s(n);
int nidx = 0;
for (int i = 0; i < n; i++) {
if (par[i] != -1) continue;
yc([&](auto self, int v) -> void {
s[v] = 1;
for (int w : ch[v]) {
self(w);
s[v] += s[w];
}
if (!ch[v].empty()) {
auto mit = max_element(ch[v].begin(), ch[v].end(), [&](int a, int b) {
return s[a] < s[b];
});
swap(*ch[v].begin(), *mit);
}
})(i);
yc([&](auto self, int v, bool rt = true) -> void {
ord[idx[v][0] = nidx++] = v;
if (rt) {
heavy[idx[v][0]] = {par[v] == -1 ? -1 : idx[par[v]][0], 1};
} else {
assert(idx[par[v]][0] == idx[v][0]-1);
heavy[idx[v][0]] = heavy[idx[v][0]-1];
heavy[idx[v][0]].second++;
}
bool crt = false;
for (int w : ch[v]) {
self(w, crt);
crt = true;
}
idx[v][1] = nidx;
})(i);
}
assert(n == nidx);
}
int lca(int a, int b) {
a = idx[a][0], b = idx[b][0];
while (true) {
if (a > b) swap(a, b);
assert(a <= b);
if (a > b - heavy[b].second) {
return ord[a];
}
b = heavy[b].first;
if (b == -1) return -1;
}
}
bool in_subtree(int a, int p) const {
return idx[p][0] <= idx[a][0] &&
idx[a][1] && idx[p][1];
}
};
const int MAXV = 2.5e7;
struct seg {
array<int, 2> c;
int v;
} pool[MAXV];
template <class T> void setmax(T& a, const T& b) {
if (a < b) a = b;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
cout << fixed << setprecision(20);
int N;
cin >> N;
vector<vector<int>> locs(N);
for (int i = 0; i < N; i++) {
int c;
cin >> c;
c--;
locs[c].push_back(i);
}
vector<vector<int>> adj(N);
for (int e = 0; e < N-1; e++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> par(N, -1);
vector<int> depth(N, 0);
yc([&](auto self, int cur, int prv, int d) -> void {
par[cur] = prv;
depth[cur] = d;
for (int nxt : adj[cur]) {
if (nxt == prv) continue;
self(nxt, cur, d+1);
}
})(0, -1, 0);
auto hld = hldecomp(par);
vector<tuple<int, int, int>> stuff;
stuff.reserve(N/2);
for (int c = 0; c < N; c++) {
if (locs[c].size() <= 1) continue;
assert(locs[c].size() == 2);
int x = locs[c][0];
int y = locs[c][1];
int a = hld.lca(x, y);
int d = depth[x] + depth[y] - 2 * depth[a];
stuff.emplace_back(d, x, y);
}
const int S = 1 << (N <= 1 ? 0 : 32 - __builtin_clz(N-1));
vector<int> seg(2*S);
int V = 1;
auto upd_inner = [&](int& rt, int k, int v) -> void {
yc([&](auto self, int& a, int l, int r) -> void {
if (!a) {
assert(V < MAXV);
a = V++;
pool[a].c = {0,0};
pool[a].v = 0;
}
assert(a);
setmax(pool[a].v, v);
if (r-l == 1) {
return;
}
int m = (l+r)/2;
if (k < m) {
self(pool[a].c[0], l, m);
assert(pool[a].c[0]);
} else {
self(pool[a].c[1], m, r);
assert(pool[a].c[1]);
}
assert(pool[a].v >= v);
})(rt, 0, S);
};
auto upd_outer = [&](int x, int y, int v) -> void {
assert(0 <= x && x < S);
assert(0 <= y && y < S);
for (int a = S+x; a >= 1; a /= 2) {
upd_inner(seg[a], y, v);
assert(seg[a]);
}
};
auto query_inner = [&](int rt, int ql, int qr) -> int {
return yc([&](auto self, int a, int l, int r) -> int {
if (!a) return 0;
if (r <= ql || qr <= l) return 0;
if (ql <= l && r <= qr) {
return pool[a].v;
}
int m = (l+r)/2;
return max(self(pool[a].c[0], l, m), self(pool[a].c[1], m, r));
})(rt, 0, S);
};
auto query_outer = [&](int xl, int xr, int yl, int yr) -> int {
assert(0 <= xl);
assert(xl <= xr);
assert(xr <= yl);
assert(yl <= yr);
assert(yr <= S);
if (xl == xr) return 0;
if (yl == yr) return 0;
int res = 0;
for (int a = S+xl, b = S+xr; a < b; a /= 2, b /= 2) {
if (a & 1) setmax(res, query_inner(seg[a++], yl, yr));
if (b & 1) setmax(res, query_inner(seg[--b], yl, yr));
}
return res;
};
const auto& idx = hld.idx;
sort(stuff.begin(), stuff.end());
reverse(stuff.begin(), stuff.end());
int ans = 1;
for (auto [d, x, y] : stuff) {
assert(x != y);
int v;
if (idx[x][0] > idx[y][0]) swap(x, y);
int a = hld.lca(x, y);
if (a != x) {
v = query_outer(idx[x][0], idx[x][1], idx[y][0], idx[y][1]);
} else {
int c = -1;
for (int z : adj[x]) {
if (hld.in_subtree(y, z)) {
c = z;
break;
}
}
assert(c != -1);
v = query_outer(0, idx[c][0], idx[y][0], idx[y][1]);
setmax(v, query_outer(idx[y][0], idx[y][1], idx[c][1], N));
}
v += 2;
setmax(ans, v + int(d >= 2));
upd_outer(idx[x][0], idx[y][0], v);
}
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3664kb
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: 3664kb
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: 3848kb
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: 3652kb
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: 2ms
memory: 4760kb
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
Runtime Error
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...