#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define INF (0x3f3f3f3f)
using namespace std;
const int MAXN = 200055;
const int MAXM = 200055;
struct DJF {
DJF() { init(); }
int ud[MAXN];
void init() { iota(ud, ud+MAXN, 0); }
int uf(int i) { return i == ud[i] ? i : (ud[i] = uf(ud[i])); }
void uf(int a, int b) { ud[uf(b)] = uf(a); }
} djf;
struct NOD {
NOD() { init(); }
DJF djf;
int A[MAXN], B[MAXM];
int L, C;
void init() {
djf.init();
fill(A, A+MAXN, 0);
fill(B, B+MAXM, 0);
L = C = 0;
}
void push(int x) {
L++;
A[L] = x;
B[x]++;
if(1 == B[x]) C++;
djf.ud[L] = L;
}
void pop() {
if(djf.uf(L) == L) {
int x = A[L];
B[x]--;
if(!B[x]) C--;
}
L--;
}
void f(int s, int e) {
for(int x;;) {
e = djf.uf(e);
if(e < s) break;
x = A[e];
B[x]--;
if(!B[x]) C--;
djf.uf(e-1, e);
}
}
void g(int i) {
if(djf.uf(i) != i) {
djf.ud[i] = i;
int x = A[i];
B[x]++;
if(1 == B[x]) C++;
}
}
int get() { return C; }
} nod;
vector<int> G[MAXN], T[MAXN];
int prt[MAXN], dep[MAXN], dmx[MAXN];
int A[MAXN];
int Ans[MAXN], PAns[MAXN];
int N, M, TA, TB;
void dfs(int i) {
dmx[i] = dep[i];
for(int v : G[i]) {
if(dep[v]) continue;
dep[v] = dep[i] + 1;
prt[v] = i;
dfs(v);
upmax(dmx[i], dmx[v]);
}
}
void f(int v) {
nod.push(A[v]);
int d1 = T[v].empty() ? 0 : dmx[T[v][0]] - dep[v];
int d2 = sz(T[v]) < 2 ? 0 : dmx[T[v][1]] - dep[v];
if(!T[v].empty()) {
nod.f(max(1, dep[v]-d2), dep[v]-1);
f(T[v][0]);
nod.g(dep[v]);
}
if(1 < sz(T[v])) {
for(int i = 1, n = sz(T[v]); i < n; i++) {
nod.f(max(1, dep[v]-d1), dep[v]-1);
f(T[v][i]);
nod.g(dep[v]);
}
}
nod.f(max(1, dep[v]-d1), dep[v]-1);
nod.pop();
Ans[v] = nod.get();
}
void solve(int Rt) {
fill(dep, dep+N+1, 0);
fill(prt, prt+N+1, 0);
dep[Rt] = 1; dfs(Rt);
nod.init();
for(int i = 1; i <= N; i++) T[i].clear();
for(int i = 1; i <= N; i++) {
for(int v : G[i]) {
if(v == prt[i]) continue;
T[i].eb(v);
}
sort(allv(T[i]), [&](int a, int b) {
return dmx[a] > dmx[b];
});
}
f(Rt);
}
int main() {
ios::sync_with_stdio(false);
cin >> N >> M;
for(int i = 1, a, b; i < N; i++) {
cin >> a >> b;
G[a].eb(b);
G[b].eb(a);
}
for(int i = 1; i <= N; i++) cin >> A[i];
dep[1] = 1; dfs(1);
TA = int(max_element(dep+1, dep+N+1) - dep);
fill(dep, dep+N+1, 0);
dep[TA] = 1; dfs(TA);
TB = int(max_element(dep+1, dep+N+1) - dep);
solve(TA);
for(int i = 1; i <= N; i++) swap(Ans[i], PAns[i]);
solve(TB);
for(int i = 1; i <= N; i++) printf("%d\n", max(Ans[i], PAns[i]));
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12928 KB |
Output is correct |
2 |
Correct |
14 ms |
13056 KB |
Output is correct |
3 |
Correct |
14 ms |
13056 KB |
Output is correct |
4 |
Correct |
15 ms |
13184 KB |
Output is correct |
5 |
Correct |
19 ms |
13056 KB |
Output is correct |
6 |
Correct |
19 ms |
13312 KB |
Output is correct |
7 |
Correct |
15 ms |
13184 KB |
Output is correct |
8 |
Correct |
16 ms |
13056 KB |
Output is correct |
9 |
Correct |
15 ms |
13184 KB |
Output is correct |
10 |
Correct |
16 ms |
13056 KB |
Output is correct |
11 |
Correct |
17 ms |
13056 KB |
Output is correct |
12 |
Correct |
14 ms |
13056 KB |
Output is correct |
13 |
Correct |
16 ms |
13312 KB |
Output is correct |
14 |
Correct |
15 ms |
13184 KB |
Output is correct |
15 |
Correct |
15 ms |
13184 KB |
Output is correct |
16 |
Correct |
14 ms |
13056 KB |
Output is correct |
17 |
Correct |
15 ms |
13176 KB |
Output is correct |
18 |
Correct |
17 ms |
13176 KB |
Output is correct |
19 |
Correct |
15 ms |
13056 KB |
Output is correct |
20 |
Correct |
15 ms |
13440 KB |
Output is correct |
21 |
Correct |
16 ms |
13184 KB |
Output is correct |
22 |
Correct |
17 ms |
13184 KB |
Output is correct |
23 |
Correct |
17 ms |
13184 KB |
Output is correct |
24 |
Correct |
16 ms |
13084 KB |
Output is correct |
25 |
Correct |
16 ms |
13056 KB |
Output is correct |
26 |
Correct |
14 ms |
13056 KB |
Output is correct |
27 |
Correct |
15 ms |
13440 KB |
Output is correct |
28 |
Correct |
17 ms |
13184 KB |
Output is correct |
29 |
Correct |
15 ms |
13184 KB |
Output is correct |
30 |
Correct |
15 ms |
13072 KB |
Output is correct |
31 |
Correct |
14 ms |
13312 KB |
Output is correct |
32 |
Correct |
15 ms |
13184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
22284 KB |
Output is correct |
2 |
Correct |
336 ms |
40788 KB |
Output is correct |
3 |
Correct |
77 ms |
17656 KB |
Output is correct |
4 |
Correct |
457 ms |
30876 KB |
Output is correct |
5 |
Correct |
704 ms |
61428 KB |
Output is correct |
6 |
Correct |
655 ms |
46712 KB |
Output is correct |
7 |
Correct |
453 ms |
30684 KB |
Output is correct |
8 |
Correct |
546 ms |
33688 KB |
Output is correct |
9 |
Correct |
550 ms |
33016 KB |
Output is correct |
10 |
Correct |
522 ms |
32640 KB |
Output is correct |
11 |
Correct |
261 ms |
28912 KB |
Output is correct |
12 |
Correct |
631 ms |
49144 KB |
Output is correct |
13 |
Correct |
569 ms |
44712 KB |
Output is correct |
14 |
Correct |
610 ms |
44608 KB |
Output is correct |
15 |
Correct |
224 ms |
28796 KB |
Output is correct |
16 |
Correct |
561 ms |
50600 KB |
Output is correct |
17 |
Correct |
665 ms |
46320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
475 ms |
26084 KB |
Output is correct |
2 |
Correct |
807 ms |
60192 KB |
Output is correct |
3 |
Correct |
72 ms |
18424 KB |
Output is correct |
4 |
Correct |
579 ms |
30968 KB |
Output is correct |
5 |
Correct |
772 ms |
62276 KB |
Output is correct |
6 |
Correct |
677 ms |
47224 KB |
Output is correct |
7 |
Correct |
448 ms |
30700 KB |
Output is correct |
8 |
Correct |
566 ms |
36784 KB |
Output is correct |
9 |
Correct |
496 ms |
34552 KB |
Output is correct |
10 |
Correct |
548 ms |
32988 KB |
Output is correct |
11 |
Correct |
402 ms |
30324 KB |
Output is correct |
12 |
Correct |
705 ms |
55544 KB |
Output is correct |
13 |
Correct |
538 ms |
43296 KB |
Output is correct |
14 |
Correct |
560 ms |
44152 KB |
Output is correct |
15 |
Correct |
250 ms |
28700 KB |
Output is correct |
16 |
Correct |
682 ms |
51184 KB |
Output is correct |
17 |
Correct |
663 ms |
46528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12928 KB |
Output is correct |
2 |
Correct |
14 ms |
13056 KB |
Output is correct |
3 |
Correct |
14 ms |
13056 KB |
Output is correct |
4 |
Correct |
15 ms |
13184 KB |
Output is correct |
5 |
Correct |
19 ms |
13056 KB |
Output is correct |
6 |
Correct |
19 ms |
13312 KB |
Output is correct |
7 |
Correct |
15 ms |
13184 KB |
Output is correct |
8 |
Correct |
16 ms |
13056 KB |
Output is correct |
9 |
Correct |
15 ms |
13184 KB |
Output is correct |
10 |
Correct |
16 ms |
13056 KB |
Output is correct |
11 |
Correct |
17 ms |
13056 KB |
Output is correct |
12 |
Correct |
14 ms |
13056 KB |
Output is correct |
13 |
Correct |
16 ms |
13312 KB |
Output is correct |
14 |
Correct |
15 ms |
13184 KB |
Output is correct |
15 |
Correct |
15 ms |
13184 KB |
Output is correct |
16 |
Correct |
14 ms |
13056 KB |
Output is correct |
17 |
Correct |
15 ms |
13176 KB |
Output is correct |
18 |
Correct |
17 ms |
13176 KB |
Output is correct |
19 |
Correct |
15 ms |
13056 KB |
Output is correct |
20 |
Correct |
15 ms |
13440 KB |
Output is correct |
21 |
Correct |
16 ms |
13184 KB |
Output is correct |
22 |
Correct |
17 ms |
13184 KB |
Output is correct |
23 |
Correct |
17 ms |
13184 KB |
Output is correct |
24 |
Correct |
16 ms |
13084 KB |
Output is correct |
25 |
Correct |
16 ms |
13056 KB |
Output is correct |
26 |
Correct |
14 ms |
13056 KB |
Output is correct |
27 |
Correct |
15 ms |
13440 KB |
Output is correct |
28 |
Correct |
17 ms |
13184 KB |
Output is correct |
29 |
Correct |
15 ms |
13184 KB |
Output is correct |
30 |
Correct |
15 ms |
13072 KB |
Output is correct |
31 |
Correct |
14 ms |
13312 KB |
Output is correct |
32 |
Correct |
15 ms |
13184 KB |
Output is correct |
33 |
Correct |
224 ms |
22284 KB |
Output is correct |
34 |
Correct |
336 ms |
40788 KB |
Output is correct |
35 |
Correct |
77 ms |
17656 KB |
Output is correct |
36 |
Correct |
457 ms |
30876 KB |
Output is correct |
37 |
Correct |
704 ms |
61428 KB |
Output is correct |
38 |
Correct |
655 ms |
46712 KB |
Output is correct |
39 |
Correct |
453 ms |
30684 KB |
Output is correct |
40 |
Correct |
546 ms |
33688 KB |
Output is correct |
41 |
Correct |
550 ms |
33016 KB |
Output is correct |
42 |
Correct |
522 ms |
32640 KB |
Output is correct |
43 |
Correct |
261 ms |
28912 KB |
Output is correct |
44 |
Correct |
631 ms |
49144 KB |
Output is correct |
45 |
Correct |
569 ms |
44712 KB |
Output is correct |
46 |
Correct |
610 ms |
44608 KB |
Output is correct |
47 |
Correct |
224 ms |
28796 KB |
Output is correct |
48 |
Correct |
561 ms |
50600 KB |
Output is correct |
49 |
Correct |
665 ms |
46320 KB |
Output is correct |
50 |
Correct |
475 ms |
26084 KB |
Output is correct |
51 |
Correct |
807 ms |
60192 KB |
Output is correct |
52 |
Correct |
72 ms |
18424 KB |
Output is correct |
53 |
Correct |
579 ms |
30968 KB |
Output is correct |
54 |
Correct |
772 ms |
62276 KB |
Output is correct |
55 |
Correct |
677 ms |
47224 KB |
Output is correct |
56 |
Correct |
448 ms |
30700 KB |
Output is correct |
57 |
Correct |
566 ms |
36784 KB |
Output is correct |
58 |
Correct |
496 ms |
34552 KB |
Output is correct |
59 |
Correct |
548 ms |
32988 KB |
Output is correct |
60 |
Correct |
402 ms |
30324 KB |
Output is correct |
61 |
Correct |
705 ms |
55544 KB |
Output is correct |
62 |
Correct |
538 ms |
43296 KB |
Output is correct |
63 |
Correct |
560 ms |
44152 KB |
Output is correct |
64 |
Correct |
250 ms |
28700 KB |
Output is correct |
65 |
Correct |
682 ms |
51184 KB |
Output is correct |
66 |
Correct |
663 ms |
46528 KB |
Output is correct |
67 |
Correct |
40 ms |
15352 KB |
Output is correct |
68 |
Correct |
223 ms |
34512 KB |
Output is correct |
69 |
Correct |
376 ms |
34432 KB |
Output is correct |
70 |
Correct |
494 ms |
30832 KB |
Output is correct |
71 |
Correct |
696 ms |
61468 KB |
Output is correct |
72 |
Correct |
653 ms |
46592 KB |
Output is correct |
73 |
Correct |
598 ms |
30612 KB |
Output is correct |
74 |
Correct |
558 ms |
36216 KB |
Output is correct |
75 |
Correct |
551 ms |
33192 KB |
Output is correct |
76 |
Correct |
589 ms |
32980 KB |
Output is correct |
77 |
Correct |
427 ms |
29936 KB |
Output is correct |
78 |
Correct |
705 ms |
52436 KB |
Output is correct |
79 |
Correct |
561 ms |
48372 KB |
Output is correct |
80 |
Correct |
574 ms |
42688 KB |
Output is correct |
81 |
Correct |
242 ms |
28772 KB |
Output is correct |
82 |
Correct |
523 ms |
50468 KB |
Output is correct |
83 |
Correct |
622 ms |
46032 KB |
Output is correct |
84 |
Correct |
529 ms |
30840 KB |
Output is correct |
85 |
Correct |
771 ms |
61816 KB |
Output is correct |
86 |
Correct |
704 ms |
46832 KB |
Output is correct |
87 |
Correct |
484 ms |
30716 KB |
Output is correct |
88 |
Correct |
511 ms |
36856 KB |
Output is correct |
89 |
Correct |
457 ms |
33656 KB |
Output is correct |
90 |
Correct |
485 ms |
33052 KB |
Output is correct |
91 |
Correct |
367 ms |
29920 KB |
Output is correct |
92 |
Correct |
734 ms |
60580 KB |
Output is correct |
93 |
Correct |
518 ms |
40576 KB |
Output is correct |
94 |
Correct |
528 ms |
38136 KB |
Output is correct |
95 |
Correct |
234 ms |
28648 KB |
Output is correct |
96 |
Correct |
581 ms |
50700 KB |
Output is correct |
97 |
Correct |
644 ms |
46196 KB |
Output is correct |
98 |
Correct |
522 ms |
30816 KB |
Output is correct |
99 |
Correct |
806 ms |
61988 KB |
Output is correct |
100 |
Correct |
736 ms |
46836 KB |
Output is correct |
101 |
Correct |
503 ms |
30584 KB |
Output is correct |
102 |
Correct |
552 ms |
35452 KB |
Output is correct |
103 |
Correct |
507 ms |
33468 KB |
Output is correct |
104 |
Correct |
538 ms |
32864 KB |
Output is correct |
105 |
Correct |
374 ms |
29692 KB |
Output is correct |
106 |
Correct |
717 ms |
47348 KB |
Output is correct |
107 |
Correct |
618 ms |
48112 KB |
Output is correct |
108 |
Correct |
607 ms |
39640 KB |
Output is correct |
109 |
Correct |
257 ms |
28468 KB |
Output is correct |
110 |
Correct |
608 ms |
50928 KB |
Output is correct |
111 |
Correct |
567 ms |
46452 KB |
Output is correct |