#include <bits/stdc++.h>
#define pb emplace_back
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MAXN = 500000;
int n, k;
int a, b;
vector <int> path[MAXN+1];
int clr[MAXN+1];
int cnt_clr[MAXN+1];
unordered_map <int,int> cnt[MAXN+1];
int full[MAXN+1];
int dsu[MAXN+1];
vector <int> G[MAXN+1];
int leaves;
int fnd(int a) {
return a == dsu[a] ? a : dsu[a] = fnd(dsu[a]);
}
void Merge(int a, int b) {
a = fnd(a), b = fnd(b);
dsu[a] = b;
}
void DFS(int now, int par) {
cnt[now][clr[now]]++;
if (cnt_clr[clr[now]] == 1)
full[now]++;
for (int i : path[now]) {
if (i == par) continue;
DFS(i, now);
if (cnt[i].size() > cnt[now].size()) {
swap(cnt[now], cnt[i]);
full[now] = full[i];
}
for (auto [v, c] : cnt[i]) {
cnt[now][v] += c;
if (cnt[now][v] == cnt_clr[v])
full[now]++;
}
}
if (full[now] != cnt[now].size())
Merge(now, par);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
cin >> a >> b;
path[a].pb(b);
path[b].pb(a);
}
for (int i = 1; i <= n; i++) {
cin >> a;
clr[i] = a;
cnt_clr[a]++;
dsu[i] = i;
}
DFS(1, 0);
for (int i = 1; i <= n; i++) {
for (int j : path[i]) {
if (fnd(i) != fnd(j)) {
G[fnd(i)].pb(fnd(j));
}
}
}
for (int i = 1; i <= n; i++) {
if (!G[i].size()) continue;
bool flag = true;
for (int j : G[i]) {
flag &= (j == G[i][0]);
}
if (flag)
leaves++;
}
cout << (leaves+1)/2 << '\n';
}
Compilation message
mergers.cpp: In function 'void DFS(int, int)':
mergers.cpp:41:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for (auto [v, c] : cnt[i]) {
| ^
mergers.cpp:47:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::unordered_map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if (full[now] != cnt[now].size())
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
51180 KB |
Output is correct |
2 |
Correct |
37 ms |
51308 KB |
Output is correct |
3 |
Correct |
37 ms |
51308 KB |
Output is correct |
4 |
Correct |
41 ms |
51180 KB |
Output is correct |
5 |
Correct |
37 ms |
51180 KB |
Output is correct |
6 |
Correct |
38 ms |
51436 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
38 ms |
51308 KB |
Output is correct |
9 |
Correct |
37 ms |
51180 KB |
Output is correct |
10 |
Correct |
41 ms |
51180 KB |
Output is correct |
11 |
Correct |
36 ms |
51308 KB |
Output is correct |
12 |
Correct |
37 ms |
51308 KB |
Output is correct |
13 |
Correct |
37 ms |
51180 KB |
Output is correct |
14 |
Correct |
36 ms |
51308 KB |
Output is correct |
15 |
Correct |
38 ms |
51304 KB |
Output is correct |
16 |
Correct |
42 ms |
51180 KB |
Output is correct |
17 |
Correct |
37 ms |
51308 KB |
Output is correct |
18 |
Correct |
44 ms |
51200 KB |
Output is correct |
19 |
Correct |
37 ms |
51308 KB |
Output is correct |
20 |
Correct |
37 ms |
51308 KB |
Output is correct |
21 |
Correct |
42 ms |
51308 KB |
Output is correct |
22 |
Correct |
37 ms |
51308 KB |
Output is correct |
23 |
Correct |
42 ms |
51308 KB |
Output is correct |
24 |
Correct |
41 ms |
51308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
51180 KB |
Output is correct |
2 |
Correct |
37 ms |
51308 KB |
Output is correct |
3 |
Correct |
37 ms |
51308 KB |
Output is correct |
4 |
Correct |
41 ms |
51180 KB |
Output is correct |
5 |
Correct |
37 ms |
51180 KB |
Output is correct |
6 |
Correct |
38 ms |
51436 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
38 ms |
51308 KB |
Output is correct |
9 |
Correct |
37 ms |
51180 KB |
Output is correct |
10 |
Correct |
41 ms |
51180 KB |
Output is correct |
11 |
Correct |
36 ms |
51308 KB |
Output is correct |
12 |
Correct |
37 ms |
51308 KB |
Output is correct |
13 |
Correct |
37 ms |
51180 KB |
Output is correct |
14 |
Correct |
36 ms |
51308 KB |
Output is correct |
15 |
Correct |
38 ms |
51304 KB |
Output is correct |
16 |
Correct |
42 ms |
51180 KB |
Output is correct |
17 |
Correct |
37 ms |
51308 KB |
Output is correct |
18 |
Correct |
44 ms |
51200 KB |
Output is correct |
19 |
Correct |
37 ms |
51308 KB |
Output is correct |
20 |
Correct |
37 ms |
51308 KB |
Output is correct |
21 |
Correct |
42 ms |
51308 KB |
Output is correct |
22 |
Correct |
37 ms |
51308 KB |
Output is correct |
23 |
Correct |
42 ms |
51308 KB |
Output is correct |
24 |
Correct |
41 ms |
51308 KB |
Output is correct |
25 |
Correct |
37 ms |
51308 KB |
Output is correct |
26 |
Correct |
40 ms |
51948 KB |
Output is correct |
27 |
Correct |
39 ms |
51692 KB |
Output is correct |
28 |
Correct |
40 ms |
52076 KB |
Output is correct |
29 |
Correct |
41 ms |
52204 KB |
Output is correct |
30 |
Correct |
40 ms |
51880 KB |
Output is correct |
31 |
Correct |
36 ms |
51208 KB |
Output is correct |
32 |
Correct |
40 ms |
52076 KB |
Output is correct |
33 |
Correct |
37 ms |
51308 KB |
Output is correct |
34 |
Correct |
39 ms |
51564 KB |
Output is correct |
35 |
Correct |
41 ms |
52204 KB |
Output is correct |
36 |
Correct |
41 ms |
51564 KB |
Output is correct |
37 |
Correct |
41 ms |
51948 KB |
Output is correct |
38 |
Correct |
40 ms |
51452 KB |
Output is correct |
39 |
Correct |
41 ms |
51948 KB |
Output is correct |
40 |
Correct |
39 ms |
51564 KB |
Output is correct |
41 |
Correct |
40 ms |
51564 KB |
Output is correct |
42 |
Correct |
39 ms |
51692 KB |
Output is correct |
43 |
Correct |
39 ms |
51948 KB |
Output is correct |
44 |
Correct |
37 ms |
51308 KB |
Output is correct |
45 |
Correct |
40 ms |
51948 KB |
Output is correct |
46 |
Correct |
41 ms |
51820 KB |
Output is correct |
47 |
Correct |
37 ms |
51308 KB |
Output is correct |
48 |
Correct |
39 ms |
51692 KB |
Output is correct |
49 |
Correct |
40 ms |
51948 KB |
Output is correct |
50 |
Correct |
43 ms |
51948 KB |
Output is correct |
51 |
Correct |
39 ms |
51692 KB |
Output is correct |
52 |
Correct |
40 ms |
51564 KB |
Output is correct |
53 |
Correct |
40 ms |
51820 KB |
Output is correct |
54 |
Correct |
40 ms |
51820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
51180 KB |
Output is correct |
2 |
Correct |
37 ms |
51308 KB |
Output is correct |
3 |
Correct |
37 ms |
51308 KB |
Output is correct |
4 |
Correct |
41 ms |
51180 KB |
Output is correct |
5 |
Correct |
37 ms |
51180 KB |
Output is correct |
6 |
Correct |
38 ms |
51436 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
38 ms |
51308 KB |
Output is correct |
9 |
Correct |
37 ms |
51180 KB |
Output is correct |
10 |
Correct |
41 ms |
51180 KB |
Output is correct |
11 |
Correct |
36 ms |
51308 KB |
Output is correct |
12 |
Correct |
37 ms |
51308 KB |
Output is correct |
13 |
Correct |
37 ms |
51180 KB |
Output is correct |
14 |
Correct |
36 ms |
51308 KB |
Output is correct |
15 |
Correct |
38 ms |
51304 KB |
Output is correct |
16 |
Correct |
42 ms |
51180 KB |
Output is correct |
17 |
Correct |
37 ms |
51308 KB |
Output is correct |
18 |
Correct |
44 ms |
51200 KB |
Output is correct |
19 |
Correct |
37 ms |
51308 KB |
Output is correct |
20 |
Correct |
37 ms |
51308 KB |
Output is correct |
21 |
Correct |
42 ms |
51308 KB |
Output is correct |
22 |
Correct |
37 ms |
51308 KB |
Output is correct |
23 |
Correct |
42 ms |
51308 KB |
Output is correct |
24 |
Correct |
41 ms |
51308 KB |
Output is correct |
25 |
Correct |
37 ms |
51308 KB |
Output is correct |
26 |
Correct |
137 ms |
63204 KB |
Output is correct |
27 |
Correct |
199 ms |
69100 KB |
Output is correct |
28 |
Correct |
40 ms |
51692 KB |
Output is correct |
29 |
Correct |
38 ms |
51308 KB |
Output is correct |
30 |
Correct |
37 ms |
51308 KB |
Output is correct |
31 |
Correct |
161 ms |
63392 KB |
Output is correct |
32 |
Correct |
38 ms |
51564 KB |
Output is correct |
33 |
Correct |
192 ms |
74860 KB |
Output is correct |
34 |
Correct |
188 ms |
67948 KB |
Output is correct |
35 |
Correct |
40 ms |
51564 KB |
Output is correct |
36 |
Correct |
177 ms |
64748 KB |
Output is correct |
37 |
Correct |
39 ms |
51692 KB |
Output is correct |
38 |
Correct |
39 ms |
51564 KB |
Output is correct |
39 |
Correct |
138 ms |
63256 KB |
Output is correct |
40 |
Correct |
40 ms |
51948 KB |
Output is correct |
41 |
Correct |
175 ms |
62988 KB |
Output is correct |
42 |
Correct |
182 ms |
67308 KB |
Output is correct |
43 |
Correct |
38 ms |
51308 KB |
Output is correct |
44 |
Correct |
187 ms |
74988 KB |
Output is correct |
45 |
Correct |
219 ms |
70124 KB |
Output is correct |
46 |
Correct |
39 ms |
51564 KB |
Output is correct |
47 |
Correct |
40 ms |
51820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
61848 KB |
Output is correct |
2 |
Correct |
175 ms |
70268 KB |
Output is correct |
3 |
Correct |
47 ms |
51948 KB |
Output is correct |
4 |
Correct |
39 ms |
51692 KB |
Output is correct |
5 |
Correct |
37 ms |
51308 KB |
Output is correct |
6 |
Correct |
37 ms |
51308 KB |
Output is correct |
7 |
Correct |
39 ms |
51564 KB |
Output is correct |
8 |
Correct |
244 ms |
75768 KB |
Output is correct |
9 |
Correct |
39 ms |
51564 KB |
Output is correct |
10 |
Correct |
162 ms |
63852 KB |
Output is correct |
11 |
Correct |
37 ms |
51308 KB |
Output is correct |
12 |
Correct |
189 ms |
63596 KB |
Output is correct |
13 |
Correct |
242 ms |
74872 KB |
Output is correct |
14 |
Correct |
303 ms |
89192 KB |
Output is correct |
15 |
Correct |
138 ms |
63204 KB |
Output is correct |
16 |
Correct |
42 ms |
51692 KB |
Output is correct |
17 |
Correct |
37 ms |
51308 KB |
Output is correct |
18 |
Correct |
195 ms |
71944 KB |
Output is correct |
19 |
Correct |
231 ms |
79632 KB |
Output is correct |
20 |
Correct |
40 ms |
51820 KB |
Output is correct |
21 |
Correct |
37 ms |
51308 KB |
Output is correct |
22 |
Correct |
170 ms |
66788 KB |
Output is correct |
23 |
Correct |
39 ms |
51692 KB |
Output is correct |
24 |
Correct |
187 ms |
65684 KB |
Output is correct |
25 |
Correct |
243 ms |
77712 KB |
Output is correct |
26 |
Correct |
41 ms |
51820 KB |
Output is correct |
27 |
Correct |
40 ms |
51948 KB |
Output is correct |
28 |
Correct |
39 ms |
51692 KB |
Output is correct |
29 |
Correct |
40 ms |
51692 KB |
Output is correct |
30 |
Correct |
40 ms |
51692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
51180 KB |
Output is correct |
2 |
Correct |
37 ms |
51308 KB |
Output is correct |
3 |
Correct |
37 ms |
51308 KB |
Output is correct |
4 |
Correct |
41 ms |
51180 KB |
Output is correct |
5 |
Correct |
37 ms |
51180 KB |
Output is correct |
6 |
Correct |
38 ms |
51436 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
38 ms |
51308 KB |
Output is correct |
9 |
Correct |
37 ms |
51180 KB |
Output is correct |
10 |
Correct |
41 ms |
51180 KB |
Output is correct |
11 |
Correct |
36 ms |
51308 KB |
Output is correct |
12 |
Correct |
37 ms |
51308 KB |
Output is correct |
13 |
Correct |
37 ms |
51180 KB |
Output is correct |
14 |
Correct |
36 ms |
51308 KB |
Output is correct |
15 |
Correct |
38 ms |
51304 KB |
Output is correct |
16 |
Correct |
42 ms |
51180 KB |
Output is correct |
17 |
Correct |
37 ms |
51308 KB |
Output is correct |
18 |
Correct |
44 ms |
51200 KB |
Output is correct |
19 |
Correct |
37 ms |
51308 KB |
Output is correct |
20 |
Correct |
37 ms |
51308 KB |
Output is correct |
21 |
Correct |
42 ms |
51308 KB |
Output is correct |
22 |
Correct |
37 ms |
51308 KB |
Output is correct |
23 |
Correct |
42 ms |
51308 KB |
Output is correct |
24 |
Correct |
41 ms |
51308 KB |
Output is correct |
25 |
Correct |
37 ms |
51308 KB |
Output is correct |
26 |
Correct |
40 ms |
51948 KB |
Output is correct |
27 |
Correct |
39 ms |
51692 KB |
Output is correct |
28 |
Correct |
40 ms |
52076 KB |
Output is correct |
29 |
Correct |
41 ms |
52204 KB |
Output is correct |
30 |
Correct |
40 ms |
51880 KB |
Output is correct |
31 |
Correct |
36 ms |
51208 KB |
Output is correct |
32 |
Correct |
40 ms |
52076 KB |
Output is correct |
33 |
Correct |
37 ms |
51308 KB |
Output is correct |
34 |
Correct |
39 ms |
51564 KB |
Output is correct |
35 |
Correct |
41 ms |
52204 KB |
Output is correct |
36 |
Correct |
41 ms |
51564 KB |
Output is correct |
37 |
Correct |
41 ms |
51948 KB |
Output is correct |
38 |
Correct |
40 ms |
51452 KB |
Output is correct |
39 |
Correct |
41 ms |
51948 KB |
Output is correct |
40 |
Correct |
39 ms |
51564 KB |
Output is correct |
41 |
Correct |
40 ms |
51564 KB |
Output is correct |
42 |
Correct |
39 ms |
51692 KB |
Output is correct |
43 |
Correct |
39 ms |
51948 KB |
Output is correct |
44 |
Correct |
37 ms |
51308 KB |
Output is correct |
45 |
Correct |
40 ms |
51948 KB |
Output is correct |
46 |
Correct |
41 ms |
51820 KB |
Output is correct |
47 |
Correct |
37 ms |
51308 KB |
Output is correct |
48 |
Correct |
39 ms |
51692 KB |
Output is correct |
49 |
Correct |
40 ms |
51948 KB |
Output is correct |
50 |
Correct |
43 ms |
51948 KB |
Output is correct |
51 |
Correct |
39 ms |
51692 KB |
Output is correct |
52 |
Correct |
40 ms |
51564 KB |
Output is correct |
53 |
Correct |
40 ms |
51820 KB |
Output is correct |
54 |
Correct |
40 ms |
51820 KB |
Output is correct |
55 |
Correct |
37 ms |
51308 KB |
Output is correct |
56 |
Correct |
137 ms |
63204 KB |
Output is correct |
57 |
Correct |
199 ms |
69100 KB |
Output is correct |
58 |
Correct |
40 ms |
51692 KB |
Output is correct |
59 |
Correct |
38 ms |
51308 KB |
Output is correct |
60 |
Correct |
37 ms |
51308 KB |
Output is correct |
61 |
Correct |
161 ms |
63392 KB |
Output is correct |
62 |
Correct |
38 ms |
51564 KB |
Output is correct |
63 |
Correct |
192 ms |
74860 KB |
Output is correct |
64 |
Correct |
188 ms |
67948 KB |
Output is correct |
65 |
Correct |
40 ms |
51564 KB |
Output is correct |
66 |
Correct |
177 ms |
64748 KB |
Output is correct |
67 |
Correct |
39 ms |
51692 KB |
Output is correct |
68 |
Correct |
39 ms |
51564 KB |
Output is correct |
69 |
Correct |
138 ms |
63256 KB |
Output is correct |
70 |
Correct |
40 ms |
51948 KB |
Output is correct |
71 |
Correct |
175 ms |
62988 KB |
Output is correct |
72 |
Correct |
182 ms |
67308 KB |
Output is correct |
73 |
Correct |
38 ms |
51308 KB |
Output is correct |
74 |
Correct |
187 ms |
74988 KB |
Output is correct |
75 |
Correct |
219 ms |
70124 KB |
Output is correct |
76 |
Correct |
39 ms |
51564 KB |
Output is correct |
77 |
Correct |
40 ms |
51820 KB |
Output is correct |
78 |
Correct |
143 ms |
61848 KB |
Output is correct |
79 |
Correct |
175 ms |
70268 KB |
Output is correct |
80 |
Correct |
47 ms |
51948 KB |
Output is correct |
81 |
Correct |
39 ms |
51692 KB |
Output is correct |
82 |
Correct |
37 ms |
51308 KB |
Output is correct |
83 |
Correct |
37 ms |
51308 KB |
Output is correct |
84 |
Correct |
39 ms |
51564 KB |
Output is correct |
85 |
Correct |
244 ms |
75768 KB |
Output is correct |
86 |
Correct |
39 ms |
51564 KB |
Output is correct |
87 |
Correct |
162 ms |
63852 KB |
Output is correct |
88 |
Correct |
37 ms |
51308 KB |
Output is correct |
89 |
Correct |
189 ms |
63596 KB |
Output is correct |
90 |
Correct |
242 ms |
74872 KB |
Output is correct |
91 |
Correct |
303 ms |
89192 KB |
Output is correct |
92 |
Correct |
138 ms |
63204 KB |
Output is correct |
93 |
Correct |
42 ms |
51692 KB |
Output is correct |
94 |
Correct |
37 ms |
51308 KB |
Output is correct |
95 |
Correct |
195 ms |
71944 KB |
Output is correct |
96 |
Correct |
231 ms |
79632 KB |
Output is correct |
97 |
Correct |
40 ms |
51820 KB |
Output is correct |
98 |
Correct |
37 ms |
51308 KB |
Output is correct |
99 |
Correct |
170 ms |
66788 KB |
Output is correct |
100 |
Correct |
39 ms |
51692 KB |
Output is correct |
101 |
Correct |
187 ms |
65684 KB |
Output is correct |
102 |
Correct |
243 ms |
77712 KB |
Output is correct |
103 |
Correct |
41 ms |
51820 KB |
Output is correct |
104 |
Correct |
40 ms |
51948 KB |
Output is correct |
105 |
Correct |
39 ms |
51692 KB |
Output is correct |
106 |
Correct |
40 ms |
51692 KB |
Output is correct |
107 |
Correct |
40 ms |
51692 KB |
Output is correct |
108 |
Correct |
895 ms |
124464 KB |
Output is correct |
109 |
Correct |
969 ms |
141456 KB |
Output is correct |
110 |
Correct |
986 ms |
157420 KB |
Output is correct |
111 |
Correct |
1429 ms |
193884 KB |
Output is correct |
112 |
Correct |
1333 ms |
175512 KB |
Output is correct |
113 |
Correct |
999 ms |
148468 KB |
Output is correct |
114 |
Correct |
924 ms |
110372 KB |
Output is correct |
115 |
Correct |
916 ms |
110276 KB |
Output is correct |
116 |
Correct |
1374 ms |
176200 KB |
Output is correct |
117 |
Correct |
1813 ms |
252128 KB |
Output is correct |
118 |
Correct |
902 ms |
111596 KB |
Output is correct |
119 |
Correct |
1808 ms |
248416 KB |
Output is correct |
120 |
Correct |
1401 ms |
182620 KB |
Output is correct |
121 |
Correct |
1693 ms |
244956 KB |
Output is correct |
122 |
Correct |
1652 ms |
226528 KB |
Output is correct |
123 |
Correct |
858 ms |
153352 KB |
Output is correct |
124 |
Correct |
1470 ms |
199292 KB |
Output is correct |