#include <bits/stdc++.h>
using namespace std;
vector<int> edge[500005], vec[500005];
int dfn[500005], dfnn, C[500005], par[500005], dep[500005], D[500005];
bool A[500005];
struct UF {
int par[500005];
UF() { iota(par, par + 500005, 0); }
int fnd(int x) { return par[x] == x ? x : par[x] = fnd(par[x]); }
} u1;;
void dfs(int x, int p) {
par[x] = p;
dep[x] = dep[p] + 1;
dfn[x] = ++dfnn;
for (auto i : edge[x]) {
if (i == p) continue;
dfs(i, x);
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int N, M; cin >> N >> M;
for (int i = 1; i < N; ++i) {
int x, y; cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
dfs(1, 1);
for (int i = 1; i <= N; ++i) {
cin >> C[i];
vec[C[i]].push_back(i);
}
for (int i = 1; i <= N; ++i) D[i] = edge[i].size();
for (int i = 1; i <= M; ++i) {
sort(vec[i].begin(), vec[i].end(), [&](int a, int b) {
return dfn[a] < dfn[b];
});
for (int j = 0; j + 1 < vec[i].size(); ++j) {
int x = u1.fnd(vec[i][j]), y = u1.fnd(vec[i][j + 1]);
while (x != y) {
int a, b;
if (dep[x] < dep[y]) {
int t = u1.fnd(par[y]);
if (y != t) {
D[y]--;
D[t]--;
u1.par[y] = t;
D[t] += D[y];
}
y = t;
}
else {
int t = u1.fnd(par[x]);
if (x != t) {
D[x]--;
D[t]--;
u1.par[x] = t;
D[t] += D[x];
}
x = t;
}
}
}
}
int ans = 1;
for (int i = 1; i <= N; ++i) {
int t = u1.fnd(i);
if (A[t] == 0 && D[t] == 1) A[t] = 1, ans++;
}
cout << ans / 2;
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:42:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j + 1 < vec[i].size(); ++j) {
~~~~~~^~~~~~~~~~~~~~~
mergers.cpp:45:9: warning: unused variable 'a' [-Wunused-variable]
int a, b;
^
mergers.cpp:45:12: warning: unused variable 'b' [-Wunused-variable]
int a, b;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
25848 KB |
Output is correct |
2 |
Correct |
28 ms |
25848 KB |
Output is correct |
3 |
Correct |
28 ms |
25848 KB |
Output is correct |
4 |
Correct |
31 ms |
25848 KB |
Output is correct |
5 |
Correct |
28 ms |
25848 KB |
Output is correct |
6 |
Correct |
28 ms |
25848 KB |
Output is correct |
7 |
Correct |
28 ms |
25848 KB |
Output is correct |
8 |
Correct |
28 ms |
25852 KB |
Output is correct |
9 |
Correct |
27 ms |
25848 KB |
Output is correct |
10 |
Correct |
29 ms |
25848 KB |
Output is correct |
11 |
Correct |
27 ms |
25848 KB |
Output is correct |
12 |
Correct |
27 ms |
25848 KB |
Output is correct |
13 |
Correct |
27 ms |
25976 KB |
Output is correct |
14 |
Correct |
28 ms |
25848 KB |
Output is correct |
15 |
Correct |
27 ms |
25900 KB |
Output is correct |
16 |
Correct |
27 ms |
25848 KB |
Output is correct |
17 |
Correct |
28 ms |
25976 KB |
Output is correct |
18 |
Correct |
27 ms |
25848 KB |
Output is correct |
19 |
Correct |
27 ms |
25848 KB |
Output is correct |
20 |
Correct |
27 ms |
25848 KB |
Output is correct |
21 |
Correct |
27 ms |
25848 KB |
Output is correct |
22 |
Correct |
28 ms |
25848 KB |
Output is correct |
23 |
Correct |
27 ms |
25848 KB |
Output is correct |
24 |
Correct |
29 ms |
25848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
25848 KB |
Output is correct |
2 |
Correct |
28 ms |
25848 KB |
Output is correct |
3 |
Correct |
28 ms |
25848 KB |
Output is correct |
4 |
Correct |
31 ms |
25848 KB |
Output is correct |
5 |
Correct |
28 ms |
25848 KB |
Output is correct |
6 |
Correct |
28 ms |
25848 KB |
Output is correct |
7 |
Correct |
28 ms |
25848 KB |
Output is correct |
8 |
Correct |
28 ms |
25852 KB |
Output is correct |
9 |
Correct |
27 ms |
25848 KB |
Output is correct |
10 |
Correct |
29 ms |
25848 KB |
Output is correct |
11 |
Correct |
27 ms |
25848 KB |
Output is correct |
12 |
Correct |
27 ms |
25848 KB |
Output is correct |
13 |
Correct |
27 ms |
25976 KB |
Output is correct |
14 |
Correct |
28 ms |
25848 KB |
Output is correct |
15 |
Correct |
27 ms |
25900 KB |
Output is correct |
16 |
Correct |
27 ms |
25848 KB |
Output is correct |
17 |
Correct |
28 ms |
25976 KB |
Output is correct |
18 |
Correct |
27 ms |
25848 KB |
Output is correct |
19 |
Correct |
27 ms |
25848 KB |
Output is correct |
20 |
Correct |
27 ms |
25848 KB |
Output is correct |
21 |
Correct |
27 ms |
25848 KB |
Output is correct |
22 |
Correct |
28 ms |
25848 KB |
Output is correct |
23 |
Correct |
27 ms |
25848 KB |
Output is correct |
24 |
Correct |
29 ms |
25848 KB |
Output is correct |
25 |
Correct |
28 ms |
25976 KB |
Output is correct |
26 |
Correct |
29 ms |
26104 KB |
Output is correct |
27 |
Correct |
30 ms |
25976 KB |
Output is correct |
28 |
Correct |
31 ms |
26284 KB |
Output is correct |
29 |
Correct |
30 ms |
26104 KB |
Output is correct |
30 |
Correct |
43 ms |
26088 KB |
Output is correct |
31 |
Correct |
33 ms |
25848 KB |
Output is correct |
32 |
Correct |
30 ms |
26204 KB |
Output is correct |
33 |
Correct |
27 ms |
25848 KB |
Output is correct |
34 |
Correct |
42 ms |
26060 KB |
Output is correct |
35 |
Correct |
29 ms |
26104 KB |
Output is correct |
36 |
Correct |
29 ms |
26104 KB |
Output is correct |
37 |
Correct |
38 ms |
26104 KB |
Output is correct |
38 |
Correct |
28 ms |
25848 KB |
Output is correct |
39 |
Correct |
30 ms |
26360 KB |
Output is correct |
40 |
Correct |
30 ms |
26104 KB |
Output is correct |
41 |
Correct |
36 ms |
25976 KB |
Output is correct |
42 |
Correct |
29 ms |
26104 KB |
Output is correct |
43 |
Correct |
30 ms |
26360 KB |
Output is correct |
44 |
Correct |
28 ms |
25980 KB |
Output is correct |
45 |
Correct |
30 ms |
26068 KB |
Output is correct |
46 |
Correct |
30 ms |
26104 KB |
Output is correct |
47 |
Correct |
29 ms |
25976 KB |
Output is correct |
48 |
Correct |
29 ms |
25976 KB |
Output is correct |
49 |
Correct |
29 ms |
26232 KB |
Output is correct |
50 |
Correct |
33 ms |
26276 KB |
Output is correct |
51 |
Correct |
30 ms |
25976 KB |
Output is correct |
52 |
Correct |
29 ms |
26104 KB |
Output is correct |
53 |
Correct |
29 ms |
26104 KB |
Output is correct |
54 |
Correct |
30 ms |
26104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
25848 KB |
Output is correct |
2 |
Correct |
28 ms |
25848 KB |
Output is correct |
3 |
Correct |
28 ms |
25848 KB |
Output is correct |
4 |
Correct |
31 ms |
25848 KB |
Output is correct |
5 |
Correct |
28 ms |
25848 KB |
Output is correct |
6 |
Correct |
28 ms |
25848 KB |
Output is correct |
7 |
Correct |
28 ms |
25848 KB |
Output is correct |
8 |
Correct |
28 ms |
25852 KB |
Output is correct |
9 |
Correct |
27 ms |
25848 KB |
Output is correct |
10 |
Correct |
29 ms |
25848 KB |
Output is correct |
11 |
Correct |
27 ms |
25848 KB |
Output is correct |
12 |
Correct |
27 ms |
25848 KB |
Output is correct |
13 |
Correct |
27 ms |
25976 KB |
Output is correct |
14 |
Correct |
28 ms |
25848 KB |
Output is correct |
15 |
Correct |
27 ms |
25900 KB |
Output is correct |
16 |
Correct |
27 ms |
25848 KB |
Output is correct |
17 |
Correct |
28 ms |
25976 KB |
Output is correct |
18 |
Correct |
27 ms |
25848 KB |
Output is correct |
19 |
Correct |
27 ms |
25848 KB |
Output is correct |
20 |
Correct |
27 ms |
25848 KB |
Output is correct |
21 |
Correct |
27 ms |
25848 KB |
Output is correct |
22 |
Correct |
28 ms |
25848 KB |
Output is correct |
23 |
Correct |
27 ms |
25848 KB |
Output is correct |
24 |
Correct |
29 ms |
25848 KB |
Output is correct |
25 |
Correct |
28 ms |
25848 KB |
Output is correct |
26 |
Correct |
100 ms |
33336 KB |
Output is correct |
27 |
Correct |
122 ms |
33144 KB |
Output is correct |
28 |
Correct |
30 ms |
25976 KB |
Output is correct |
29 |
Correct |
28 ms |
25868 KB |
Output is correct |
30 |
Correct |
27 ms |
25848 KB |
Output is correct |
31 |
Correct |
118 ms |
33016 KB |
Output is correct |
32 |
Correct |
29 ms |
26032 KB |
Output is correct |
33 |
Correct |
121 ms |
37240 KB |
Output is correct |
34 |
Correct |
123 ms |
33144 KB |
Output is correct |
35 |
Correct |
29 ms |
25976 KB |
Output is correct |
36 |
Correct |
132 ms |
33400 KB |
Output is correct |
37 |
Correct |
29 ms |
26104 KB |
Output is correct |
38 |
Correct |
30 ms |
25976 KB |
Output is correct |
39 |
Correct |
100 ms |
33292 KB |
Output is correct |
40 |
Correct |
29 ms |
26104 KB |
Output is correct |
41 |
Correct |
115 ms |
33000 KB |
Output is correct |
42 |
Correct |
130 ms |
34356 KB |
Output is correct |
43 |
Correct |
28 ms |
25848 KB |
Output is correct |
44 |
Correct |
142 ms |
37388 KB |
Output is correct |
45 |
Correct |
119 ms |
35192 KB |
Output is correct |
46 |
Correct |
28 ms |
26104 KB |
Output is correct |
47 |
Correct |
30 ms |
26104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
32236 KB |
Output is correct |
2 |
Correct |
111 ms |
34888 KB |
Output is correct |
3 |
Correct |
29 ms |
26104 KB |
Output is correct |
4 |
Correct |
29 ms |
26076 KB |
Output is correct |
5 |
Correct |
27 ms |
25848 KB |
Output is correct |
6 |
Correct |
27 ms |
25848 KB |
Output is correct |
7 |
Correct |
29 ms |
26104 KB |
Output is correct |
8 |
Correct |
130 ms |
34424 KB |
Output is correct |
9 |
Correct |
29 ms |
25976 KB |
Output is correct |
10 |
Correct |
125 ms |
33244 KB |
Output is correct |
11 |
Correct |
27 ms |
25848 KB |
Output is correct |
12 |
Correct |
163 ms |
33252 KB |
Output is correct |
13 |
Correct |
138 ms |
34412 KB |
Output is correct |
14 |
Correct |
132 ms |
36056 KB |
Output is correct |
15 |
Correct |
102 ms |
33264 KB |
Output is correct |
16 |
Correct |
29 ms |
26104 KB |
Output is correct |
17 |
Correct |
27 ms |
25848 KB |
Output is correct |
18 |
Correct |
109 ms |
36060 KB |
Output is correct |
19 |
Correct |
172 ms |
38160 KB |
Output is correct |
20 |
Correct |
29 ms |
26108 KB |
Output is correct |
21 |
Correct |
36 ms |
25852 KB |
Output is correct |
22 |
Correct |
112 ms |
34600 KB |
Output is correct |
23 |
Correct |
34 ms |
26068 KB |
Output is correct |
24 |
Correct |
140 ms |
33528 KB |
Output is correct |
25 |
Correct |
129 ms |
37516 KB |
Output is correct |
26 |
Correct |
29 ms |
26076 KB |
Output is correct |
27 |
Correct |
30 ms |
26188 KB |
Output is correct |
28 |
Correct |
30 ms |
26040 KB |
Output is correct |
29 |
Correct |
35 ms |
26108 KB |
Output is correct |
30 |
Correct |
30 ms |
26004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
25848 KB |
Output is correct |
2 |
Correct |
28 ms |
25848 KB |
Output is correct |
3 |
Correct |
28 ms |
25848 KB |
Output is correct |
4 |
Correct |
31 ms |
25848 KB |
Output is correct |
5 |
Correct |
28 ms |
25848 KB |
Output is correct |
6 |
Correct |
28 ms |
25848 KB |
Output is correct |
7 |
Correct |
28 ms |
25848 KB |
Output is correct |
8 |
Correct |
28 ms |
25852 KB |
Output is correct |
9 |
Correct |
27 ms |
25848 KB |
Output is correct |
10 |
Correct |
29 ms |
25848 KB |
Output is correct |
11 |
Correct |
27 ms |
25848 KB |
Output is correct |
12 |
Correct |
27 ms |
25848 KB |
Output is correct |
13 |
Correct |
27 ms |
25976 KB |
Output is correct |
14 |
Correct |
28 ms |
25848 KB |
Output is correct |
15 |
Correct |
27 ms |
25900 KB |
Output is correct |
16 |
Correct |
27 ms |
25848 KB |
Output is correct |
17 |
Correct |
28 ms |
25976 KB |
Output is correct |
18 |
Correct |
27 ms |
25848 KB |
Output is correct |
19 |
Correct |
27 ms |
25848 KB |
Output is correct |
20 |
Correct |
27 ms |
25848 KB |
Output is correct |
21 |
Correct |
27 ms |
25848 KB |
Output is correct |
22 |
Correct |
28 ms |
25848 KB |
Output is correct |
23 |
Correct |
27 ms |
25848 KB |
Output is correct |
24 |
Correct |
29 ms |
25848 KB |
Output is correct |
25 |
Correct |
28 ms |
25976 KB |
Output is correct |
26 |
Correct |
29 ms |
26104 KB |
Output is correct |
27 |
Correct |
30 ms |
25976 KB |
Output is correct |
28 |
Correct |
31 ms |
26284 KB |
Output is correct |
29 |
Correct |
30 ms |
26104 KB |
Output is correct |
30 |
Correct |
43 ms |
26088 KB |
Output is correct |
31 |
Correct |
33 ms |
25848 KB |
Output is correct |
32 |
Correct |
30 ms |
26204 KB |
Output is correct |
33 |
Correct |
27 ms |
25848 KB |
Output is correct |
34 |
Correct |
42 ms |
26060 KB |
Output is correct |
35 |
Correct |
29 ms |
26104 KB |
Output is correct |
36 |
Correct |
29 ms |
26104 KB |
Output is correct |
37 |
Correct |
38 ms |
26104 KB |
Output is correct |
38 |
Correct |
28 ms |
25848 KB |
Output is correct |
39 |
Correct |
30 ms |
26360 KB |
Output is correct |
40 |
Correct |
30 ms |
26104 KB |
Output is correct |
41 |
Correct |
36 ms |
25976 KB |
Output is correct |
42 |
Correct |
29 ms |
26104 KB |
Output is correct |
43 |
Correct |
30 ms |
26360 KB |
Output is correct |
44 |
Correct |
28 ms |
25980 KB |
Output is correct |
45 |
Correct |
30 ms |
26068 KB |
Output is correct |
46 |
Correct |
30 ms |
26104 KB |
Output is correct |
47 |
Correct |
29 ms |
25976 KB |
Output is correct |
48 |
Correct |
29 ms |
25976 KB |
Output is correct |
49 |
Correct |
29 ms |
26232 KB |
Output is correct |
50 |
Correct |
33 ms |
26276 KB |
Output is correct |
51 |
Correct |
30 ms |
25976 KB |
Output is correct |
52 |
Correct |
29 ms |
26104 KB |
Output is correct |
53 |
Correct |
29 ms |
26104 KB |
Output is correct |
54 |
Correct |
30 ms |
26104 KB |
Output is correct |
55 |
Correct |
28 ms |
25848 KB |
Output is correct |
56 |
Correct |
100 ms |
33336 KB |
Output is correct |
57 |
Correct |
122 ms |
33144 KB |
Output is correct |
58 |
Correct |
30 ms |
25976 KB |
Output is correct |
59 |
Correct |
28 ms |
25868 KB |
Output is correct |
60 |
Correct |
27 ms |
25848 KB |
Output is correct |
61 |
Correct |
118 ms |
33016 KB |
Output is correct |
62 |
Correct |
29 ms |
26032 KB |
Output is correct |
63 |
Correct |
121 ms |
37240 KB |
Output is correct |
64 |
Correct |
123 ms |
33144 KB |
Output is correct |
65 |
Correct |
29 ms |
25976 KB |
Output is correct |
66 |
Correct |
132 ms |
33400 KB |
Output is correct |
67 |
Correct |
29 ms |
26104 KB |
Output is correct |
68 |
Correct |
30 ms |
25976 KB |
Output is correct |
69 |
Correct |
100 ms |
33292 KB |
Output is correct |
70 |
Correct |
29 ms |
26104 KB |
Output is correct |
71 |
Correct |
115 ms |
33000 KB |
Output is correct |
72 |
Correct |
130 ms |
34356 KB |
Output is correct |
73 |
Correct |
28 ms |
25848 KB |
Output is correct |
74 |
Correct |
142 ms |
37388 KB |
Output is correct |
75 |
Correct |
119 ms |
35192 KB |
Output is correct |
76 |
Correct |
28 ms |
26104 KB |
Output is correct |
77 |
Correct |
30 ms |
26104 KB |
Output is correct |
78 |
Correct |
102 ms |
32236 KB |
Output is correct |
79 |
Correct |
111 ms |
34888 KB |
Output is correct |
80 |
Correct |
29 ms |
26104 KB |
Output is correct |
81 |
Correct |
29 ms |
26076 KB |
Output is correct |
82 |
Correct |
27 ms |
25848 KB |
Output is correct |
83 |
Correct |
27 ms |
25848 KB |
Output is correct |
84 |
Correct |
29 ms |
26104 KB |
Output is correct |
85 |
Correct |
130 ms |
34424 KB |
Output is correct |
86 |
Correct |
29 ms |
25976 KB |
Output is correct |
87 |
Correct |
125 ms |
33244 KB |
Output is correct |
88 |
Correct |
27 ms |
25848 KB |
Output is correct |
89 |
Correct |
163 ms |
33252 KB |
Output is correct |
90 |
Correct |
138 ms |
34412 KB |
Output is correct |
91 |
Correct |
132 ms |
36056 KB |
Output is correct |
92 |
Correct |
102 ms |
33264 KB |
Output is correct |
93 |
Correct |
29 ms |
26104 KB |
Output is correct |
94 |
Correct |
27 ms |
25848 KB |
Output is correct |
95 |
Correct |
109 ms |
36060 KB |
Output is correct |
96 |
Correct |
172 ms |
38160 KB |
Output is correct |
97 |
Correct |
29 ms |
26108 KB |
Output is correct |
98 |
Correct |
36 ms |
25852 KB |
Output is correct |
99 |
Correct |
112 ms |
34600 KB |
Output is correct |
100 |
Correct |
34 ms |
26068 KB |
Output is correct |
101 |
Correct |
140 ms |
33528 KB |
Output is correct |
102 |
Correct |
129 ms |
37516 KB |
Output is correct |
103 |
Correct |
29 ms |
26076 KB |
Output is correct |
104 |
Correct |
30 ms |
26188 KB |
Output is correct |
105 |
Correct |
30 ms |
26040 KB |
Output is correct |
106 |
Correct |
35 ms |
26108 KB |
Output is correct |
107 |
Correct |
30 ms |
26004 KB |
Output is correct |
108 |
Correct |
700 ms |
70072 KB |
Output is correct |
109 |
Correct |
852 ms |
72148 KB |
Output is correct |
110 |
Correct |
819 ms |
78840 KB |
Output is correct |
111 |
Correct |
899 ms |
91044 KB |
Output is correct |
112 |
Correct |
976 ms |
85744 KB |
Output is correct |
113 |
Correct |
601 ms |
77900 KB |
Output is correct |
114 |
Correct |
733 ms |
61704 KB |
Output is correct |
115 |
Correct |
765 ms |
61672 KB |
Output is correct |
116 |
Correct |
905 ms |
66808 KB |
Output is correct |
117 |
Correct |
780 ms |
78024 KB |
Output is correct |
118 |
Correct |
722 ms |
63352 KB |
Output is correct |
119 |
Correct |
813 ms |
77944 KB |
Output is correct |
120 |
Correct |
807 ms |
86080 KB |
Output is correct |
121 |
Correct |
785 ms |
78080 KB |
Output is correct |
122 |
Correct |
876 ms |
71672 KB |
Output is correct |
123 |
Correct |
724 ms |
79532 KB |
Output is correct |
124 |
Correct |
865 ms |
76620 KB |
Output is correct |