#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
ll N, K, A[500005], D[500005], P[500005];
vector<ll> T[500005];
struct UnionFind{
ll p[500005];
ll Find(ll n) { return (p[n] < 0 ? n : p[n] = Find(p[n])); }
void Union(ll u, ll v) { u = Find(u), v = Find(v); if(u != v) p[u] += p[v], p[v] = u; }
}uf;
void dfs(ll u, ll p){
P[u] = p;
for(auto v : T[u]){
if(v == p) continue;
D[v] = D[u] + 1;
dfs(v, u);
}
}
void Comp(ll u, ll v){
u = uf.Find(u), v = uf.Find(v);
while(u != v){
if(D[u] < D[v]) swap(u, v);
uf.Union(P[u], u);
u = uf.Find(u);
}
}
vector<ll> S[500005];
ll deg[500005];
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> N >> K;
fill(uf.p, uf.p + N + 1, -1LL);
for(ll i = 1; i < N; i++){
ll u, v; cin >> u >> v;
T[u].push_back(v); T[v].push_back(u);
}
dfs(1, -1);
for(ll i = 1; i <= N; i++){
ll v; cin >> v;
S[v].push_back(i);
}
for(ll i = 1; i <= K; i++){
for(ll j = 1; j < S[i].size(); j++){
Comp(S[i][0], S[i][j]);
}
}
for(ll i = 1; i <= N; i++){
for(auto j : T[i]){
if(i > j) continue;
ll u = uf.Find(i), v = uf.Find(j);
if(u != v){
deg[u]++, deg[v]++;
}
}
}
ll Cnt = 0;
for(ll i = 1; i <= N; i++){
if(deg[i] == 1) Cnt++;
}
cout << (Cnt + 1) / 2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:52:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(ll j = 1; j < S[i].size(); j++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23948 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
9 ms |
23896 KB |
Output is correct |
10 |
Correct |
9 ms |
23764 KB |
Output is correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23984 KB |
Output is correct |
13 |
Correct |
11 ms |
23964 KB |
Output is correct |
14 |
Correct |
10 ms |
23896 KB |
Output is correct |
15 |
Correct |
10 ms |
23900 KB |
Output is correct |
16 |
Correct |
10 ms |
23816 KB |
Output is correct |
17 |
Correct |
10 ms |
23900 KB |
Output is correct |
18 |
Correct |
12 ms |
23900 KB |
Output is correct |
19 |
Correct |
11 ms |
23896 KB |
Output is correct |
20 |
Correct |
11 ms |
23836 KB |
Output is correct |
21 |
Correct |
11 ms |
23984 KB |
Output is correct |
22 |
Correct |
10 ms |
23900 KB |
Output is correct |
23 |
Correct |
10 ms |
23976 KB |
Output is correct |
24 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23948 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
9 ms |
23896 KB |
Output is correct |
10 |
Correct |
9 ms |
23764 KB |
Output is correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23984 KB |
Output is correct |
13 |
Correct |
11 ms |
23964 KB |
Output is correct |
14 |
Correct |
10 ms |
23896 KB |
Output is correct |
15 |
Correct |
10 ms |
23900 KB |
Output is correct |
16 |
Correct |
10 ms |
23816 KB |
Output is correct |
17 |
Correct |
10 ms |
23900 KB |
Output is correct |
18 |
Correct |
12 ms |
23900 KB |
Output is correct |
19 |
Correct |
11 ms |
23896 KB |
Output is correct |
20 |
Correct |
11 ms |
23836 KB |
Output is correct |
21 |
Correct |
11 ms |
23984 KB |
Output is correct |
22 |
Correct |
10 ms |
23900 KB |
Output is correct |
23 |
Correct |
10 ms |
23976 KB |
Output is correct |
24 |
Correct |
11 ms |
23900 KB |
Output is correct |
25 |
Correct |
11 ms |
24152 KB |
Output is correct |
26 |
Correct |
11 ms |
24156 KB |
Output is correct |
27 |
Correct |
11 ms |
24156 KB |
Output is correct |
28 |
Correct |
11 ms |
24156 KB |
Output is correct |
29 |
Correct |
11 ms |
24152 KB |
Output is correct |
30 |
Correct |
11 ms |
24200 KB |
Output is correct |
31 |
Correct |
11 ms |
23900 KB |
Output is correct |
32 |
Correct |
12 ms |
24344 KB |
Output is correct |
33 |
Correct |
11 ms |
23896 KB |
Output is correct |
34 |
Correct |
12 ms |
24156 KB |
Output is correct |
35 |
Correct |
10 ms |
24152 KB |
Output is correct |
36 |
Correct |
12 ms |
24196 KB |
Output is correct |
37 |
Correct |
12 ms |
24156 KB |
Output is correct |
38 |
Correct |
10 ms |
23948 KB |
Output is correct |
39 |
Correct |
12 ms |
24264 KB |
Output is correct |
40 |
Correct |
12 ms |
24012 KB |
Output is correct |
41 |
Correct |
11 ms |
24156 KB |
Output is correct |
42 |
Correct |
11 ms |
24152 KB |
Output is correct |
43 |
Correct |
15 ms |
24156 KB |
Output is correct |
44 |
Correct |
10 ms |
23896 KB |
Output is correct |
45 |
Correct |
13 ms |
24156 KB |
Output is correct |
46 |
Correct |
12 ms |
24156 KB |
Output is correct |
47 |
Correct |
10 ms |
23732 KB |
Output is correct |
48 |
Correct |
10 ms |
24228 KB |
Output is correct |
49 |
Correct |
11 ms |
24156 KB |
Output is correct |
50 |
Correct |
10 ms |
24156 KB |
Output is correct |
51 |
Correct |
11 ms |
24156 KB |
Output is correct |
52 |
Correct |
10 ms |
24248 KB |
Output is correct |
53 |
Correct |
10 ms |
24156 KB |
Output is correct |
54 |
Correct |
11 ms |
24156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23948 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
9 ms |
23896 KB |
Output is correct |
10 |
Correct |
9 ms |
23764 KB |
Output is correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23984 KB |
Output is correct |
13 |
Correct |
11 ms |
23964 KB |
Output is correct |
14 |
Correct |
10 ms |
23896 KB |
Output is correct |
15 |
Correct |
10 ms |
23900 KB |
Output is correct |
16 |
Correct |
10 ms |
23816 KB |
Output is correct |
17 |
Correct |
10 ms |
23900 KB |
Output is correct |
18 |
Correct |
12 ms |
23900 KB |
Output is correct |
19 |
Correct |
11 ms |
23896 KB |
Output is correct |
20 |
Correct |
11 ms |
23836 KB |
Output is correct |
21 |
Correct |
11 ms |
23984 KB |
Output is correct |
22 |
Correct |
10 ms |
23900 KB |
Output is correct |
23 |
Correct |
10 ms |
23976 KB |
Output is correct |
24 |
Correct |
11 ms |
23900 KB |
Output is correct |
25 |
Correct |
9 ms |
23900 KB |
Output is correct |
26 |
Correct |
36 ms |
32644 KB |
Output is correct |
27 |
Correct |
58 ms |
32408 KB |
Output is correct |
28 |
Correct |
11 ms |
24156 KB |
Output is correct |
29 |
Correct |
10 ms |
23900 KB |
Output is correct |
30 |
Correct |
9 ms |
23992 KB |
Output is correct |
31 |
Correct |
46 ms |
32476 KB |
Output is correct |
32 |
Correct |
11 ms |
24156 KB |
Output is correct |
33 |
Correct |
48 ms |
36096 KB |
Output is correct |
34 |
Correct |
44 ms |
32340 KB |
Output is correct |
35 |
Correct |
11 ms |
24188 KB |
Output is correct |
36 |
Correct |
45 ms |
32460 KB |
Output is correct |
37 |
Correct |
11 ms |
24156 KB |
Output is correct |
38 |
Correct |
10 ms |
24156 KB |
Output is correct |
39 |
Correct |
39 ms |
32576 KB |
Output is correct |
40 |
Correct |
11 ms |
24156 KB |
Output is correct |
41 |
Correct |
47 ms |
32532 KB |
Output is correct |
42 |
Correct |
51 ms |
33280 KB |
Output is correct |
43 |
Correct |
10 ms |
23896 KB |
Output is correct |
44 |
Correct |
46 ms |
36300 KB |
Output is correct |
45 |
Correct |
47 ms |
35264 KB |
Output is correct |
46 |
Correct |
11 ms |
24156 KB |
Output is correct |
47 |
Correct |
11 ms |
24116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
32640 KB |
Output is correct |
2 |
Correct |
48 ms |
35788 KB |
Output is correct |
3 |
Correct |
12 ms |
24156 KB |
Output is correct |
4 |
Correct |
12 ms |
24152 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23756 KB |
Output is correct |
7 |
Correct |
11 ms |
24156 KB |
Output is correct |
8 |
Correct |
59 ms |
34236 KB |
Output is correct |
9 |
Correct |
12 ms |
24156 KB |
Output is correct |
10 |
Correct |
54 ms |
33448 KB |
Output is correct |
11 |
Correct |
11 ms |
23896 KB |
Output is correct |
12 |
Correct |
50 ms |
32932 KB |
Output is correct |
13 |
Correct |
56 ms |
34128 KB |
Output is correct |
14 |
Correct |
57 ms |
35676 KB |
Output is correct |
15 |
Correct |
39 ms |
32576 KB |
Output is correct |
16 |
Correct |
10 ms |
24192 KB |
Output is correct |
17 |
Correct |
10 ms |
23760 KB |
Output is correct |
18 |
Correct |
42 ms |
35456 KB |
Output is correct |
19 |
Correct |
50 ms |
37480 KB |
Output is correct |
20 |
Correct |
11 ms |
24156 KB |
Output is correct |
21 |
Correct |
11 ms |
23776 KB |
Output is correct |
22 |
Correct |
48 ms |
34352 KB |
Output is correct |
23 |
Correct |
11 ms |
24252 KB |
Output is correct |
24 |
Correct |
54 ms |
33600 KB |
Output is correct |
25 |
Correct |
59 ms |
37484 KB |
Output is correct |
26 |
Correct |
11 ms |
24108 KB |
Output is correct |
27 |
Correct |
11 ms |
24116 KB |
Output is correct |
28 |
Correct |
11 ms |
24220 KB |
Output is correct |
29 |
Correct |
11 ms |
24168 KB |
Output is correct |
30 |
Correct |
11 ms |
24156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23948 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
9 ms |
23896 KB |
Output is correct |
10 |
Correct |
9 ms |
23764 KB |
Output is correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23984 KB |
Output is correct |
13 |
Correct |
11 ms |
23964 KB |
Output is correct |
14 |
Correct |
10 ms |
23896 KB |
Output is correct |
15 |
Correct |
10 ms |
23900 KB |
Output is correct |
16 |
Correct |
10 ms |
23816 KB |
Output is correct |
17 |
Correct |
10 ms |
23900 KB |
Output is correct |
18 |
Correct |
12 ms |
23900 KB |
Output is correct |
19 |
Correct |
11 ms |
23896 KB |
Output is correct |
20 |
Correct |
11 ms |
23836 KB |
Output is correct |
21 |
Correct |
11 ms |
23984 KB |
Output is correct |
22 |
Correct |
10 ms |
23900 KB |
Output is correct |
23 |
Correct |
10 ms |
23976 KB |
Output is correct |
24 |
Correct |
11 ms |
23900 KB |
Output is correct |
25 |
Correct |
11 ms |
24152 KB |
Output is correct |
26 |
Correct |
11 ms |
24156 KB |
Output is correct |
27 |
Correct |
11 ms |
24156 KB |
Output is correct |
28 |
Correct |
11 ms |
24156 KB |
Output is correct |
29 |
Correct |
11 ms |
24152 KB |
Output is correct |
30 |
Correct |
11 ms |
24200 KB |
Output is correct |
31 |
Correct |
11 ms |
23900 KB |
Output is correct |
32 |
Correct |
12 ms |
24344 KB |
Output is correct |
33 |
Correct |
11 ms |
23896 KB |
Output is correct |
34 |
Correct |
12 ms |
24156 KB |
Output is correct |
35 |
Correct |
10 ms |
24152 KB |
Output is correct |
36 |
Correct |
12 ms |
24196 KB |
Output is correct |
37 |
Correct |
12 ms |
24156 KB |
Output is correct |
38 |
Correct |
10 ms |
23948 KB |
Output is correct |
39 |
Correct |
12 ms |
24264 KB |
Output is correct |
40 |
Correct |
12 ms |
24012 KB |
Output is correct |
41 |
Correct |
11 ms |
24156 KB |
Output is correct |
42 |
Correct |
11 ms |
24152 KB |
Output is correct |
43 |
Correct |
15 ms |
24156 KB |
Output is correct |
44 |
Correct |
10 ms |
23896 KB |
Output is correct |
45 |
Correct |
13 ms |
24156 KB |
Output is correct |
46 |
Correct |
12 ms |
24156 KB |
Output is correct |
47 |
Correct |
10 ms |
23732 KB |
Output is correct |
48 |
Correct |
10 ms |
24228 KB |
Output is correct |
49 |
Correct |
11 ms |
24156 KB |
Output is correct |
50 |
Correct |
10 ms |
24156 KB |
Output is correct |
51 |
Correct |
11 ms |
24156 KB |
Output is correct |
52 |
Correct |
10 ms |
24248 KB |
Output is correct |
53 |
Correct |
10 ms |
24156 KB |
Output is correct |
54 |
Correct |
11 ms |
24156 KB |
Output is correct |
55 |
Correct |
9 ms |
23900 KB |
Output is correct |
56 |
Correct |
36 ms |
32644 KB |
Output is correct |
57 |
Correct |
58 ms |
32408 KB |
Output is correct |
58 |
Correct |
11 ms |
24156 KB |
Output is correct |
59 |
Correct |
10 ms |
23900 KB |
Output is correct |
60 |
Correct |
9 ms |
23992 KB |
Output is correct |
61 |
Correct |
46 ms |
32476 KB |
Output is correct |
62 |
Correct |
11 ms |
24156 KB |
Output is correct |
63 |
Correct |
48 ms |
36096 KB |
Output is correct |
64 |
Correct |
44 ms |
32340 KB |
Output is correct |
65 |
Correct |
11 ms |
24188 KB |
Output is correct |
66 |
Correct |
45 ms |
32460 KB |
Output is correct |
67 |
Correct |
11 ms |
24156 KB |
Output is correct |
68 |
Correct |
10 ms |
24156 KB |
Output is correct |
69 |
Correct |
39 ms |
32576 KB |
Output is correct |
70 |
Correct |
11 ms |
24156 KB |
Output is correct |
71 |
Correct |
47 ms |
32532 KB |
Output is correct |
72 |
Correct |
51 ms |
33280 KB |
Output is correct |
73 |
Correct |
10 ms |
23896 KB |
Output is correct |
74 |
Correct |
46 ms |
36300 KB |
Output is correct |
75 |
Correct |
47 ms |
35264 KB |
Output is correct |
76 |
Correct |
11 ms |
24156 KB |
Output is correct |
77 |
Correct |
11 ms |
24116 KB |
Output is correct |
78 |
Correct |
46 ms |
32640 KB |
Output is correct |
79 |
Correct |
48 ms |
35788 KB |
Output is correct |
80 |
Correct |
12 ms |
24156 KB |
Output is correct |
81 |
Correct |
12 ms |
24152 KB |
Output is correct |
82 |
Correct |
11 ms |
23900 KB |
Output is correct |
83 |
Correct |
10 ms |
23756 KB |
Output is correct |
84 |
Correct |
11 ms |
24156 KB |
Output is correct |
85 |
Correct |
59 ms |
34236 KB |
Output is correct |
86 |
Correct |
12 ms |
24156 KB |
Output is correct |
87 |
Correct |
54 ms |
33448 KB |
Output is correct |
88 |
Correct |
11 ms |
23896 KB |
Output is correct |
89 |
Correct |
50 ms |
32932 KB |
Output is correct |
90 |
Correct |
56 ms |
34128 KB |
Output is correct |
91 |
Correct |
57 ms |
35676 KB |
Output is correct |
92 |
Correct |
39 ms |
32576 KB |
Output is correct |
93 |
Correct |
10 ms |
24192 KB |
Output is correct |
94 |
Correct |
10 ms |
23760 KB |
Output is correct |
95 |
Correct |
42 ms |
35456 KB |
Output is correct |
96 |
Correct |
50 ms |
37480 KB |
Output is correct |
97 |
Correct |
11 ms |
24156 KB |
Output is correct |
98 |
Correct |
11 ms |
23776 KB |
Output is correct |
99 |
Correct |
48 ms |
34352 KB |
Output is correct |
100 |
Correct |
11 ms |
24252 KB |
Output is correct |
101 |
Correct |
54 ms |
33600 KB |
Output is correct |
102 |
Correct |
59 ms |
37484 KB |
Output is correct |
103 |
Correct |
11 ms |
24108 KB |
Output is correct |
104 |
Correct |
11 ms |
24116 KB |
Output is correct |
105 |
Correct |
11 ms |
24220 KB |
Output is correct |
106 |
Correct |
11 ms |
24168 KB |
Output is correct |
107 |
Correct |
11 ms |
24156 KB |
Output is correct |
108 |
Correct |
268 ms |
77572 KB |
Output is correct |
109 |
Correct |
310 ms |
74088 KB |
Output is correct |
110 |
Correct |
312 ms |
80792 KB |
Output is correct |
111 |
Correct |
349 ms |
94952 KB |
Output is correct |
112 |
Correct |
363 ms |
93392 KB |
Output is correct |
113 |
Correct |
250 ms |
83244 KB |
Output is correct |
114 |
Correct |
258 ms |
65892 KB |
Output is correct |
115 |
Correct |
287 ms |
66192 KB |
Output is correct |
116 |
Correct |
320 ms |
73808 KB |
Output is correct |
117 |
Correct |
356 ms |
83704 KB |
Output is correct |
118 |
Correct |
284 ms |
70288 KB |
Output is correct |
119 |
Correct |
316 ms |
83904 KB |
Output is correct |
120 |
Correct |
345 ms |
93508 KB |
Output is correct |
121 |
Correct |
317 ms |
83796 KB |
Output is correct |
122 |
Correct |
342 ms |
78164 KB |
Output is correct |
123 |
Correct |
242 ms |
84916 KB |
Output is correct |
124 |
Correct |
372 ms |
79952 KB |
Output is correct |