#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 5e5+5;
int ans;
int leaves;
vector<int> col[MAXN];
vector<pair<int,int>> edges;
int depth[MAXN];
int dsu[MAXN];
int par[MAXN];
int cnt[MAXN];
vector<int> v1[MAXN];
int find(int curr){
if(dsu[curr] == curr){
return curr;
}
return dsu[curr] = find(dsu[curr]);
}
void dfs(int curr,int p,int d){
par[curr] = p;
dsu[curr] = curr;
depth[curr] = d;
for(int x:v1[curr]){
if(x!=p){
dfs(x,curr,d+1);
}
}
}
int main() {
int n,m;
cin>>n>>m;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
v1[x].push_back(y);
v1[y].push_back(x);
edges.push_back(make_pair(x,y));
}
for(int i=1;i<=n;i++){
int c;
cin>>c;
col[c].push_back(i);
}
dfs(1,1,1);
for(int i=1;i<=m;i++){
for(int j=1;j<col[i].size();j++){
int a = find(col[i][j-1]);
int b = find(col[i][j]);
while(a!=b){
if(depth[a]<depth[b]){
swap(a,b);
}
dsu[a] = find(par[a]);
a = dsu[a];
//cout<<find(2)<<endl;
}
}
}
for(auto e:edges){
int a = find(e.first);
int b = find(e.second);
if(a!=b){
cnt[a]++;
cnt[b]++;
}
}
for(int i=1;i<=n;i++){
if(cnt[i] == 1){
leaves++;
}
}
if(leaves == 1){
cout<<0<<endl;
return 0;
}
cout<<(leaves+1)/2<<endl;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:49:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=1;j<col[i].size();j++){
~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
25 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
25 ms |
23800 KB |
Output is correct |
6 |
Correct |
26 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
26 ms |
23804 KB |
Output is correct |
9 |
Correct |
26 ms |
23828 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
25 ms |
23800 KB |
Output is correct |
12 |
Correct |
25 ms |
23800 KB |
Output is correct |
13 |
Correct |
24 ms |
23800 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
25 ms |
23800 KB |
Output is correct |
16 |
Correct |
26 ms |
23928 KB |
Output is correct |
17 |
Correct |
24 ms |
23800 KB |
Output is correct |
18 |
Correct |
25 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
26 ms |
23800 KB |
Output is correct |
21 |
Correct |
25 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
26 ms |
23772 KB |
Output is correct |
24 |
Correct |
25 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
25 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
25 ms |
23800 KB |
Output is correct |
6 |
Correct |
26 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
26 ms |
23804 KB |
Output is correct |
9 |
Correct |
26 ms |
23828 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
25 ms |
23800 KB |
Output is correct |
12 |
Correct |
25 ms |
23800 KB |
Output is correct |
13 |
Correct |
24 ms |
23800 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
25 ms |
23800 KB |
Output is correct |
16 |
Correct |
26 ms |
23928 KB |
Output is correct |
17 |
Correct |
24 ms |
23800 KB |
Output is correct |
18 |
Correct |
25 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
26 ms |
23800 KB |
Output is correct |
21 |
Correct |
25 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
26 ms |
23772 KB |
Output is correct |
24 |
Correct |
25 ms |
23928 KB |
Output is correct |
25 |
Correct |
25 ms |
23928 KB |
Output is correct |
26 |
Correct |
30 ms |
24184 KB |
Output is correct |
27 |
Correct |
29 ms |
24028 KB |
Output is correct |
28 |
Correct |
32 ms |
24184 KB |
Output is correct |
29 |
Correct |
30 ms |
24184 KB |
Output is correct |
30 |
Correct |
29 ms |
24028 KB |
Output is correct |
31 |
Correct |
27 ms |
23844 KB |
Output is correct |
32 |
Correct |
31 ms |
24284 KB |
Output is correct |
33 |
Correct |
26 ms |
23800 KB |
Output is correct |
34 |
Correct |
32 ms |
24056 KB |
Output is correct |
35 |
Correct |
31 ms |
24068 KB |
Output is correct |
36 |
Correct |
30 ms |
24084 KB |
Output is correct |
37 |
Correct |
31 ms |
24056 KB |
Output is correct |
38 |
Correct |
25 ms |
23928 KB |
Output is correct |
39 |
Correct |
29 ms |
24056 KB |
Output is correct |
40 |
Correct |
30 ms |
24056 KB |
Output is correct |
41 |
Correct |
31 ms |
24056 KB |
Output is correct |
42 |
Correct |
29 ms |
24184 KB |
Output is correct |
43 |
Correct |
29 ms |
24184 KB |
Output is correct |
44 |
Correct |
24 ms |
23800 KB |
Output is correct |
45 |
Correct |
29 ms |
24188 KB |
Output is correct |
46 |
Correct |
30 ms |
24060 KB |
Output is correct |
47 |
Correct |
25 ms |
23772 KB |
Output is correct |
48 |
Correct |
29 ms |
24104 KB |
Output is correct |
49 |
Correct |
29 ms |
24212 KB |
Output is correct |
50 |
Correct |
32 ms |
24184 KB |
Output is correct |
51 |
Correct |
30 ms |
24056 KB |
Output is correct |
52 |
Correct |
30 ms |
24072 KB |
Output is correct |
53 |
Correct |
29 ms |
24056 KB |
Output is correct |
54 |
Correct |
28 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
25 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
25 ms |
23800 KB |
Output is correct |
6 |
Correct |
26 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
26 ms |
23804 KB |
Output is correct |
9 |
Correct |
26 ms |
23828 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
25 ms |
23800 KB |
Output is correct |
12 |
Correct |
25 ms |
23800 KB |
Output is correct |
13 |
Correct |
24 ms |
23800 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
25 ms |
23800 KB |
Output is correct |
16 |
Correct |
26 ms |
23928 KB |
Output is correct |
17 |
Correct |
24 ms |
23800 KB |
Output is correct |
18 |
Correct |
25 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
26 ms |
23800 KB |
Output is correct |
21 |
Correct |
25 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
26 ms |
23772 KB |
Output is correct |
24 |
Correct |
25 ms |
23928 KB |
Output is correct |
25 |
Correct |
25 ms |
23800 KB |
Output is correct |
26 |
Correct |
212 ms |
31532 KB |
Output is correct |
27 |
Correct |
235 ms |
31180 KB |
Output is correct |
28 |
Correct |
29 ms |
24056 KB |
Output is correct |
29 |
Correct |
25 ms |
23800 KB |
Output is correct |
30 |
Correct |
25 ms |
23800 KB |
Output is correct |
31 |
Correct |
225 ms |
31180 KB |
Output is correct |
32 |
Correct |
29 ms |
24056 KB |
Output is correct |
33 |
Correct |
253 ms |
35444 KB |
Output is correct |
34 |
Correct |
228 ms |
31084 KB |
Output is correct |
35 |
Correct |
30 ms |
24060 KB |
Output is correct |
36 |
Correct |
252 ms |
31664 KB |
Output is correct |
37 |
Correct |
29 ms |
24056 KB |
Output is correct |
38 |
Correct |
30 ms |
24056 KB |
Output is correct |
39 |
Correct |
206 ms |
31336 KB |
Output is correct |
40 |
Correct |
28 ms |
24188 KB |
Output is correct |
41 |
Correct |
224 ms |
31340 KB |
Output is correct |
42 |
Correct |
254 ms |
32608 KB |
Output is correct |
43 |
Correct |
27 ms |
23800 KB |
Output is correct |
44 |
Correct |
238 ms |
35820 KB |
Output is correct |
45 |
Correct |
253 ms |
33388 KB |
Output is correct |
46 |
Correct |
29 ms |
24056 KB |
Output is correct |
47 |
Correct |
28 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
31332 KB |
Output is correct |
2 |
Correct |
248 ms |
34720 KB |
Output is correct |
3 |
Correct |
31 ms |
24060 KB |
Output is correct |
4 |
Correct |
29 ms |
24084 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
25 ms |
23800 KB |
Output is correct |
7 |
Correct |
31 ms |
24056 KB |
Output is correct |
8 |
Correct |
263 ms |
32816 KB |
Output is correct |
9 |
Correct |
28 ms |
24056 KB |
Output is correct |
10 |
Correct |
249 ms |
31652 KB |
Output is correct |
11 |
Correct |
24 ms |
23800 KB |
Output is correct |
12 |
Correct |
244 ms |
31468 KB |
Output is correct |
13 |
Correct |
270 ms |
32756 KB |
Output is correct |
14 |
Correct |
256 ms |
34284 KB |
Output is correct |
15 |
Correct |
201 ms |
31268 KB |
Output is correct |
16 |
Correct |
30 ms |
24056 KB |
Output is correct |
17 |
Correct |
25 ms |
23800 KB |
Output is correct |
18 |
Correct |
236 ms |
34324 KB |
Output is correct |
19 |
Correct |
279 ms |
36828 KB |
Output is correct |
20 |
Correct |
31 ms |
24056 KB |
Output is correct |
21 |
Correct |
24 ms |
23828 KB |
Output is correct |
22 |
Correct |
231 ms |
32980 KB |
Output is correct |
23 |
Correct |
30 ms |
24056 KB |
Output is correct |
24 |
Correct |
254 ms |
31848 KB |
Output is correct |
25 |
Correct |
264 ms |
35832 KB |
Output is correct |
26 |
Correct |
30 ms |
24184 KB |
Output is correct |
27 |
Correct |
29 ms |
24184 KB |
Output is correct |
28 |
Correct |
29 ms |
24056 KB |
Output is correct |
29 |
Correct |
30 ms |
24084 KB |
Output is correct |
30 |
Correct |
30 ms |
24084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
25 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
25 ms |
23800 KB |
Output is correct |
6 |
Correct |
26 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
26 ms |
23804 KB |
Output is correct |
9 |
Correct |
26 ms |
23828 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
25 ms |
23800 KB |
Output is correct |
12 |
Correct |
25 ms |
23800 KB |
Output is correct |
13 |
Correct |
24 ms |
23800 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
25 ms |
23800 KB |
Output is correct |
16 |
Correct |
26 ms |
23928 KB |
Output is correct |
17 |
Correct |
24 ms |
23800 KB |
Output is correct |
18 |
Correct |
25 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
26 ms |
23800 KB |
Output is correct |
21 |
Correct |
25 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
26 ms |
23772 KB |
Output is correct |
24 |
Correct |
25 ms |
23928 KB |
Output is correct |
25 |
Correct |
25 ms |
23928 KB |
Output is correct |
26 |
Correct |
30 ms |
24184 KB |
Output is correct |
27 |
Correct |
29 ms |
24028 KB |
Output is correct |
28 |
Correct |
32 ms |
24184 KB |
Output is correct |
29 |
Correct |
30 ms |
24184 KB |
Output is correct |
30 |
Correct |
29 ms |
24028 KB |
Output is correct |
31 |
Correct |
27 ms |
23844 KB |
Output is correct |
32 |
Correct |
31 ms |
24284 KB |
Output is correct |
33 |
Correct |
26 ms |
23800 KB |
Output is correct |
34 |
Correct |
32 ms |
24056 KB |
Output is correct |
35 |
Correct |
31 ms |
24068 KB |
Output is correct |
36 |
Correct |
30 ms |
24084 KB |
Output is correct |
37 |
Correct |
31 ms |
24056 KB |
Output is correct |
38 |
Correct |
25 ms |
23928 KB |
Output is correct |
39 |
Correct |
29 ms |
24056 KB |
Output is correct |
40 |
Correct |
30 ms |
24056 KB |
Output is correct |
41 |
Correct |
31 ms |
24056 KB |
Output is correct |
42 |
Correct |
29 ms |
24184 KB |
Output is correct |
43 |
Correct |
29 ms |
24184 KB |
Output is correct |
44 |
Correct |
24 ms |
23800 KB |
Output is correct |
45 |
Correct |
29 ms |
24188 KB |
Output is correct |
46 |
Correct |
30 ms |
24060 KB |
Output is correct |
47 |
Correct |
25 ms |
23772 KB |
Output is correct |
48 |
Correct |
29 ms |
24104 KB |
Output is correct |
49 |
Correct |
29 ms |
24212 KB |
Output is correct |
50 |
Correct |
32 ms |
24184 KB |
Output is correct |
51 |
Correct |
30 ms |
24056 KB |
Output is correct |
52 |
Correct |
30 ms |
24072 KB |
Output is correct |
53 |
Correct |
29 ms |
24056 KB |
Output is correct |
54 |
Correct |
28 ms |
24056 KB |
Output is correct |
55 |
Correct |
25 ms |
23800 KB |
Output is correct |
56 |
Correct |
212 ms |
31532 KB |
Output is correct |
57 |
Correct |
235 ms |
31180 KB |
Output is correct |
58 |
Correct |
29 ms |
24056 KB |
Output is correct |
59 |
Correct |
25 ms |
23800 KB |
Output is correct |
60 |
Correct |
25 ms |
23800 KB |
Output is correct |
61 |
Correct |
225 ms |
31180 KB |
Output is correct |
62 |
Correct |
29 ms |
24056 KB |
Output is correct |
63 |
Correct |
253 ms |
35444 KB |
Output is correct |
64 |
Correct |
228 ms |
31084 KB |
Output is correct |
65 |
Correct |
30 ms |
24060 KB |
Output is correct |
66 |
Correct |
252 ms |
31664 KB |
Output is correct |
67 |
Correct |
29 ms |
24056 KB |
Output is correct |
68 |
Correct |
30 ms |
24056 KB |
Output is correct |
69 |
Correct |
206 ms |
31336 KB |
Output is correct |
70 |
Correct |
28 ms |
24188 KB |
Output is correct |
71 |
Correct |
224 ms |
31340 KB |
Output is correct |
72 |
Correct |
254 ms |
32608 KB |
Output is correct |
73 |
Correct |
27 ms |
23800 KB |
Output is correct |
74 |
Correct |
238 ms |
35820 KB |
Output is correct |
75 |
Correct |
253 ms |
33388 KB |
Output is correct |
76 |
Correct |
29 ms |
24056 KB |
Output is correct |
77 |
Correct |
28 ms |
24056 KB |
Output is correct |
78 |
Correct |
208 ms |
31332 KB |
Output is correct |
79 |
Correct |
248 ms |
34720 KB |
Output is correct |
80 |
Correct |
31 ms |
24060 KB |
Output is correct |
81 |
Correct |
29 ms |
24084 KB |
Output is correct |
82 |
Correct |
24 ms |
23800 KB |
Output is correct |
83 |
Correct |
25 ms |
23800 KB |
Output is correct |
84 |
Correct |
31 ms |
24056 KB |
Output is correct |
85 |
Correct |
263 ms |
32816 KB |
Output is correct |
86 |
Correct |
28 ms |
24056 KB |
Output is correct |
87 |
Correct |
249 ms |
31652 KB |
Output is correct |
88 |
Correct |
24 ms |
23800 KB |
Output is correct |
89 |
Correct |
244 ms |
31468 KB |
Output is correct |
90 |
Correct |
270 ms |
32756 KB |
Output is correct |
91 |
Correct |
256 ms |
34284 KB |
Output is correct |
92 |
Correct |
201 ms |
31268 KB |
Output is correct |
93 |
Correct |
30 ms |
24056 KB |
Output is correct |
94 |
Correct |
25 ms |
23800 KB |
Output is correct |
95 |
Correct |
236 ms |
34324 KB |
Output is correct |
96 |
Correct |
279 ms |
36828 KB |
Output is correct |
97 |
Correct |
31 ms |
24056 KB |
Output is correct |
98 |
Correct |
24 ms |
23828 KB |
Output is correct |
99 |
Correct |
231 ms |
32980 KB |
Output is correct |
100 |
Correct |
30 ms |
24056 KB |
Output is correct |
101 |
Correct |
254 ms |
31848 KB |
Output is correct |
102 |
Correct |
264 ms |
35832 KB |
Output is correct |
103 |
Correct |
30 ms |
24184 KB |
Output is correct |
104 |
Correct |
29 ms |
24184 KB |
Output is correct |
105 |
Correct |
29 ms |
24056 KB |
Output is correct |
106 |
Correct |
30 ms |
24084 KB |
Output is correct |
107 |
Correct |
30 ms |
24084 KB |
Output is correct |
108 |
Correct |
1414 ms |
69740 KB |
Output is correct |
109 |
Correct |
1538 ms |
71852 KB |
Output is correct |
110 |
Correct |
1542 ms |
78940 KB |
Output is correct |
111 |
Correct |
1766 ms |
91100 KB |
Output is correct |
112 |
Correct |
1731 ms |
85392 KB |
Output is correct |
113 |
Correct |
1436 ms |
77628 KB |
Output is correct |
114 |
Correct |
1386 ms |
61172 KB |
Output is correct |
115 |
Correct |
1373 ms |
61676 KB |
Output is correct |
116 |
Correct |
1624 ms |
66148 KB |
Output is correct |
117 |
Correct |
1664 ms |
77532 KB |
Output is correct |
118 |
Correct |
1473 ms |
62556 KB |
Output is correct |
119 |
Correct |
1681 ms |
77680 KB |
Output is correct |
120 |
Correct |
1707 ms |
85804 KB |
Output is correct |
121 |
Correct |
1646 ms |
77612 KB |
Output is correct |
122 |
Correct |
1716 ms |
71172 KB |
Output is correct |
123 |
Correct |
1361 ms |
79056 KB |
Output is correct |
124 |
Correct |
1737 ms |
76292 KB |
Output is correct |