#include<bits/stdc++.h>
using namespace std;
const int mN = 500009;
vector<int> V[mN];
int L[mN],R[mN], A[mN], St[mN], En[mN], S[mN], Good[mN], Deg[mN], Sz[mN], Min[mN], Max[mN];
int c = 1;
int good_cnt = 0;
void dfs1(int x, int p){
L[x] = c;
R[x] = c;
A[c] = x;
c++;
for(int y : V[x]){
if(y == p) continue;
dfs1(y, x);
R[x] = R[y];
}
}
void dfs2(int x, int p){
if(Good[x]) Sz[x] = 1;
for(int y : V[x]){
if(y == p) continue;
dfs2(y, x);
Sz[x] += Sz[y];
}
if(x > 1 && (Good[x] || (Sz[x] > 0 && Sz[x] < good_cnt))){
Deg[x]++;
Deg[p]++;
}
}
void dfs3(int x, int p){
Min[x] = St[S[x]];
Max[x] = En[S[x]];
for(int y : V[x]){
if(y == p) continue;
dfs3(y, x);
Min[x] = min(Min[x], Min[y]);
Max[x] = max(Max[x], Max[y]);
}
if(x > 1 && Min[x] == L[x] && Max[x] == R[x]){
Good[x] = 1;
good_cnt++;
}
}
main(){
int n, k;
cin >> n >> k;
for(int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
V[a].push_back(b);
V[b].push_back(a);
}
for(int i = 1; i <= n; i++)
cin >> S[i];
dfs1(1, 0);
for(int i = 1; i <= n; i++){
int x = A[i];
if(!St[S[x]]) St[S[x]] = i;
En[S[x]] = i;
}
dfs3(1, 0);
dfs2(1, 0);
int leaf_cnt = 0;
for(int i = 1; i <= n; i++)
if(Deg[i] == 1) leaf_cnt++;
cout << (leaf_cnt + 1) / 2 << endl;
}
Compilation message
mergers.cpp:47:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
47 | main(){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12192 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
10 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
10 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
10 ms |
12160 KB |
Output is correct |
24 |
Correct |
7 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12192 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
10 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
10 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
10 ms |
12160 KB |
Output is correct |
24 |
Correct |
7 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
13 ms |
12416 KB |
Output is correct |
27 |
Correct |
13 ms |
12416 KB |
Output is correct |
28 |
Correct |
16 ms |
12544 KB |
Output is correct |
29 |
Correct |
13 ms |
12416 KB |
Output is correct |
30 |
Correct |
16 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
16 ms |
12544 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
34 |
Correct |
12 ms |
12416 KB |
Output is correct |
35 |
Correct |
13 ms |
12416 KB |
Output is correct |
36 |
Correct |
14 ms |
12416 KB |
Output is correct |
37 |
Correct |
12 ms |
12416 KB |
Output is correct |
38 |
Correct |
8 ms |
12160 KB |
Output is correct |
39 |
Correct |
13 ms |
12392 KB |
Output is correct |
40 |
Correct |
14 ms |
12288 KB |
Output is correct |
41 |
Correct |
15 ms |
12416 KB |
Output is correct |
42 |
Correct |
13 ms |
12416 KB |
Output is correct |
43 |
Correct |
15 ms |
12544 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
13 ms |
12416 KB |
Output is correct |
46 |
Correct |
13 ms |
12416 KB |
Output is correct |
47 |
Correct |
8 ms |
12160 KB |
Output is correct |
48 |
Correct |
15 ms |
12416 KB |
Output is correct |
49 |
Correct |
13 ms |
12416 KB |
Output is correct |
50 |
Correct |
13 ms |
12416 KB |
Output is correct |
51 |
Correct |
13 ms |
12416 KB |
Output is correct |
52 |
Correct |
15 ms |
12416 KB |
Output is correct |
53 |
Correct |
13 ms |
12416 KB |
Output is correct |
54 |
Correct |
13 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12192 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
10 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
10 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
10 ms |
12160 KB |
Output is correct |
24 |
Correct |
7 ms |
12160 KB |
Output is correct |
25 |
Correct |
9 ms |
12160 KB |
Output is correct |
26 |
Correct |
187 ms |
19444 KB |
Output is correct |
27 |
Correct |
203 ms |
19576 KB |
Output is correct |
28 |
Correct |
13 ms |
12416 KB |
Output is correct |
29 |
Correct |
9 ms |
12160 KB |
Output is correct |
30 |
Correct |
9 ms |
12160 KB |
Output is correct |
31 |
Correct |
203 ms |
20216 KB |
Output is correct |
32 |
Correct |
12 ms |
12288 KB |
Output is correct |
33 |
Correct |
247 ms |
25208 KB |
Output is correct |
34 |
Correct |
210 ms |
19576 KB |
Output is correct |
35 |
Correct |
13 ms |
12416 KB |
Output is correct |
36 |
Correct |
243 ms |
20600 KB |
Output is correct |
37 |
Correct |
14 ms |
12416 KB |
Output is correct |
38 |
Correct |
13 ms |
12416 KB |
Output is correct |
39 |
Correct |
177 ms |
19440 KB |
Output is correct |
40 |
Correct |
13 ms |
12544 KB |
Output is correct |
41 |
Correct |
220 ms |
19496 KB |
Output is correct |
42 |
Correct |
244 ms |
21496 KB |
Output is correct |
43 |
Correct |
8 ms |
12160 KB |
Output is correct |
44 |
Correct |
250 ms |
25464 KB |
Output is correct |
45 |
Correct |
223 ms |
22520 KB |
Output is correct |
46 |
Correct |
12 ms |
12416 KB |
Output is correct |
47 |
Correct |
14 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
18028 KB |
Output is correct |
2 |
Correct |
211 ms |
19972 KB |
Output is correct |
3 |
Correct |
15 ms |
12416 KB |
Output is correct |
4 |
Correct |
12 ms |
12288 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12288 KB |
Output is correct |
8 |
Correct |
266 ms |
19244 KB |
Output is correct |
9 |
Correct |
12 ms |
12288 KB |
Output is correct |
10 |
Correct |
238 ms |
18900 KB |
Output is correct |
11 |
Correct |
10 ms |
12160 KB |
Output is correct |
12 |
Correct |
260 ms |
20328 KB |
Output is correct |
13 |
Correct |
268 ms |
20984 KB |
Output is correct |
14 |
Correct |
263 ms |
21368 KB |
Output is correct |
15 |
Correct |
191 ms |
19416 KB |
Output is correct |
16 |
Correct |
15 ms |
12416 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
231 ms |
21604 KB |
Output is correct |
19 |
Correct |
277 ms |
24468 KB |
Output is correct |
20 |
Correct |
15 ms |
12416 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
197 ms |
21184 KB |
Output is correct |
23 |
Correct |
13 ms |
12436 KB |
Output is correct |
24 |
Correct |
263 ms |
20472 KB |
Output is correct |
25 |
Correct |
297 ms |
23488 KB |
Output is correct |
26 |
Correct |
14 ms |
12416 KB |
Output is correct |
27 |
Correct |
15 ms |
12416 KB |
Output is correct |
28 |
Correct |
14 ms |
12416 KB |
Output is correct |
29 |
Correct |
13 ms |
12416 KB |
Output is correct |
30 |
Correct |
14 ms |
12416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12192 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
10 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
10 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
10 ms |
12160 KB |
Output is correct |
24 |
Correct |
7 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
13 ms |
12416 KB |
Output is correct |
27 |
Correct |
13 ms |
12416 KB |
Output is correct |
28 |
Correct |
16 ms |
12544 KB |
Output is correct |
29 |
Correct |
13 ms |
12416 KB |
Output is correct |
30 |
Correct |
16 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
16 ms |
12544 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
34 |
Correct |
12 ms |
12416 KB |
Output is correct |
35 |
Correct |
13 ms |
12416 KB |
Output is correct |
36 |
Correct |
14 ms |
12416 KB |
Output is correct |
37 |
Correct |
12 ms |
12416 KB |
Output is correct |
38 |
Correct |
8 ms |
12160 KB |
Output is correct |
39 |
Correct |
13 ms |
12392 KB |
Output is correct |
40 |
Correct |
14 ms |
12288 KB |
Output is correct |
41 |
Correct |
15 ms |
12416 KB |
Output is correct |
42 |
Correct |
13 ms |
12416 KB |
Output is correct |
43 |
Correct |
15 ms |
12544 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
13 ms |
12416 KB |
Output is correct |
46 |
Correct |
13 ms |
12416 KB |
Output is correct |
47 |
Correct |
8 ms |
12160 KB |
Output is correct |
48 |
Correct |
15 ms |
12416 KB |
Output is correct |
49 |
Correct |
13 ms |
12416 KB |
Output is correct |
50 |
Correct |
13 ms |
12416 KB |
Output is correct |
51 |
Correct |
13 ms |
12416 KB |
Output is correct |
52 |
Correct |
15 ms |
12416 KB |
Output is correct |
53 |
Correct |
13 ms |
12416 KB |
Output is correct |
54 |
Correct |
13 ms |
12288 KB |
Output is correct |
55 |
Correct |
9 ms |
12160 KB |
Output is correct |
56 |
Correct |
187 ms |
19444 KB |
Output is correct |
57 |
Correct |
203 ms |
19576 KB |
Output is correct |
58 |
Correct |
13 ms |
12416 KB |
Output is correct |
59 |
Correct |
9 ms |
12160 KB |
Output is correct |
60 |
Correct |
9 ms |
12160 KB |
Output is correct |
61 |
Correct |
203 ms |
20216 KB |
Output is correct |
62 |
Correct |
12 ms |
12288 KB |
Output is correct |
63 |
Correct |
247 ms |
25208 KB |
Output is correct |
64 |
Correct |
210 ms |
19576 KB |
Output is correct |
65 |
Correct |
13 ms |
12416 KB |
Output is correct |
66 |
Correct |
243 ms |
20600 KB |
Output is correct |
67 |
Correct |
14 ms |
12416 KB |
Output is correct |
68 |
Correct |
13 ms |
12416 KB |
Output is correct |
69 |
Correct |
177 ms |
19440 KB |
Output is correct |
70 |
Correct |
13 ms |
12544 KB |
Output is correct |
71 |
Correct |
220 ms |
19496 KB |
Output is correct |
72 |
Correct |
244 ms |
21496 KB |
Output is correct |
73 |
Correct |
8 ms |
12160 KB |
Output is correct |
74 |
Correct |
250 ms |
25464 KB |
Output is correct |
75 |
Correct |
223 ms |
22520 KB |
Output is correct |
76 |
Correct |
12 ms |
12416 KB |
Output is correct |
77 |
Correct |
14 ms |
12288 KB |
Output is correct |
78 |
Correct |
180 ms |
18028 KB |
Output is correct |
79 |
Correct |
211 ms |
19972 KB |
Output is correct |
80 |
Correct |
15 ms |
12416 KB |
Output is correct |
81 |
Correct |
12 ms |
12288 KB |
Output is correct |
82 |
Correct |
8 ms |
12160 KB |
Output is correct |
83 |
Correct |
8 ms |
12160 KB |
Output is correct |
84 |
Correct |
12 ms |
12288 KB |
Output is correct |
85 |
Correct |
266 ms |
19244 KB |
Output is correct |
86 |
Correct |
12 ms |
12288 KB |
Output is correct |
87 |
Correct |
238 ms |
18900 KB |
Output is correct |
88 |
Correct |
10 ms |
12160 KB |
Output is correct |
89 |
Correct |
260 ms |
20328 KB |
Output is correct |
90 |
Correct |
268 ms |
20984 KB |
Output is correct |
91 |
Correct |
263 ms |
21368 KB |
Output is correct |
92 |
Correct |
191 ms |
19416 KB |
Output is correct |
93 |
Correct |
15 ms |
12416 KB |
Output is correct |
94 |
Correct |
8 ms |
12160 KB |
Output is correct |
95 |
Correct |
231 ms |
21604 KB |
Output is correct |
96 |
Correct |
277 ms |
24468 KB |
Output is correct |
97 |
Correct |
15 ms |
12416 KB |
Output is correct |
98 |
Correct |
8 ms |
12160 KB |
Output is correct |
99 |
Correct |
197 ms |
21184 KB |
Output is correct |
100 |
Correct |
13 ms |
12436 KB |
Output is correct |
101 |
Correct |
263 ms |
20472 KB |
Output is correct |
102 |
Correct |
297 ms |
23488 KB |
Output is correct |
103 |
Correct |
14 ms |
12416 KB |
Output is correct |
104 |
Correct |
15 ms |
12416 KB |
Output is correct |
105 |
Correct |
14 ms |
12416 KB |
Output is correct |
106 |
Correct |
13 ms |
12416 KB |
Output is correct |
107 |
Correct |
14 ms |
12416 KB |
Output is correct |
108 |
Correct |
1312 ms |
58792 KB |
Output is correct |
109 |
Correct |
1513 ms |
63840 KB |
Output is correct |
110 |
Correct |
1526 ms |
72804 KB |
Output is correct |
111 |
Correct |
1715 ms |
78092 KB |
Output is correct |
112 |
Correct |
1684 ms |
70324 KB |
Output is correct |
113 |
Correct |
1267 ms |
60880 KB |
Output is correct |
114 |
Correct |
1319 ms |
49668 KB |
Output is correct |
115 |
Correct |
1291 ms |
49704 KB |
Output is correct |
116 |
Correct |
1462 ms |
55980 KB |
Output is correct |
117 |
Correct |
1518 ms |
59700 KB |
Output is correct |
118 |
Correct |
1342 ms |
53208 KB |
Output is correct |
119 |
Correct |
1513 ms |
59756 KB |
Output is correct |
120 |
Correct |
1579 ms |
70884 KB |
Output is correct |
121 |
Correct |
1519 ms |
59900 KB |
Output is correct |
122 |
Correct |
1501 ms |
58104 KB |
Output is correct |
123 |
Correct |
1226 ms |
61464 KB |
Output is correct |
124 |
Correct |
1593 ms |
58744 KB |
Output is correct |