QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#554734 | #7513. Palindromic Beads | OIer_kzc | WA | 2ms | 10484kb | C++14 | 2.1kb | 2024-09-09 15:13:03 | 2024-09-09 15:13:03 |
Judging History
answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#define LOG(FMT...) fprintf(stderr, FMT)
#define eb emplace_back
using namespace std;
typedef unsigned long long ULL;
constexpr int N = 200005, TC = 4 * N, MC = N * 60;
int n, c[N];
vector<int> e[N];
int mat[N], last[N];
int bg[N], ed[N], ids;
int fa[N];
void dfs1(int x) {
bg[x] = ++ids;
for (int y : e[x]) {
if (y == fa[x]) {
continue;
}
fa[y] = x;
dfs1(y);
}
ed[x] = ids;
}
int apr(int y, int x) {
int l = 0, r = e[y].size(), md;
while (l < r) {
md = l + r >> 1;
if (ed[e[y][md]] >= bg[x]) {
r = md;
} else {
l = md + 1;
}
}
return e[y][r];
}
#define L (z << 1)
#define R (z << 1 | 1)
int v[TC];
struct His {
int z, v;
} stk[MC];
int tt;
void mdf(int ql, int qr, int c, int l = 1, int r = n, int z = 1) {
if (l >= ql && r <= qr) {
if (c > v[z]) {
stk[++tt] = {z, v[z]};
v[z] = c;
}
} else {
int mid = l + r >> 1;
if (ql <= mid) {
mdf(ql, qr, c, l, mid, L);
}
if (mid < qr) {
mdf(ql, qr, c, mid + 1, r, R);
}
}
}
int qry(int x, int l = 1, int r = n, int z = 1) {
if (l == r) {
return v[z];
}
int mid = l + r >> 1;
return max(x <= mid ? qry(x, l, mid, L) : qry(x, mid + 1, r, R), v[z]);
}
int res = 1;
void dfs2(int x) {
int y = mat[x];
int bac = tt;
if (y) {
int w = qry(bg[y]);
if (!w && y != fa[x] && x != fa[y]) {
w = 1;
}
w += 2;
if (bg[y] <= bg[x] && ed[x] <= ed[y]) {
int z = apr(y, x);
mdf(1, bg[z] - 1, w);
if (ed[z] < n) {
mdf(ed[z] + 1, n, w);
}
} else {
mdf(bg[y], ed[y], w);
}
res = max(res, w);
}
for (int y : e[x]) {
if (y != fa[x]) {
dfs2(y);
}
}
while (tt > bac) {
auto [z, w] = stk[tt--];
v[z] = w;
}
}
int main() {
scanf("%d", &n);
for (int x = 1; x <= n; ++x) {
scanf("%d", c + x);
int &y = last[c[x]];
if (y) {
mat[x] = y;
} else {
y = x;
}
}
for (int i = 1, x, y; i < n; ++i) {
scanf("%d%d", &x, &y);
e[x].eb(y), e[y].eb(x);
}
dfs1(1);
dfs2(1);
printf("%d\n", res);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10484kb
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: 8888kb
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: 10048kb
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: 9972kb
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: 2ms
memory: 10156kb
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'