#include<cstdio>
#include<algorithm>
#include<vector>
#define N_ 201000
using namespace std;
int n, m, Col[N_], Num[N_], cnt, Ed[N_], S[N_];
vector<int>E[N_], Ch[N_];
int D1[N_], D2[N_], P1[N_], par[N_][20], Dep[N_];
struct Range {
int b, e;
bool operator<(const Range &p)const {
return b < p.b;
}
};
vector<Range>G[N_];
void DFS1(int a, int pp) {
Num[a] = ++cnt;
par[a][0] = pp;
for (int i = 0; i < 18; i++)par[a][i + 1] = par[par[a][i]][i];
P1[a] = a;
for (auto &x : E[a]) {
if (x == pp)continue;
Dep[x] = Dep[a] + 1;
DFS1(x, a);
Ch[a].push_back(x);
D2[a] = max(D2[a], D1[a] + D1[x] + 1);
D2[a] = max(D2[a], D2[x]);
if (D1[a] < D1[x] + 1) {
D1[a] = D1[x] + 1;
P1[a] = P1[x];
}
}
Ed[a] = cnt;
}
int Up(int a, int d) {
int i = 0;
while (d) {
if (d & 1)a = par[a][i];
d >>= 1, i++;
}
return a;
}
int LCA(int a, int b) {
if (Dep[a] < Dep[b])return LCA(b, a);
a = Up(a, Dep[a] - Dep[b]);
for (int i = 18; i >= 0; i--) {
if (par[a][i] != par[b][i])a = par[a][i], b = par[b][i];
}
if (a != b)a = par[a][0];
return a;
}
void Put(int col, int b, int e) {
if (b > e)return;
G[col].push_back({ b,e });
}
void Add(int a, int x) {
int l = LCA(a, x);
int d = Dep[a] + Dep[x] - Dep[l] * 2;
if (Dep[a] <= Dep[x]) {
int t = Up(x, (d - 1) / 2);
Put(Col[a], Num[t], Ed[t]);
}
else {
int t = Up(a, d / 2);
Put(Col[a], Num[1], Num[t] - 1);
Put(Col[a], Ed[t] + 1, Ed[1]);
}
}
void DFS2(int a, int pp, int d1, int d2, int p1) {
if (pp) {
if (d1 > d2) {
Add(a, p1);
}
}
int dd2[2][2] = { {-3,0},{-3,0} }, dd1[3][2] = { {0,a},{-3,0},{ -3,0 } }, i;
for (auto &x : Ch[a]) {
if (D2[x] < D1[x] + 1) {
Add(a, P1[x]);
}
if (dd2[0][0] < D2[x]) {
dd2[1][0] = dd2[0][0], dd2[1][1] = dd2[0][1];
dd2[0][0] = D2[x], dd2[0][1] = x;
}
else if (dd2[1][0] < D2[x]) {
dd2[1][0] = D2[x], dd2[1][1] = x;
}
if (dd1[0][0] < D1[x] + 1) {
dd1[2][0] = dd1[1][0], dd1[2][1] = dd1[1][1];
dd1[1][0] = dd1[0][0], dd1[1][1] = dd1[0][1];
dd1[0][0] = D1[x] + 1, dd1[0][1] = x;
}
else if (dd1[1][0] < D1[x] + 1) {
dd1[2][0] = dd1[1][0], dd1[2][1] = dd1[1][1];
dd1[1][0] = D1[x] + 1, dd1[1][1] = x;
}
else if (dd1[2][0] < D1[x] + 1) {
dd1[2][0] = D1[x] + 1, dd1[2][1] = x;
}
}
for (auto &x : Ch[a]) {
int nd2 = d2, nd1 = d1 + 1, np1 = p1;
if (x == dd2[0][1]) nd2 = max(nd2, dd2[1][0]);
else nd2 = max(nd2, dd2[0][0]);
int ck = 0;
if (x == dd1[0][1])ck = 1;
if (nd1 < dd1[ck][0] + 1) {
nd1 = dd1[ck][0] + 1;
np1 = P1[dd1[ck][1]];
}
nd2 = max(nd2, dd1[ck][0] + d1);
if (x == dd1[0][1])nd2 = max(nd2, dd1[1][0] + dd1[2][0]);
else if (x == dd1[1][1])nd2 = max(nd2, dd1[0][0] + dd1[2][0]);
else nd2 = max(nd2, dd1[0][0] + dd1[1][0]);
DFS2(x, a, nd1, nd2, np1);
}
}
void Go(vector<Range> &T) {
sort(T.begin(), T.end());
int b = -1, e = -1;
for (auto &t : T) {
if (e < t.b) {
if (e != -1) {
S[b]++;
S[e + 1]--;
}
b = t.b;
}
e = max(e, t.e);
}
if (e != -1) {
S[b]++, S[e + 1]--;
}
}
int main() {
scanf("%d%d", &n, &m);
int i, a, b, j;
for (i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
E[a].push_back(b);
E[b].push_back(a);
}
for (i = 1; i <= n; i++) {
scanf("%d", &Col[i]);
}
DFS1(1, 0);
DFS2(1, 0, 0, 0, 1);
for (i = 1; i <= m; i++) {
Go(G[i]);
}
for (i = 1; i <= n; i++)S[i] += S[i - 1];
for (i = 1; i <= n; i++) {
printf("%d\n", S[Num[i]]);
}
}
Compilation message
joi2019_ho_t5.cpp: In function 'void DFS2(int, int, int, int, int)':
joi2019_ho_t5.cpp:75:76: warning: unused variable 'i' [-Wunused-variable]
int dd2[2][2] = { {-3,0},{-3,0} }, dd1[3][2] = { {0,a},{-3,0},{ -3,0 } }, i;
^
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:136:15: warning: unused variable 'j' [-Wunused-variable]
int i, a, b, j;
^
joi2019_ho_t5.cpp:135:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:138:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:143:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Col[i]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14572 KB |
Output is correct |
2 |
Correct |
18 ms |
14840 KB |
Output is correct |
3 |
Correct |
21 ms |
14840 KB |
Output is correct |
4 |
Correct |
18 ms |
14948 KB |
Output is correct |
5 |
Correct |
18 ms |
14848 KB |
Output is correct |
6 |
Correct |
19 ms |
15144 KB |
Output is correct |
7 |
Correct |
17 ms |
14976 KB |
Output is correct |
8 |
Correct |
16 ms |
14820 KB |
Output is correct |
9 |
Correct |
19 ms |
14892 KB |
Output is correct |
10 |
Correct |
19 ms |
14848 KB |
Output is correct |
11 |
Correct |
16 ms |
14820 KB |
Output is correct |
12 |
Correct |
16 ms |
14848 KB |
Output is correct |
13 |
Correct |
20 ms |
14976 KB |
Output is correct |
14 |
Correct |
20 ms |
14976 KB |
Output is correct |
15 |
Correct |
22 ms |
14952 KB |
Output is correct |
16 |
Correct |
18 ms |
14820 KB |
Output is correct |
17 |
Correct |
19 ms |
15232 KB |
Output is correct |
18 |
Correct |
20 ms |
14976 KB |
Output is correct |
19 |
Correct |
17 ms |
14840 KB |
Output is correct |
20 |
Correct |
18 ms |
15096 KB |
Output is correct |
21 |
Correct |
21 ms |
14976 KB |
Output is correct |
22 |
Correct |
17 ms |
14888 KB |
Output is correct |
23 |
Correct |
20 ms |
14848 KB |
Output is correct |
24 |
Correct |
19 ms |
14840 KB |
Output is correct |
25 |
Correct |
17 ms |
14848 KB |
Output is correct |
26 |
Correct |
19 ms |
14848 KB |
Output is correct |
27 |
Correct |
22 ms |
15116 KB |
Output is correct |
28 |
Correct |
23 ms |
15088 KB |
Output is correct |
29 |
Correct |
21 ms |
14912 KB |
Output is correct |
30 |
Correct |
19 ms |
14820 KB |
Output is correct |
31 |
Correct |
18 ms |
14976 KB |
Output is correct |
32 |
Correct |
18 ms |
14976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
33984 KB |
Output is correct |
2 |
Correct |
565 ms |
52472 KB |
Output is correct |
3 |
Correct |
77 ms |
21624 KB |
Output is correct |
4 |
Correct |
649 ms |
51144 KB |
Output is correct |
5 |
Correct |
1167 ms |
70344 KB |
Output is correct |
6 |
Correct |
795 ms |
65660 KB |
Output is correct |
7 |
Correct |
549 ms |
51176 KB |
Output is correct |
8 |
Correct |
664 ms |
53068 KB |
Output is correct |
9 |
Correct |
573 ms |
52984 KB |
Output is correct |
10 |
Correct |
533 ms |
52968 KB |
Output is correct |
11 |
Correct |
373 ms |
49640 KB |
Output is correct |
12 |
Correct |
778 ms |
70632 KB |
Output is correct |
13 |
Correct |
717 ms |
60052 KB |
Output is correct |
14 |
Correct |
650 ms |
62340 KB |
Output is correct |
15 |
Correct |
414 ms |
47568 KB |
Output is correct |
16 |
Correct |
864 ms |
63820 KB |
Output is correct |
17 |
Correct |
674 ms |
62088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
386 ms |
42744 KB |
Output is correct |
2 |
Correct |
1164 ms |
74616 KB |
Output is correct |
3 |
Correct |
101 ms |
23388 KB |
Output is correct |
4 |
Correct |
572 ms |
54024 KB |
Output is correct |
5 |
Correct |
1302 ms |
76200 KB |
Output is correct |
6 |
Correct |
871 ms |
65400 KB |
Output is correct |
7 |
Correct |
747 ms |
53940 KB |
Output is correct |
8 |
Correct |
784 ms |
57596 KB |
Output is correct |
9 |
Correct |
660 ms |
56628 KB |
Output is correct |
10 |
Correct |
734 ms |
55800 KB |
Output is correct |
11 |
Correct |
565 ms |
53224 KB |
Output is correct |
12 |
Correct |
1076 ms |
70264 KB |
Output is correct |
13 |
Correct |
764 ms |
61168 KB |
Output is correct |
14 |
Correct |
660 ms |
65392 KB |
Output is correct |
15 |
Correct |
412 ms |
48828 KB |
Output is correct |
16 |
Correct |
933 ms |
66316 KB |
Output is correct |
17 |
Correct |
777 ms |
64372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14572 KB |
Output is correct |
2 |
Correct |
18 ms |
14840 KB |
Output is correct |
3 |
Correct |
21 ms |
14840 KB |
Output is correct |
4 |
Correct |
18 ms |
14948 KB |
Output is correct |
5 |
Correct |
18 ms |
14848 KB |
Output is correct |
6 |
Correct |
19 ms |
15144 KB |
Output is correct |
7 |
Correct |
17 ms |
14976 KB |
Output is correct |
8 |
Correct |
16 ms |
14820 KB |
Output is correct |
9 |
Correct |
19 ms |
14892 KB |
Output is correct |
10 |
Correct |
19 ms |
14848 KB |
Output is correct |
11 |
Correct |
16 ms |
14820 KB |
Output is correct |
12 |
Correct |
16 ms |
14848 KB |
Output is correct |
13 |
Correct |
20 ms |
14976 KB |
Output is correct |
14 |
Correct |
20 ms |
14976 KB |
Output is correct |
15 |
Correct |
22 ms |
14952 KB |
Output is correct |
16 |
Correct |
18 ms |
14820 KB |
Output is correct |
17 |
Correct |
19 ms |
15232 KB |
Output is correct |
18 |
Correct |
20 ms |
14976 KB |
Output is correct |
19 |
Correct |
17 ms |
14840 KB |
Output is correct |
20 |
Correct |
18 ms |
15096 KB |
Output is correct |
21 |
Correct |
21 ms |
14976 KB |
Output is correct |
22 |
Correct |
17 ms |
14888 KB |
Output is correct |
23 |
Correct |
20 ms |
14848 KB |
Output is correct |
24 |
Correct |
19 ms |
14840 KB |
Output is correct |
25 |
Correct |
17 ms |
14848 KB |
Output is correct |
26 |
Correct |
19 ms |
14848 KB |
Output is correct |
27 |
Correct |
22 ms |
15116 KB |
Output is correct |
28 |
Correct |
23 ms |
15088 KB |
Output is correct |
29 |
Correct |
21 ms |
14912 KB |
Output is correct |
30 |
Correct |
19 ms |
14820 KB |
Output is correct |
31 |
Correct |
18 ms |
14976 KB |
Output is correct |
32 |
Correct |
18 ms |
14976 KB |
Output is correct |
33 |
Correct |
285 ms |
33984 KB |
Output is correct |
34 |
Correct |
565 ms |
52472 KB |
Output is correct |
35 |
Correct |
77 ms |
21624 KB |
Output is correct |
36 |
Correct |
649 ms |
51144 KB |
Output is correct |
37 |
Correct |
1167 ms |
70344 KB |
Output is correct |
38 |
Correct |
795 ms |
65660 KB |
Output is correct |
39 |
Correct |
549 ms |
51176 KB |
Output is correct |
40 |
Correct |
664 ms |
53068 KB |
Output is correct |
41 |
Correct |
573 ms |
52984 KB |
Output is correct |
42 |
Correct |
533 ms |
52968 KB |
Output is correct |
43 |
Correct |
373 ms |
49640 KB |
Output is correct |
44 |
Correct |
778 ms |
70632 KB |
Output is correct |
45 |
Correct |
717 ms |
60052 KB |
Output is correct |
46 |
Correct |
650 ms |
62340 KB |
Output is correct |
47 |
Correct |
414 ms |
47568 KB |
Output is correct |
48 |
Correct |
864 ms |
63820 KB |
Output is correct |
49 |
Correct |
674 ms |
62088 KB |
Output is correct |
50 |
Correct |
386 ms |
42744 KB |
Output is correct |
51 |
Correct |
1164 ms |
74616 KB |
Output is correct |
52 |
Correct |
101 ms |
23388 KB |
Output is correct |
53 |
Correct |
572 ms |
54024 KB |
Output is correct |
54 |
Correct |
1302 ms |
76200 KB |
Output is correct |
55 |
Correct |
871 ms |
65400 KB |
Output is correct |
56 |
Correct |
747 ms |
53940 KB |
Output is correct |
57 |
Correct |
784 ms |
57596 KB |
Output is correct |
58 |
Correct |
660 ms |
56628 KB |
Output is correct |
59 |
Correct |
734 ms |
55800 KB |
Output is correct |
60 |
Correct |
565 ms |
53224 KB |
Output is correct |
61 |
Correct |
1076 ms |
70264 KB |
Output is correct |
62 |
Correct |
764 ms |
61168 KB |
Output is correct |
63 |
Correct |
660 ms |
65392 KB |
Output is correct |
64 |
Correct |
412 ms |
48828 KB |
Output is correct |
65 |
Correct |
933 ms |
66316 KB |
Output is correct |
66 |
Correct |
777 ms |
64372 KB |
Output is correct |
67 |
Correct |
80 ms |
19648 KB |
Output is correct |
68 |
Correct |
467 ms |
46960 KB |
Output is correct |
69 |
Correct |
473 ms |
44152 KB |
Output is correct |
70 |
Correct |
581 ms |
51392 KB |
Output is correct |
71 |
Correct |
1121 ms |
74072 KB |
Output is correct |
72 |
Correct |
686 ms |
64724 KB |
Output is correct |
73 |
Correct |
552 ms |
51336 KB |
Output is correct |
74 |
Correct |
587 ms |
56676 KB |
Output is correct |
75 |
Correct |
568 ms |
53040 KB |
Output is correct |
76 |
Correct |
533 ms |
52472 KB |
Output is correct |
77 |
Correct |
492 ms |
50808 KB |
Output is correct |
78 |
Correct |
948 ms |
65028 KB |
Output is correct |
79 |
Correct |
800 ms |
67688 KB |
Output is correct |
80 |
Correct |
652 ms |
63232 KB |
Output is correct |
81 |
Correct |
438 ms |
47440 KB |
Output is correct |
82 |
Correct |
818 ms |
73036 KB |
Output is correct |
83 |
Correct |
707 ms |
62020 KB |
Output is correct |
84 |
Correct |
702 ms |
51544 KB |
Output is correct |
85 |
Correct |
1127 ms |
80140 KB |
Output is correct |
86 |
Correct |
864 ms |
64000 KB |
Output is correct |
87 |
Correct |
657 ms |
51692 KB |
Output is correct |
88 |
Correct |
732 ms |
56964 KB |
Output is correct |
89 |
Correct |
699 ms |
54576 KB |
Output is correct |
90 |
Correct |
601 ms |
53748 KB |
Output is correct |
91 |
Correct |
590 ms |
51564 KB |
Output is correct |
92 |
Correct |
1094 ms |
79816 KB |
Output is correct |
93 |
Correct |
741 ms |
60404 KB |
Output is correct |
94 |
Correct |
611 ms |
55900 KB |
Output is correct |
95 |
Correct |
418 ms |
48044 KB |
Output is correct |
96 |
Correct |
918 ms |
63972 KB |
Output is correct |
97 |
Correct |
736 ms |
61928 KB |
Output is correct |
98 |
Correct |
608 ms |
53508 KB |
Output is correct |
99 |
Correct |
1145 ms |
83716 KB |
Output is correct |
100 |
Correct |
811 ms |
65844 KB |
Output is correct |
101 |
Correct |
597 ms |
52500 KB |
Output is correct |
102 |
Correct |
666 ms |
56316 KB |
Output is correct |
103 |
Correct |
654 ms |
55496 KB |
Output is correct |
104 |
Correct |
595 ms |
53868 KB |
Output is correct |
105 |
Correct |
513 ms |
51688 KB |
Output is correct |
106 |
Correct |
784 ms |
69688 KB |
Output is correct |
107 |
Correct |
878 ms |
64836 KB |
Output is correct |
108 |
Correct |
729 ms |
59656 KB |
Output is correct |
109 |
Correct |
444 ms |
48564 KB |
Output is correct |
110 |
Correct |
949 ms |
65412 KB |
Output is correct |
111 |
Correct |
709 ms |
63344 KB |
Output is correct |