#include <bits/stdc++.h>
using namespace std;
vector<int> adj[300001];
int dep[300001] , par[300001];
//DSU
int pr[300001];
int gs[300001];
int findleader(int x){
if(pr[x]==x){
return x;
}
return pr[x] = findleader(pr[x]);
}
void mergegroup(int x,int y){
int led1 = findleader(x);
int led2 = findleader(y);
if(led1==led2)return;
if(gs[led1]>gs[led2]){
pr[led2]=led1;
gs[led1]+=gs[led2];
}else{
pr[led1]=led2;
gs[led2]+=gs[led1];
}
}
void merge(int a,int b){
while(a!=b){
if(dep[a] < dep[b])swap(a, b);
pr[a]=findleader(par[a]);
a = pr[a];
}
}
void dfs(int i ,int prr){
par[i] = prr;
dep[i] = dep[prr]+1;
for(auto j:adj[i]){
if(j!=prr)dfs(j,i);
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
for(int i = 0;i<=n;i++){
gs[i] = 1, pr[i] = i;
}
vector<int> col[k+1];
for(int i = 0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,0);
for(int i = 1;i<=n;i++){
int x;cin>>x;
col[x].push_back(i);
}
for(int i = 1;i<=k;i++){
for(int j = 1;j<col[i].size();j++){
merge(col[i][0],col[i][j]);
}
}
int deg[n+1] = {0};
for(int i = 1;i<=n;i++){
for(auto j:adj[i]){
if(findleader(i)!=findleader(j)){
deg[findleader(i)]++;deg[findleader(j)]++;
}
}
}
int cnt = 0;
for(int i = 1;i<=n;i++){
if(deg[i]==2){
cnt++;
}
}
cout<<(cnt+1)/2<<"\n";
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:62:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int j = 1;j<col[i].size();j++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
3 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7396 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
3 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7396 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
5 ms |
7640 KB |
Output is correct |
27 |
Correct |
5 ms |
7508 KB |
Output is correct |
28 |
Correct |
5 ms |
7636 KB |
Output is correct |
29 |
Correct |
6 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7508 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7764 KB |
Output is correct |
33 |
Correct |
4 ms |
7380 KB |
Output is correct |
34 |
Correct |
5 ms |
7508 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
5 ms |
7508 KB |
Output is correct |
37 |
Correct |
5 ms |
7508 KB |
Output is correct |
38 |
Correct |
4 ms |
7380 KB |
Output is correct |
39 |
Correct |
6 ms |
7508 KB |
Output is correct |
40 |
Correct |
5 ms |
7508 KB |
Output is correct |
41 |
Correct |
5 ms |
7508 KB |
Output is correct |
42 |
Correct |
5 ms |
7636 KB |
Output is correct |
43 |
Correct |
5 ms |
7680 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
5 ms |
7636 KB |
Output is correct |
46 |
Correct |
5 ms |
7616 KB |
Output is correct |
47 |
Correct |
4 ms |
7380 KB |
Output is correct |
48 |
Correct |
7 ms |
7508 KB |
Output is correct |
49 |
Correct |
5 ms |
7636 KB |
Output is correct |
50 |
Correct |
6 ms |
7636 KB |
Output is correct |
51 |
Correct |
5 ms |
7508 KB |
Output is correct |
52 |
Correct |
5 ms |
7508 KB |
Output is correct |
53 |
Correct |
5 ms |
7636 KB |
Output is correct |
54 |
Correct |
5 ms |
7508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
3 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7396 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7388 KB |
Output is correct |
26 |
Correct |
42 ms |
13396 KB |
Output is correct |
27 |
Correct |
60 ms |
13016 KB |
Output is correct |
28 |
Correct |
6 ms |
7508 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
61 ms |
13052 KB |
Output is correct |
32 |
Correct |
5 ms |
7508 KB |
Output is correct |
33 |
Correct |
65 ms |
16892 KB |
Output is correct |
34 |
Correct |
58 ms |
13016 KB |
Output is correct |
35 |
Correct |
5 ms |
7508 KB |
Output is correct |
36 |
Correct |
61 ms |
13036 KB |
Output is correct |
37 |
Correct |
5 ms |
7576 KB |
Output is correct |
38 |
Correct |
5 ms |
7508 KB |
Output is correct |
39 |
Correct |
45 ms |
13396 KB |
Output is correct |
40 |
Correct |
5 ms |
7636 KB |
Output is correct |
41 |
Correct |
49 ms |
12996 KB |
Output is correct |
42 |
Correct |
54 ms |
14028 KB |
Output is correct |
43 |
Correct |
4 ms |
7380 KB |
Output is correct |
44 |
Correct |
60 ms |
17184 KB |
Output is correct |
45 |
Correct |
54 ms |
14856 KB |
Output is correct |
46 |
Correct |
7 ms |
7508 KB |
Output is correct |
47 |
Correct |
5 ms |
7456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
13360 KB |
Output is correct |
2 |
Correct |
51 ms |
18244 KB |
Output is correct |
3 |
Correct |
5 ms |
7636 KB |
Output is correct |
4 |
Correct |
5 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7508 KB |
Output is correct |
8 |
Correct |
69 ms |
15236 KB |
Output is correct |
9 |
Correct |
5 ms |
7508 KB |
Output is correct |
10 |
Correct |
74 ms |
13084 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
58 ms |
13064 KB |
Output is correct |
13 |
Correct |
64 ms |
15128 KB |
Output is correct |
14 |
Correct |
82 ms |
18020 KB |
Output is correct |
15 |
Correct |
51 ms |
13456 KB |
Output is correct |
16 |
Correct |
5 ms |
7636 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
62 ms |
17760 KB |
Output is correct |
19 |
Correct |
70 ms |
19964 KB |
Output is correct |
20 |
Correct |
5 ms |
7508 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
60 ms |
15572 KB |
Output is correct |
23 |
Correct |
5 ms |
7508 KB |
Output is correct |
24 |
Correct |
78 ms |
13604 KB |
Output is correct |
25 |
Correct |
72 ms |
19152 KB |
Output is correct |
26 |
Correct |
5 ms |
7636 KB |
Output is correct |
27 |
Correct |
5 ms |
7732 KB |
Output is correct |
28 |
Correct |
5 ms |
7508 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
5 ms |
7636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
3 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7396 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
5 ms |
7640 KB |
Output is correct |
27 |
Correct |
5 ms |
7508 KB |
Output is correct |
28 |
Correct |
5 ms |
7636 KB |
Output is correct |
29 |
Correct |
6 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7508 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7764 KB |
Output is correct |
33 |
Correct |
4 ms |
7380 KB |
Output is correct |
34 |
Correct |
5 ms |
7508 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
5 ms |
7508 KB |
Output is correct |
37 |
Correct |
5 ms |
7508 KB |
Output is correct |
38 |
Correct |
4 ms |
7380 KB |
Output is correct |
39 |
Correct |
6 ms |
7508 KB |
Output is correct |
40 |
Correct |
5 ms |
7508 KB |
Output is correct |
41 |
Correct |
5 ms |
7508 KB |
Output is correct |
42 |
Correct |
5 ms |
7636 KB |
Output is correct |
43 |
Correct |
5 ms |
7680 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
5 ms |
7636 KB |
Output is correct |
46 |
Correct |
5 ms |
7616 KB |
Output is correct |
47 |
Correct |
4 ms |
7380 KB |
Output is correct |
48 |
Correct |
7 ms |
7508 KB |
Output is correct |
49 |
Correct |
5 ms |
7636 KB |
Output is correct |
50 |
Correct |
6 ms |
7636 KB |
Output is correct |
51 |
Correct |
5 ms |
7508 KB |
Output is correct |
52 |
Correct |
5 ms |
7508 KB |
Output is correct |
53 |
Correct |
5 ms |
7636 KB |
Output is correct |
54 |
Correct |
5 ms |
7508 KB |
Output is correct |
55 |
Correct |
4 ms |
7388 KB |
Output is correct |
56 |
Correct |
42 ms |
13396 KB |
Output is correct |
57 |
Correct |
60 ms |
13016 KB |
Output is correct |
58 |
Correct |
6 ms |
7508 KB |
Output is correct |
59 |
Correct |
4 ms |
7380 KB |
Output is correct |
60 |
Correct |
4 ms |
7380 KB |
Output is correct |
61 |
Correct |
61 ms |
13052 KB |
Output is correct |
62 |
Correct |
5 ms |
7508 KB |
Output is correct |
63 |
Correct |
65 ms |
16892 KB |
Output is correct |
64 |
Correct |
58 ms |
13016 KB |
Output is correct |
65 |
Correct |
5 ms |
7508 KB |
Output is correct |
66 |
Correct |
61 ms |
13036 KB |
Output is correct |
67 |
Correct |
5 ms |
7576 KB |
Output is correct |
68 |
Correct |
5 ms |
7508 KB |
Output is correct |
69 |
Correct |
45 ms |
13396 KB |
Output is correct |
70 |
Correct |
5 ms |
7636 KB |
Output is correct |
71 |
Correct |
49 ms |
12996 KB |
Output is correct |
72 |
Correct |
54 ms |
14028 KB |
Output is correct |
73 |
Correct |
4 ms |
7380 KB |
Output is correct |
74 |
Correct |
60 ms |
17184 KB |
Output is correct |
75 |
Correct |
54 ms |
14856 KB |
Output is correct |
76 |
Correct |
7 ms |
7508 KB |
Output is correct |
77 |
Correct |
5 ms |
7456 KB |
Output is correct |
78 |
Correct |
39 ms |
13360 KB |
Output is correct |
79 |
Correct |
51 ms |
18244 KB |
Output is correct |
80 |
Correct |
5 ms |
7636 KB |
Output is correct |
81 |
Correct |
5 ms |
7508 KB |
Output is correct |
82 |
Correct |
4 ms |
7380 KB |
Output is correct |
83 |
Correct |
4 ms |
7380 KB |
Output is correct |
84 |
Correct |
5 ms |
7508 KB |
Output is correct |
85 |
Correct |
69 ms |
15236 KB |
Output is correct |
86 |
Correct |
5 ms |
7508 KB |
Output is correct |
87 |
Correct |
74 ms |
13084 KB |
Output is correct |
88 |
Correct |
4 ms |
7380 KB |
Output is correct |
89 |
Correct |
58 ms |
13064 KB |
Output is correct |
90 |
Correct |
64 ms |
15128 KB |
Output is correct |
91 |
Correct |
82 ms |
18020 KB |
Output is correct |
92 |
Correct |
51 ms |
13456 KB |
Output is correct |
93 |
Correct |
5 ms |
7636 KB |
Output is correct |
94 |
Correct |
4 ms |
7380 KB |
Output is correct |
95 |
Correct |
62 ms |
17760 KB |
Output is correct |
96 |
Correct |
70 ms |
19964 KB |
Output is correct |
97 |
Correct |
5 ms |
7508 KB |
Output is correct |
98 |
Correct |
4 ms |
7380 KB |
Output is correct |
99 |
Correct |
60 ms |
15572 KB |
Output is correct |
100 |
Correct |
5 ms |
7508 KB |
Output is correct |
101 |
Correct |
78 ms |
13604 KB |
Output is correct |
102 |
Correct |
72 ms |
19152 KB |
Output is correct |
103 |
Correct |
5 ms |
7636 KB |
Output is correct |
104 |
Correct |
5 ms |
7732 KB |
Output is correct |
105 |
Correct |
5 ms |
7508 KB |
Output is correct |
106 |
Correct |
5 ms |
7508 KB |
Output is correct |
107 |
Correct |
5 ms |
7636 KB |
Output is correct |
108 |
Runtime error |
20 ms |
30672 KB |
Execution killed with signal 11 |
109 |
Halted |
0 ms |
0 KB |
- |