#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
vector<int> adj[500001];
int col[500001];
vector<int> cc[500001];
int par[500001][20];
int lev[500001];
int st[500001];
int endd[500001];
int co=0;
int dfs(int no,int par2=-1,int lev2=0){
st[no]=co;
co+=1;
par[no][0]=par2;
lev[no]=lev2;
for(auto j:adj[no]){
if(j==par2){
continue;
}
dfs(j,no,lev2+1);
}
endd[no]=co-1;
}
int lca(int aa,int bb){
if(lev[aa]>lev[bb]){
swap(aa,bb);
}
int dif=lev[bb]-lev[aa];
if(dif>0){
for(int j=19;j>=0;j--){
if((1<<j)&dif){
int cc=bb;
bb=par[bb][j];
}
}
}
if(aa==bb){
return aa;
}
for(int j=19;j>=0;j--){
if(par[aa][j]!=par[bb][j] and par[aa][j]!=-1 and par[bb][j]!=-1){
aa=par[aa][j];
bb=par[bb][j];
}
}
return par[aa][0];
}
int par3[500001];
int find(int no){
if(par3[no]==no){
return no;
}
par3[no]=find(par3[no]);
return par3[no];
}
vector<int> proc[500001];
set<pair<int,int>> cur;
int dfs2(int no,int par2=0){
if(cur.size()>0){
auto j=cur.lower_bound({st[no],0});
if(j==cur.end()){
}
else if((*j).b<=endd[no]){
par3[no]=par2;
}
}
for(auto j:proc[no]){
cur.insert({st[j],endd[j]});
}
auto tt=cur.find({st[no],endd[no]});
if(tt!=cur.end()){
cur.erase(tt);
}
for(auto j:adj[no]){
if(j==par2){
continue;
}
dfs2(j,no);
}
}
set<int> adj2[500001];
int ans=0;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
int aa,bb;
co=0;
cur.clear();
for(int i=0;i<n-1;i++){
cin>>aa>>bb;
adj[aa-1].pb(bb-1);
adj[bb-1].pb(aa-1);
}
for(int i=0;i<n;i++){
cin>>aa;
col[i]=aa;
cc[aa].pb(i);
}
dfs(0);
/* if(n==3 and k==2){
while(true){
continue;
}
}*/
for(int i=0;i<n;i++){
par3[i]=i;
}
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1]==-1){
par[i][j]=-1;
continue;
}
if(par[par[i][j-1]][j-1]==-1){
par[i][j]=-1;
continue;
}
par[i][j]=par[par[i][j-1]][j-1];
/* if(lev[par[i][j]]-lev[par[i][j-1]]!=(1<<(j-1))){
//cout<<i<<" "<<j<<endl;
}*/
}
}
for(int i=0;i<n;i++){
//cout<<par[i][0]<<" ";
}
//cout<<endl;
for(int i=1;i<=k;i++){
if(cc[i].size()<=1){
continue;
}
aa=lca(cc[i][0],cc[i][1]);
for(int j=1;j<cc[i].size();j++){
aa=lca(aa,cc[i][j]);
}
for(auto j:cc[i]){
proc[aa].pb(j);
}
// //cout<<aa<<" ";
}
// //cout<<endl;
dfs2(0);
for(int i=0;i<n;i++){
find(i);
}
for(int i=0;i<n;i++){
//cout<<par3[i]<<" ";
}
//cout<<endl;
for(int i=0;i<n;i++){
for(auto j:adj[i]){
if(find(par3[j])!=find(par3[i])){
adj2[find(par3[j])].insert(find(par3[i]));
adj2[find(par3[i])].insert(find(par3[j]));
}
}
}
for(int i=0;i<n;i++){
if(adj2[i].size()==1){
ans+=1;
}
}
cout<<(ans+1)/2<<endl;
return 0;
}
Compilation message
mergers.cpp: In function 'int dfs(int, int, int)':
mergers.cpp:29:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
mergers.cpp: In function 'int lca(int, int)':
mergers.cpp:40:9: warning: unused variable 'cc' [-Wunused-variable]
int cc=bb;
^~
mergers.cpp: In function 'int dfs2(int, int)':
mergers.cpp:94:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
mergers.cpp: In function 'int main()':
mergers.cpp:152:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=1;j<cc[i].size();j++){
~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
59136 KB |
Output is correct |
2 |
Correct |
41 ms |
59128 KB |
Output is correct |
3 |
Correct |
41 ms |
59136 KB |
Output is correct |
4 |
Correct |
41 ms |
59128 KB |
Output is correct |
5 |
Correct |
43 ms |
59128 KB |
Output is correct |
6 |
Correct |
42 ms |
59136 KB |
Output is correct |
7 |
Correct |
41 ms |
59136 KB |
Output is correct |
8 |
Correct |
40 ms |
59128 KB |
Output is correct |
9 |
Correct |
42 ms |
59128 KB |
Output is correct |
10 |
Correct |
42 ms |
59136 KB |
Output is correct |
11 |
Correct |
43 ms |
59136 KB |
Output is correct |
12 |
Correct |
43 ms |
59128 KB |
Output is correct |
13 |
Correct |
42 ms |
59132 KB |
Output is correct |
14 |
Correct |
41 ms |
59128 KB |
Output is correct |
15 |
Correct |
45 ms |
59128 KB |
Output is correct |
16 |
Correct |
42 ms |
59000 KB |
Output is correct |
17 |
Correct |
41 ms |
59136 KB |
Output is correct |
18 |
Correct |
41 ms |
59136 KB |
Output is correct |
19 |
Correct |
53 ms |
59132 KB |
Output is correct |
20 |
Correct |
43 ms |
59136 KB |
Output is correct |
21 |
Correct |
42 ms |
59136 KB |
Output is correct |
22 |
Correct |
42 ms |
59136 KB |
Output is correct |
23 |
Correct |
40 ms |
59128 KB |
Output is correct |
24 |
Correct |
42 ms |
59128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
59136 KB |
Output is correct |
2 |
Correct |
41 ms |
59128 KB |
Output is correct |
3 |
Correct |
41 ms |
59136 KB |
Output is correct |
4 |
Correct |
41 ms |
59128 KB |
Output is correct |
5 |
Correct |
43 ms |
59128 KB |
Output is correct |
6 |
Correct |
42 ms |
59136 KB |
Output is correct |
7 |
Correct |
41 ms |
59136 KB |
Output is correct |
8 |
Correct |
40 ms |
59128 KB |
Output is correct |
9 |
Correct |
42 ms |
59128 KB |
Output is correct |
10 |
Correct |
42 ms |
59136 KB |
Output is correct |
11 |
Correct |
43 ms |
59136 KB |
Output is correct |
12 |
Correct |
43 ms |
59128 KB |
Output is correct |
13 |
Correct |
42 ms |
59132 KB |
Output is correct |
14 |
Correct |
41 ms |
59128 KB |
Output is correct |
15 |
Correct |
45 ms |
59128 KB |
Output is correct |
16 |
Correct |
42 ms |
59000 KB |
Output is correct |
17 |
Correct |
41 ms |
59136 KB |
Output is correct |
18 |
Correct |
41 ms |
59136 KB |
Output is correct |
19 |
Correct |
53 ms |
59132 KB |
Output is correct |
20 |
Correct |
43 ms |
59136 KB |
Output is correct |
21 |
Correct |
42 ms |
59136 KB |
Output is correct |
22 |
Correct |
42 ms |
59136 KB |
Output is correct |
23 |
Correct |
40 ms |
59128 KB |
Output is correct |
24 |
Correct |
42 ms |
59128 KB |
Output is correct |
25 |
Correct |
40 ms |
59128 KB |
Output is correct |
26 |
Correct |
47 ms |
59776 KB |
Output is correct |
27 |
Correct |
44 ms |
59640 KB |
Output is correct |
28 |
Correct |
46 ms |
59896 KB |
Output is correct |
29 |
Correct |
44 ms |
59896 KB |
Output is correct |
30 |
Correct |
45 ms |
59648 KB |
Output is correct |
31 |
Correct |
43 ms |
59128 KB |
Output is correct |
32 |
Correct |
44 ms |
59896 KB |
Output is correct |
33 |
Correct |
42 ms |
59128 KB |
Output is correct |
34 |
Correct |
45 ms |
59768 KB |
Output is correct |
35 |
Correct |
46 ms |
59896 KB |
Output is correct |
36 |
Correct |
46 ms |
59520 KB |
Output is correct |
37 |
Correct |
44 ms |
59640 KB |
Output is correct |
38 |
Correct |
44 ms |
59128 KB |
Output is correct |
39 |
Correct |
44 ms |
59640 KB |
Output is correct |
40 |
Correct |
47 ms |
59776 KB |
Output is correct |
41 |
Correct |
45 ms |
59640 KB |
Output is correct |
42 |
Correct |
45 ms |
59640 KB |
Output is correct |
43 |
Correct |
44 ms |
59904 KB |
Output is correct |
44 |
Correct |
43 ms |
59136 KB |
Output is correct |
45 |
Correct |
45 ms |
59648 KB |
Output is correct |
46 |
Correct |
46 ms |
59640 KB |
Output is correct |
47 |
Correct |
44 ms |
59128 KB |
Output is correct |
48 |
Correct |
47 ms |
59640 KB |
Output is correct |
49 |
Correct |
45 ms |
59904 KB |
Output is correct |
50 |
Correct |
45 ms |
59896 KB |
Output is correct |
51 |
Correct |
44 ms |
59640 KB |
Output is correct |
52 |
Correct |
43 ms |
59648 KB |
Output is correct |
53 |
Correct |
46 ms |
59660 KB |
Output is correct |
54 |
Correct |
44 ms |
59640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
59136 KB |
Output is correct |
2 |
Correct |
41 ms |
59128 KB |
Output is correct |
3 |
Correct |
41 ms |
59136 KB |
Output is correct |
4 |
Correct |
41 ms |
59128 KB |
Output is correct |
5 |
Correct |
43 ms |
59128 KB |
Output is correct |
6 |
Correct |
42 ms |
59136 KB |
Output is correct |
7 |
Correct |
41 ms |
59136 KB |
Output is correct |
8 |
Correct |
40 ms |
59128 KB |
Output is correct |
9 |
Correct |
42 ms |
59128 KB |
Output is correct |
10 |
Correct |
42 ms |
59136 KB |
Output is correct |
11 |
Correct |
43 ms |
59136 KB |
Output is correct |
12 |
Correct |
43 ms |
59128 KB |
Output is correct |
13 |
Correct |
42 ms |
59132 KB |
Output is correct |
14 |
Correct |
41 ms |
59128 KB |
Output is correct |
15 |
Correct |
45 ms |
59128 KB |
Output is correct |
16 |
Correct |
42 ms |
59000 KB |
Output is correct |
17 |
Correct |
41 ms |
59136 KB |
Output is correct |
18 |
Correct |
41 ms |
59136 KB |
Output is correct |
19 |
Correct |
53 ms |
59132 KB |
Output is correct |
20 |
Correct |
43 ms |
59136 KB |
Output is correct |
21 |
Correct |
42 ms |
59136 KB |
Output is correct |
22 |
Correct |
42 ms |
59136 KB |
Output is correct |
23 |
Correct |
40 ms |
59128 KB |
Output is correct |
24 |
Correct |
42 ms |
59128 KB |
Output is correct |
25 |
Correct |
43 ms |
59128 KB |
Output is correct |
26 |
Correct |
174 ms |
79360 KB |
Output is correct |
27 |
Correct |
234 ms |
79088 KB |
Output is correct |
28 |
Correct |
42 ms |
59512 KB |
Output is correct |
29 |
Correct |
39 ms |
59136 KB |
Output is correct |
30 |
Correct |
39 ms |
59136 KB |
Output is correct |
31 |
Correct |
185 ms |
75000 KB |
Output is correct |
32 |
Correct |
42 ms |
59648 KB |
Output is correct |
33 |
Correct |
285 ms |
87744 KB |
Output is correct |
34 |
Correct |
206 ms |
77300 KB |
Output is correct |
35 |
Correct |
42 ms |
59512 KB |
Output is correct |
36 |
Correct |
198 ms |
75912 KB |
Output is correct |
37 |
Correct |
42 ms |
59640 KB |
Output is correct |
38 |
Correct |
41 ms |
59560 KB |
Output is correct |
39 |
Correct |
188 ms |
79484 KB |
Output is correct |
40 |
Correct |
42 ms |
60032 KB |
Output is correct |
41 |
Correct |
222 ms |
79120 KB |
Output is correct |
42 |
Correct |
246 ms |
79728 KB |
Output is correct |
43 |
Correct |
39 ms |
59128 KB |
Output is correct |
44 |
Correct |
252 ms |
88024 KB |
Output is correct |
45 |
Correct |
248 ms |
83568 KB |
Output is correct |
46 |
Correct |
41 ms |
59640 KB |
Output is correct |
47 |
Correct |
43 ms |
59644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
77952 KB |
Output is correct |
2 |
Correct |
222 ms |
84952 KB |
Output is correct |
3 |
Correct |
41 ms |
59776 KB |
Output is correct |
4 |
Correct |
42 ms |
59512 KB |
Output is correct |
5 |
Correct |
39 ms |
59128 KB |
Output is correct |
6 |
Correct |
39 ms |
59136 KB |
Output is correct |
7 |
Correct |
40 ms |
59640 KB |
Output is correct |
8 |
Correct |
221 ms |
77160 KB |
Output is correct |
9 |
Correct |
41 ms |
59512 KB |
Output is correct |
10 |
Correct |
203 ms |
74768 KB |
Output is correct |
11 |
Correct |
39 ms |
59128 KB |
Output is correct |
12 |
Correct |
226 ms |
74744 KB |
Output is correct |
13 |
Correct |
227 ms |
78200 KB |
Output is correct |
14 |
Correct |
246 ms |
86396 KB |
Output is correct |
15 |
Correct |
183 ms |
79348 KB |
Output is correct |
16 |
Correct |
41 ms |
59648 KB |
Output is correct |
17 |
Correct |
39 ms |
59136 KB |
Output is correct |
18 |
Correct |
248 ms |
84592 KB |
Output is correct |
19 |
Correct |
240 ms |
91128 KB |
Output is correct |
20 |
Correct |
43 ms |
59640 KB |
Output is correct |
21 |
Correct |
40 ms |
59128 KB |
Output is correct |
22 |
Correct |
192 ms |
79724 KB |
Output is correct |
23 |
Correct |
42 ms |
59768 KB |
Output is correct |
24 |
Correct |
205 ms |
76152 KB |
Output is correct |
25 |
Correct |
264 ms |
89508 KB |
Output is correct |
26 |
Correct |
43 ms |
59904 KB |
Output is correct |
27 |
Correct |
42 ms |
60032 KB |
Output is correct |
28 |
Correct |
41 ms |
59640 KB |
Output is correct |
29 |
Correct |
41 ms |
59640 KB |
Output is correct |
30 |
Correct |
41 ms |
59732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
59136 KB |
Output is correct |
2 |
Correct |
41 ms |
59128 KB |
Output is correct |
3 |
Correct |
41 ms |
59136 KB |
Output is correct |
4 |
Correct |
41 ms |
59128 KB |
Output is correct |
5 |
Correct |
43 ms |
59128 KB |
Output is correct |
6 |
Correct |
42 ms |
59136 KB |
Output is correct |
7 |
Correct |
41 ms |
59136 KB |
Output is correct |
8 |
Correct |
40 ms |
59128 KB |
Output is correct |
9 |
Correct |
42 ms |
59128 KB |
Output is correct |
10 |
Correct |
42 ms |
59136 KB |
Output is correct |
11 |
Correct |
43 ms |
59136 KB |
Output is correct |
12 |
Correct |
43 ms |
59128 KB |
Output is correct |
13 |
Correct |
42 ms |
59132 KB |
Output is correct |
14 |
Correct |
41 ms |
59128 KB |
Output is correct |
15 |
Correct |
45 ms |
59128 KB |
Output is correct |
16 |
Correct |
42 ms |
59000 KB |
Output is correct |
17 |
Correct |
41 ms |
59136 KB |
Output is correct |
18 |
Correct |
41 ms |
59136 KB |
Output is correct |
19 |
Correct |
53 ms |
59132 KB |
Output is correct |
20 |
Correct |
43 ms |
59136 KB |
Output is correct |
21 |
Correct |
42 ms |
59136 KB |
Output is correct |
22 |
Correct |
42 ms |
59136 KB |
Output is correct |
23 |
Correct |
40 ms |
59128 KB |
Output is correct |
24 |
Correct |
42 ms |
59128 KB |
Output is correct |
25 |
Correct |
40 ms |
59128 KB |
Output is correct |
26 |
Correct |
47 ms |
59776 KB |
Output is correct |
27 |
Correct |
44 ms |
59640 KB |
Output is correct |
28 |
Correct |
46 ms |
59896 KB |
Output is correct |
29 |
Correct |
44 ms |
59896 KB |
Output is correct |
30 |
Correct |
45 ms |
59648 KB |
Output is correct |
31 |
Correct |
43 ms |
59128 KB |
Output is correct |
32 |
Correct |
44 ms |
59896 KB |
Output is correct |
33 |
Correct |
42 ms |
59128 KB |
Output is correct |
34 |
Correct |
45 ms |
59768 KB |
Output is correct |
35 |
Correct |
46 ms |
59896 KB |
Output is correct |
36 |
Correct |
46 ms |
59520 KB |
Output is correct |
37 |
Correct |
44 ms |
59640 KB |
Output is correct |
38 |
Correct |
44 ms |
59128 KB |
Output is correct |
39 |
Correct |
44 ms |
59640 KB |
Output is correct |
40 |
Correct |
47 ms |
59776 KB |
Output is correct |
41 |
Correct |
45 ms |
59640 KB |
Output is correct |
42 |
Correct |
45 ms |
59640 KB |
Output is correct |
43 |
Correct |
44 ms |
59904 KB |
Output is correct |
44 |
Correct |
43 ms |
59136 KB |
Output is correct |
45 |
Correct |
45 ms |
59648 KB |
Output is correct |
46 |
Correct |
46 ms |
59640 KB |
Output is correct |
47 |
Correct |
44 ms |
59128 KB |
Output is correct |
48 |
Correct |
47 ms |
59640 KB |
Output is correct |
49 |
Correct |
45 ms |
59904 KB |
Output is correct |
50 |
Correct |
45 ms |
59896 KB |
Output is correct |
51 |
Correct |
44 ms |
59640 KB |
Output is correct |
52 |
Correct |
43 ms |
59648 KB |
Output is correct |
53 |
Correct |
46 ms |
59660 KB |
Output is correct |
54 |
Correct |
44 ms |
59640 KB |
Output is correct |
55 |
Correct |
43 ms |
59128 KB |
Output is correct |
56 |
Correct |
174 ms |
79360 KB |
Output is correct |
57 |
Correct |
234 ms |
79088 KB |
Output is correct |
58 |
Correct |
42 ms |
59512 KB |
Output is correct |
59 |
Correct |
39 ms |
59136 KB |
Output is correct |
60 |
Correct |
39 ms |
59136 KB |
Output is correct |
61 |
Correct |
185 ms |
75000 KB |
Output is correct |
62 |
Correct |
42 ms |
59648 KB |
Output is correct |
63 |
Correct |
285 ms |
87744 KB |
Output is correct |
64 |
Correct |
206 ms |
77300 KB |
Output is correct |
65 |
Correct |
42 ms |
59512 KB |
Output is correct |
66 |
Correct |
198 ms |
75912 KB |
Output is correct |
67 |
Correct |
42 ms |
59640 KB |
Output is correct |
68 |
Correct |
41 ms |
59560 KB |
Output is correct |
69 |
Correct |
188 ms |
79484 KB |
Output is correct |
70 |
Correct |
42 ms |
60032 KB |
Output is correct |
71 |
Correct |
222 ms |
79120 KB |
Output is correct |
72 |
Correct |
246 ms |
79728 KB |
Output is correct |
73 |
Correct |
39 ms |
59128 KB |
Output is correct |
74 |
Correct |
252 ms |
88024 KB |
Output is correct |
75 |
Correct |
248 ms |
83568 KB |
Output is correct |
76 |
Correct |
41 ms |
59640 KB |
Output is correct |
77 |
Correct |
43 ms |
59644 KB |
Output is correct |
78 |
Correct |
209 ms |
77952 KB |
Output is correct |
79 |
Correct |
222 ms |
84952 KB |
Output is correct |
80 |
Correct |
41 ms |
59776 KB |
Output is correct |
81 |
Correct |
42 ms |
59512 KB |
Output is correct |
82 |
Correct |
39 ms |
59128 KB |
Output is correct |
83 |
Correct |
39 ms |
59136 KB |
Output is correct |
84 |
Correct |
40 ms |
59640 KB |
Output is correct |
85 |
Correct |
221 ms |
77160 KB |
Output is correct |
86 |
Correct |
41 ms |
59512 KB |
Output is correct |
87 |
Correct |
203 ms |
74768 KB |
Output is correct |
88 |
Correct |
39 ms |
59128 KB |
Output is correct |
89 |
Correct |
226 ms |
74744 KB |
Output is correct |
90 |
Correct |
227 ms |
78200 KB |
Output is correct |
91 |
Correct |
246 ms |
86396 KB |
Output is correct |
92 |
Correct |
183 ms |
79348 KB |
Output is correct |
93 |
Correct |
41 ms |
59648 KB |
Output is correct |
94 |
Correct |
39 ms |
59136 KB |
Output is correct |
95 |
Correct |
248 ms |
84592 KB |
Output is correct |
96 |
Correct |
240 ms |
91128 KB |
Output is correct |
97 |
Correct |
43 ms |
59640 KB |
Output is correct |
98 |
Correct |
40 ms |
59128 KB |
Output is correct |
99 |
Correct |
192 ms |
79724 KB |
Output is correct |
100 |
Correct |
42 ms |
59768 KB |
Output is correct |
101 |
Correct |
205 ms |
76152 KB |
Output is correct |
102 |
Correct |
264 ms |
89508 KB |
Output is correct |
103 |
Correct |
43 ms |
59904 KB |
Output is correct |
104 |
Correct |
42 ms |
60032 KB |
Output is correct |
105 |
Correct |
41 ms |
59640 KB |
Output is correct |
106 |
Correct |
41 ms |
59640 KB |
Output is correct |
107 |
Correct |
41 ms |
59732 KB |
Output is correct |
108 |
Correct |
1294 ms |
165168 KB |
Output is correct |
109 |
Correct |
1575 ms |
169316 KB |
Output is correct |
110 |
Correct |
1592 ms |
194528 KB |
Output is correct |
111 |
Correct |
1562 ms |
221036 KB |
Output is correct |
112 |
Correct |
1517 ms |
190972 KB |
Output is correct |
113 |
Correct |
1466 ms |
188244 KB |
Output is correct |
114 |
Correct |
1297 ms |
149000 KB |
Output is correct |
115 |
Correct |
1310 ms |
159400 KB |
Output is correct |
116 |
Correct |
1292 ms |
142468 KB |
Output is correct |
117 |
Correct |
1452 ms |
196788 KB |
Output is correct |
118 |
Correct |
1092 ms |
137436 KB |
Output is correct |
119 |
Correct |
1426 ms |
196728 KB |
Output is correct |
120 |
Correct |
1476 ms |
213624 KB |
Output is correct |
121 |
Correct |
1487 ms |
196728 KB |
Output is correct |
122 |
Correct |
1640 ms |
157780 KB |
Output is correct |
123 |
Correct |
1503 ms |
198244 KB |
Output is correct |
124 |
Correct |
1361 ms |
178168 KB |
Output is correct |