QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#576615 | #7513. Palindromic Beads | Max_s_xaM | WA | 5ms | 22068kb | C++17 | 7.1kb | 2024-09-19 21:17:49 | 2024-09-19 21:17:49 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <random>
#include <ctime>
#include <chrono>
#include <numeric>
typedef long long ll;
typedef double lf;
// #define DEBUG 1
struct IO
{
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
#endif
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
#define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
template <typename T>
void Read(T &x)
{
#if DEBUG
std::cin >> x;
#else
bool sign = 0; char ch = gc(); x = 0;
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
if (sign) x = -x;
#endif
}
void Read(char *s)
{
#if DEBUG
std::cin >> s;
#else
char ch = gc();
for (; blank(ch); ch = gc());
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
#endif
}
void Read(char &c) {for (c = gc(); blank(c); c = gc());}
void Push(const char &c)
{
#if DEBUG
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <typename T>
void Write(T x)
{
if (x < 0) x = -x, Push('-');
static T sta[35];
int top = 0;
do sta[top++] = x % 10, x /= 10; while (x);
while (top) Push(sta[--top] ^ 48);
}
template <typename T>
void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)
using namespace std;
const int MAXN = 2e5 + 10;
int n, a[MAXN], xs[MAXN], ans = 1;
vector <int> e[MAXN];
bool vis[MAXN];
int rt, tsz, sz[MAXN];
int val[MAXN];
int ff[MAXN], dfn[MAXN], clk;
int anc[MAXN];
void Findrt(int u, int fa)
{
sz[u] = 1; int mx = 0;
for (auto v : e[u])
if (v != fa && !vis[v]) Findrt(v, u), sz[u] += sz[v], mx = max(mx, sz[v]);
mx = max(mx, tsz - sz[u]);
if (mx <= tsz / 2) rt = u;
}
struct SegTree
{
int lc[MAXN * 40], rc[MAXN * 40];
int mx[MAXN * 40];
int rt[MAXN], tot;
bool type;
#define ls lc[p]
#define rs rc[p]
int Update1(int q, int l, int r, int x, int y, int k)
{
int p = ++tot; ls = lc[q], rs = rc[q], mx[p] = mx[q];
if (x <= l && r <= y) return mx[p] = max(mx[p], k), p;
int mid = l + r >> 1;
if (x <= mid) ls = Update1(lc[q], l, mid, x, y, k);
if (y > mid) rs = Update1(rc[q], mid + 1, r, x, y, k);
return p;
}
int Update2(int q, int l, int r, int x, int k)
{
int p = ++tot; ls = lc[q], rs = rc[q], mx[p] = max(mx[q], k);
// cout << p << ' ' << q << ' ' << ls << ' ' << rs << ' ' << l << ' ' << r << ' ' << mx[p] << '\n';
if (l == r) return p;
int mid = l + r >> 1;
if (x <= mid) ls = Update2(lc[q], l, mid, x, k);//, cout << ls << " E\n";
else rs = Update2(rc[q], mid + 1, r, x, k);
// cout << "ret " << p << ' ' << ls << ' ' << rs << "\n";
return p;
}
int Query1(int p, int l, int r, int x, int y)
{
// cout << "Query " << p << ' ' << ls << ' ' << rs << "\n";
if (!p) return 0;
if (x <= l && r <= y) return mx[p];
int mid = l + r >> 1, ans = 0;
if (x <= mid) ans = max(ans, Query1(ls, l, mid, x, y));
if (y > mid) ans = max(ans, Query1(rs, mid + 1, r, x, y));
// cout << p << ' ' << ls << ' ' << rs << ' ' << l << ' ' << r << ' ' << ans << " ##\n";
return ans;
}
int Query2(int p, int l, int r, int x)
{
if (!p) return 0;
int ans = mx[p];
if (l == r) return ans;
int mid = l + r >> 1;
if (x <= mid) ans = max(ans, Query2(ls, l, mid, x));
else ans = max(ans, Query2(rs, mid + 1, r, x));
return ans;
}
void Update(int &p, int q, int l, int r, int x, int y, int k)
{
if (!type) p = Update1(q, l, r, x, y, k);
else p = Update2(q, l, r, x, k);
}
int Query(int p, int l, int r, int x, int y)
{
if (type) return Query1(p, l, r, x, y);
return Query2(p, l, r, x);
}
}T1, T2;
void DFS2(int u, int fa)
{
sz[u] = 1, dfn[u] = ++clk, anc[u] = rt;
for (auto v : e[u])
if (v != fa && !vis[v]) ff[v] = (ff[u] ? ff[u] : v), DFS2(v, u), sz[u] += sz[v];
if (xs[a[u]] == u) val[a[u]] = 1;
else
{
int v = xs[a[u]] ^ u;
if (anc[v] == rt && dfn[v] < dfn[u]) val[a[u]] = 2 + (fa != v);
}
}
void DFS3(int u, int fa)
{
T1.rt[u] = T1.rt[fa], T2.rt[u] = T2.rt[fa];
if (xs[a[u]] != u)
{
int v = xs[a[u]] ^ u;
// cout << u << ' ' << v << ' ' << a[u] << ' ' << val[a[u]] << '\n';
if (dfn[v] < dfn[u] && dfn[u] < dfn[v] + sz[v])
{
// cout << "## " << dfn[v] << ' ' << dfn[v] + sz[v] - 1 << '\n';
val[a[u]] = max(val[a[u]], T2.Query(T2.rt[fa], 1, n, dfn[v], dfn[v] + sz[v] - 1) + 2);
// cout << T2.Query(T2.rt[u], 1, n, 6, 6) << "\n";
T2.Update(T2.rt[u], T2.rt[u], 1, n, dfn[v], dfn[v], val[a[u]]);
// cout << T2.Query(T2.rt[u], 1, n, 6, 6) << "\n";
// for (int i = 1; i <= n; i++) cout << T2.Query(T2.rt[u], 1, n, i, i) << ' '; cout << "\n";
}
else if (ff[u] != ff[v])
{
val[a[u]] = max(val[a[u]], T1.Query(T1.rt[fa], 1, n, dfn[v], dfn[v]) + 2);
val[a[u]] = max(val[a[u]], T2.Query(T2.rt[fa], 1, n, 1, n) + 2);
T1.Update(T1.rt[u], T1.rt[u], 1, n, dfn[v], dfn[v] + sz[v] - 1, val[a[u]]);
}
ans = max(ans, val[a[u]]);
}
for (auto v : e[u])
if (v != fa && !vis[v]) DFS3(v, u);
}
void DFS1(int u)
{
vis[u] = 1;
clk = 0, ff[u] = 0, DFS2(u, 0);
T1.rt[u] = T2.rt[u] = T1.tot = T2.tot = 0;
// for (int i = 1; i <= n; i++) cout << dfn[i] << ' '; cout << "\n";
// for (int i = 1; i <= 3; i++) cout << val[i] << ' '; cout << '\n';
for (auto v : e[u])
if (!vis[v]) DFS3(v, u);
for (auto v : e[u])
if (!vis[v]) tsz = sz[v], Findrt(v, u), DFS1(rt);
}
int main()
{
// freopen("B.in", "r", stdin);
// freopen("B.out", "w", stdout);
#if DEBUG
#else
ios::sync_with_stdio(0), cin.tie(0);
#endif
Read(n);
for (int i = 1; i <= n; i++) Read(a[i]), xs[a[i]] ^= i;
for (int i = 1, u, v; i < n; i++) Read(u), Read(v), e[u].push_back(v), e[v].push_back(u);
T2.type = 1;
// T2.Update(T2.rt[1], 0, 1, n, 1, 1, 2);
// cout << T2.rt[1] << '\n';
// cout << T2.Query(T2.rt[1], 1, n, n, n) << '\n';
tsz = n, Findrt(1, 0), DFS1(rt);
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22024kb
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: 22068kb
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: 4ms
memory: 20016kb
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: 4ms
memory: 20124kb
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: 20260kb
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:
33
result:
wrong answer 1st lines differ - expected: '5', found: '33'