#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define vp vector<pii>
using namespace std;
const int N=5e5+5;
vector<int>g[N],gr[N];
set<int>gg[N];
int dep[N],up[N][21],pr[N],d[N],t=0;
int get(int r){
return pr[r]==r?r:pr[r]=get(pr[r]);
}
void dfs(int u,int p,int l){
dep[u]=l;up[u][0]=p;d[u]=++t;
for(int i=1;i<=20;i++)up[u][i]=up[up[u][i-1]][i-1];
for(auto v:g[u]){
if(v==p)continue;
dfs(v,u,l+1);
}
}
int qr(int a,int b){
if(dep[a]>dep[b])swap(a,b);
for(int i=20;i>=0;i--)if(dep[b]-(1<<i)>=dep[a])b=up[b][i];
if(a==b)return a;
for(int i=20;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
return up[a][0];
}
bool cmp(int a,int b){
return d[a]<d[b];
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,k;cin>>n>>k;iota(pr,pr+n+1,0);
for(int i=1;i<=n-1;i++){
int u,v;cin>>u>>v;g[u].pb(v);g[v].pb(u);
}dfs(1,1,0);
for(int i=1,x;i<=n;i++)cin>>x,gr[x].pb(i);
for(int i=1;i<=k;i++){
sort(gr[i].begin(),gr[i].end(),cmp);
for(int j=1;j<gr[i].size();j++){
int lca=qr(gr[i][j-1],gr[i][j]);
int u=gr[i][j-1],v=gr[i][j];
while(get(u)!=get(lca)){
int uu=up[get(u)][0];
pr[get(u)]=get(uu);u=uu;
}
while(get(v)!=get(lca)){
int vv=up[get(v)][0];
pr[get(v)]=get(vv);v=vv;
}
}
}
for(int i=1;i<=n;i++){
for(auto it : g[i]){
if(get(it)==get(i))continue;
gg[get(it)].insert(get(i));
gg[get(i)].insert(get(it));
}
}int ct=0;
for(int i=1;i<=n;i++)if(gg[i].size()==1)ct++;
cout<<(ct+1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:47:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int j=1;j<gr[i].size();j++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
55128 KB |
Output is correct |
2 |
Correct |
15 ms |
55132 KB |
Output is correct |
3 |
Correct |
18 ms |
55132 KB |
Output is correct |
4 |
Correct |
15 ms |
55128 KB |
Output is correct |
5 |
Correct |
15 ms |
55128 KB |
Output is correct |
6 |
Correct |
14 ms |
55128 KB |
Output is correct |
7 |
Correct |
15 ms |
55128 KB |
Output is correct |
8 |
Correct |
15 ms |
55128 KB |
Output is correct |
9 |
Correct |
17 ms |
55128 KB |
Output is correct |
10 |
Correct |
14 ms |
55128 KB |
Output is correct |
11 |
Correct |
15 ms |
55132 KB |
Output is correct |
12 |
Correct |
14 ms |
55128 KB |
Output is correct |
13 |
Correct |
14 ms |
55128 KB |
Output is correct |
14 |
Correct |
14 ms |
55128 KB |
Output is correct |
15 |
Correct |
15 ms |
55384 KB |
Output is correct |
16 |
Correct |
15 ms |
55132 KB |
Output is correct |
17 |
Correct |
15 ms |
55128 KB |
Output is correct |
18 |
Correct |
15 ms |
55132 KB |
Output is correct |
19 |
Correct |
17 ms |
55132 KB |
Output is correct |
20 |
Correct |
16 ms |
55136 KB |
Output is correct |
21 |
Correct |
15 ms |
55128 KB |
Output is correct |
22 |
Correct |
15 ms |
55128 KB |
Output is correct |
23 |
Correct |
14 ms |
55128 KB |
Output is correct |
24 |
Correct |
15 ms |
55128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
55128 KB |
Output is correct |
2 |
Correct |
15 ms |
55132 KB |
Output is correct |
3 |
Correct |
18 ms |
55132 KB |
Output is correct |
4 |
Correct |
15 ms |
55128 KB |
Output is correct |
5 |
Correct |
15 ms |
55128 KB |
Output is correct |
6 |
Correct |
14 ms |
55128 KB |
Output is correct |
7 |
Correct |
15 ms |
55128 KB |
Output is correct |
8 |
Correct |
15 ms |
55128 KB |
Output is correct |
9 |
Correct |
17 ms |
55128 KB |
Output is correct |
10 |
Correct |
14 ms |
55128 KB |
Output is correct |
11 |
Correct |
15 ms |
55132 KB |
Output is correct |
12 |
Correct |
14 ms |
55128 KB |
Output is correct |
13 |
Correct |
14 ms |
55128 KB |
Output is correct |
14 |
Correct |
14 ms |
55128 KB |
Output is correct |
15 |
Correct |
15 ms |
55384 KB |
Output is correct |
16 |
Correct |
15 ms |
55132 KB |
Output is correct |
17 |
Correct |
15 ms |
55128 KB |
Output is correct |
18 |
Correct |
15 ms |
55132 KB |
Output is correct |
19 |
Correct |
17 ms |
55132 KB |
Output is correct |
20 |
Correct |
16 ms |
55136 KB |
Output is correct |
21 |
Correct |
15 ms |
55128 KB |
Output is correct |
22 |
Correct |
15 ms |
55128 KB |
Output is correct |
23 |
Correct |
14 ms |
55128 KB |
Output is correct |
24 |
Correct |
15 ms |
55128 KB |
Output is correct |
25 |
Correct |
15 ms |
55128 KB |
Output is correct |
26 |
Correct |
17 ms |
55388 KB |
Output is correct |
27 |
Correct |
16 ms |
55128 KB |
Output is correct |
28 |
Correct |
17 ms |
55652 KB |
Output is correct |
29 |
Correct |
17 ms |
55896 KB |
Output is correct |
30 |
Correct |
16 ms |
55128 KB |
Output is correct |
31 |
Correct |
16 ms |
55128 KB |
Output is correct |
32 |
Correct |
17 ms |
55388 KB |
Output is correct |
33 |
Correct |
15 ms |
55156 KB |
Output is correct |
34 |
Correct |
18 ms |
55640 KB |
Output is correct |
35 |
Correct |
16 ms |
55644 KB |
Output is correct |
36 |
Correct |
17 ms |
55132 KB |
Output is correct |
37 |
Correct |
17 ms |
55384 KB |
Output is correct |
38 |
Correct |
15 ms |
55128 KB |
Output is correct |
39 |
Correct |
18 ms |
55180 KB |
Output is correct |
40 |
Correct |
16 ms |
55128 KB |
Output is correct |
41 |
Correct |
18 ms |
55128 KB |
Output is correct |
42 |
Correct |
17 ms |
55384 KB |
Output is correct |
43 |
Correct |
17 ms |
55388 KB |
Output is correct |
44 |
Correct |
14 ms |
55128 KB |
Output is correct |
45 |
Correct |
17 ms |
55384 KB |
Output is correct |
46 |
Correct |
19 ms |
55384 KB |
Output is correct |
47 |
Correct |
15 ms |
55132 KB |
Output is correct |
48 |
Correct |
19 ms |
55384 KB |
Output is correct |
49 |
Correct |
19 ms |
55692 KB |
Output is correct |
50 |
Correct |
18 ms |
55640 KB |
Output is correct |
51 |
Correct |
17 ms |
55388 KB |
Output is correct |
52 |
Correct |
16 ms |
55128 KB |
Output is correct |
53 |
Correct |
17 ms |
55428 KB |
Output is correct |
54 |
Correct |
16 ms |
55128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
55128 KB |
Output is correct |
2 |
Correct |
15 ms |
55132 KB |
Output is correct |
3 |
Correct |
18 ms |
55132 KB |
Output is correct |
4 |
Correct |
15 ms |
55128 KB |
Output is correct |
5 |
Correct |
15 ms |
55128 KB |
Output is correct |
6 |
Correct |
14 ms |
55128 KB |
Output is correct |
7 |
Correct |
15 ms |
55128 KB |
Output is correct |
8 |
Correct |
15 ms |
55128 KB |
Output is correct |
9 |
Correct |
17 ms |
55128 KB |
Output is correct |
10 |
Correct |
14 ms |
55128 KB |
Output is correct |
11 |
Correct |
15 ms |
55132 KB |
Output is correct |
12 |
Correct |
14 ms |
55128 KB |
Output is correct |
13 |
Correct |
14 ms |
55128 KB |
Output is correct |
14 |
Correct |
14 ms |
55128 KB |
Output is correct |
15 |
Correct |
15 ms |
55384 KB |
Output is correct |
16 |
Correct |
15 ms |
55132 KB |
Output is correct |
17 |
Correct |
15 ms |
55128 KB |
Output is correct |
18 |
Correct |
15 ms |
55132 KB |
Output is correct |
19 |
Correct |
17 ms |
55132 KB |
Output is correct |
20 |
Correct |
16 ms |
55136 KB |
Output is correct |
21 |
Correct |
15 ms |
55128 KB |
Output is correct |
22 |
Correct |
15 ms |
55128 KB |
Output is correct |
23 |
Correct |
14 ms |
55128 KB |
Output is correct |
24 |
Correct |
15 ms |
55128 KB |
Output is correct |
25 |
Correct |
15 ms |
55384 KB |
Output is correct |
26 |
Correct |
82 ms |
67716 KB |
Output is correct |
27 |
Correct |
102 ms |
67364 KB |
Output is correct |
28 |
Correct |
16 ms |
55128 KB |
Output is correct |
29 |
Correct |
15 ms |
55128 KB |
Output is correct |
30 |
Correct |
14 ms |
55136 KB |
Output is correct |
31 |
Correct |
110 ms |
67340 KB |
Output is correct |
32 |
Correct |
16 ms |
55128 KB |
Output is correct |
33 |
Correct |
114 ms |
71440 KB |
Output is correct |
34 |
Correct |
105 ms |
67528 KB |
Output is correct |
35 |
Correct |
17 ms |
55128 KB |
Output is correct |
36 |
Correct |
105 ms |
67696 KB |
Output is correct |
37 |
Correct |
16 ms |
55160 KB |
Output is correct |
38 |
Correct |
17 ms |
55384 KB |
Output is correct |
39 |
Correct |
85 ms |
67656 KB |
Output is correct |
40 |
Correct |
16 ms |
55384 KB |
Output is correct |
41 |
Correct |
116 ms |
67472 KB |
Output is correct |
42 |
Correct |
100 ms |
68684 KB |
Output is correct |
43 |
Correct |
16 ms |
55128 KB |
Output is correct |
44 |
Correct |
145 ms |
71916 KB |
Output is correct |
45 |
Correct |
136 ms |
69688 KB |
Output is correct |
46 |
Correct |
16 ms |
55384 KB |
Output is correct |
47 |
Correct |
17 ms |
55132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
67468 KB |
Output is correct |
2 |
Correct |
145 ms |
79416 KB |
Output is correct |
3 |
Correct |
18 ms |
55384 KB |
Output is correct |
4 |
Correct |
17 ms |
55128 KB |
Output is correct |
5 |
Correct |
16 ms |
55132 KB |
Output is correct |
6 |
Correct |
15 ms |
55132 KB |
Output is correct |
7 |
Correct |
17 ms |
55148 KB |
Output is correct |
8 |
Correct |
123 ms |
69176 KB |
Output is correct |
9 |
Correct |
17 ms |
55128 KB |
Output is correct |
10 |
Correct |
103 ms |
67444 KB |
Output is correct |
11 |
Correct |
15 ms |
55128 KB |
Output is correct |
12 |
Correct |
85 ms |
67376 KB |
Output is correct |
13 |
Correct |
129 ms |
70516 KB |
Output is correct |
14 |
Correct |
165 ms |
79352 KB |
Output is correct |
15 |
Correct |
93 ms |
67640 KB |
Output is correct |
16 |
Correct |
16 ms |
55384 KB |
Output is correct |
17 |
Correct |
15 ms |
55128 KB |
Output is correct |
18 |
Correct |
137 ms |
77440 KB |
Output is correct |
19 |
Correct |
143 ms |
81740 KB |
Output is correct |
20 |
Correct |
17 ms |
55384 KB |
Output is correct |
21 |
Correct |
15 ms |
55128 KB |
Output is correct |
22 |
Correct |
110 ms |
70244 KB |
Output is correct |
23 |
Correct |
16 ms |
55384 KB |
Output is correct |
24 |
Correct |
111 ms |
68492 KB |
Output is correct |
25 |
Correct |
180 ms |
80784 KB |
Output is correct |
26 |
Correct |
17 ms |
55536 KB |
Output is correct |
27 |
Correct |
18 ms |
55640 KB |
Output is correct |
28 |
Correct |
17 ms |
55388 KB |
Output is correct |
29 |
Correct |
16 ms |
55388 KB |
Output is correct |
30 |
Correct |
16 ms |
55384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
55128 KB |
Output is correct |
2 |
Correct |
15 ms |
55132 KB |
Output is correct |
3 |
Correct |
18 ms |
55132 KB |
Output is correct |
4 |
Correct |
15 ms |
55128 KB |
Output is correct |
5 |
Correct |
15 ms |
55128 KB |
Output is correct |
6 |
Correct |
14 ms |
55128 KB |
Output is correct |
7 |
Correct |
15 ms |
55128 KB |
Output is correct |
8 |
Correct |
15 ms |
55128 KB |
Output is correct |
9 |
Correct |
17 ms |
55128 KB |
Output is correct |
10 |
Correct |
14 ms |
55128 KB |
Output is correct |
11 |
Correct |
15 ms |
55132 KB |
Output is correct |
12 |
Correct |
14 ms |
55128 KB |
Output is correct |
13 |
Correct |
14 ms |
55128 KB |
Output is correct |
14 |
Correct |
14 ms |
55128 KB |
Output is correct |
15 |
Correct |
15 ms |
55384 KB |
Output is correct |
16 |
Correct |
15 ms |
55132 KB |
Output is correct |
17 |
Correct |
15 ms |
55128 KB |
Output is correct |
18 |
Correct |
15 ms |
55132 KB |
Output is correct |
19 |
Correct |
17 ms |
55132 KB |
Output is correct |
20 |
Correct |
16 ms |
55136 KB |
Output is correct |
21 |
Correct |
15 ms |
55128 KB |
Output is correct |
22 |
Correct |
15 ms |
55128 KB |
Output is correct |
23 |
Correct |
14 ms |
55128 KB |
Output is correct |
24 |
Correct |
15 ms |
55128 KB |
Output is correct |
25 |
Correct |
15 ms |
55128 KB |
Output is correct |
26 |
Correct |
17 ms |
55388 KB |
Output is correct |
27 |
Correct |
16 ms |
55128 KB |
Output is correct |
28 |
Correct |
17 ms |
55652 KB |
Output is correct |
29 |
Correct |
17 ms |
55896 KB |
Output is correct |
30 |
Correct |
16 ms |
55128 KB |
Output is correct |
31 |
Correct |
16 ms |
55128 KB |
Output is correct |
32 |
Correct |
17 ms |
55388 KB |
Output is correct |
33 |
Correct |
15 ms |
55156 KB |
Output is correct |
34 |
Correct |
18 ms |
55640 KB |
Output is correct |
35 |
Correct |
16 ms |
55644 KB |
Output is correct |
36 |
Correct |
17 ms |
55132 KB |
Output is correct |
37 |
Correct |
17 ms |
55384 KB |
Output is correct |
38 |
Correct |
15 ms |
55128 KB |
Output is correct |
39 |
Correct |
18 ms |
55180 KB |
Output is correct |
40 |
Correct |
16 ms |
55128 KB |
Output is correct |
41 |
Correct |
18 ms |
55128 KB |
Output is correct |
42 |
Correct |
17 ms |
55384 KB |
Output is correct |
43 |
Correct |
17 ms |
55388 KB |
Output is correct |
44 |
Correct |
14 ms |
55128 KB |
Output is correct |
45 |
Correct |
17 ms |
55384 KB |
Output is correct |
46 |
Correct |
19 ms |
55384 KB |
Output is correct |
47 |
Correct |
15 ms |
55132 KB |
Output is correct |
48 |
Correct |
19 ms |
55384 KB |
Output is correct |
49 |
Correct |
19 ms |
55692 KB |
Output is correct |
50 |
Correct |
18 ms |
55640 KB |
Output is correct |
51 |
Correct |
17 ms |
55388 KB |
Output is correct |
52 |
Correct |
16 ms |
55128 KB |
Output is correct |
53 |
Correct |
17 ms |
55428 KB |
Output is correct |
54 |
Correct |
16 ms |
55128 KB |
Output is correct |
55 |
Correct |
15 ms |
55384 KB |
Output is correct |
56 |
Correct |
82 ms |
67716 KB |
Output is correct |
57 |
Correct |
102 ms |
67364 KB |
Output is correct |
58 |
Correct |
16 ms |
55128 KB |
Output is correct |
59 |
Correct |
15 ms |
55128 KB |
Output is correct |
60 |
Correct |
14 ms |
55136 KB |
Output is correct |
61 |
Correct |
110 ms |
67340 KB |
Output is correct |
62 |
Correct |
16 ms |
55128 KB |
Output is correct |
63 |
Correct |
114 ms |
71440 KB |
Output is correct |
64 |
Correct |
105 ms |
67528 KB |
Output is correct |
65 |
Correct |
17 ms |
55128 KB |
Output is correct |
66 |
Correct |
105 ms |
67696 KB |
Output is correct |
67 |
Correct |
16 ms |
55160 KB |
Output is correct |
68 |
Correct |
17 ms |
55384 KB |
Output is correct |
69 |
Correct |
85 ms |
67656 KB |
Output is correct |
70 |
Correct |
16 ms |
55384 KB |
Output is correct |
71 |
Correct |
116 ms |
67472 KB |
Output is correct |
72 |
Correct |
100 ms |
68684 KB |
Output is correct |
73 |
Correct |
16 ms |
55128 KB |
Output is correct |
74 |
Correct |
145 ms |
71916 KB |
Output is correct |
75 |
Correct |
136 ms |
69688 KB |
Output is correct |
76 |
Correct |
16 ms |
55384 KB |
Output is correct |
77 |
Correct |
17 ms |
55132 KB |
Output is correct |
78 |
Correct |
87 ms |
67468 KB |
Output is correct |
79 |
Correct |
145 ms |
79416 KB |
Output is correct |
80 |
Correct |
18 ms |
55384 KB |
Output is correct |
81 |
Correct |
17 ms |
55128 KB |
Output is correct |
82 |
Correct |
16 ms |
55132 KB |
Output is correct |
83 |
Correct |
15 ms |
55132 KB |
Output is correct |
84 |
Correct |
17 ms |
55148 KB |
Output is correct |
85 |
Correct |
123 ms |
69176 KB |
Output is correct |
86 |
Correct |
17 ms |
55128 KB |
Output is correct |
87 |
Correct |
103 ms |
67444 KB |
Output is correct |
88 |
Correct |
15 ms |
55128 KB |
Output is correct |
89 |
Correct |
85 ms |
67376 KB |
Output is correct |
90 |
Correct |
129 ms |
70516 KB |
Output is correct |
91 |
Correct |
165 ms |
79352 KB |
Output is correct |
92 |
Correct |
93 ms |
67640 KB |
Output is correct |
93 |
Correct |
16 ms |
55384 KB |
Output is correct |
94 |
Correct |
15 ms |
55128 KB |
Output is correct |
95 |
Correct |
137 ms |
77440 KB |
Output is correct |
96 |
Correct |
143 ms |
81740 KB |
Output is correct |
97 |
Correct |
17 ms |
55384 KB |
Output is correct |
98 |
Correct |
15 ms |
55128 KB |
Output is correct |
99 |
Correct |
110 ms |
70244 KB |
Output is correct |
100 |
Correct |
16 ms |
55384 KB |
Output is correct |
101 |
Correct |
111 ms |
68492 KB |
Output is correct |
102 |
Correct |
180 ms |
80784 KB |
Output is correct |
103 |
Correct |
17 ms |
55536 KB |
Output is correct |
104 |
Correct |
18 ms |
55640 KB |
Output is correct |
105 |
Correct |
17 ms |
55388 KB |
Output is correct |
106 |
Correct |
16 ms |
55388 KB |
Output is correct |
107 |
Correct |
16 ms |
55384 KB |
Output is correct |
108 |
Correct |
668 ms |
122456 KB |
Output is correct |
109 |
Correct |
962 ms |
130620 KB |
Output is correct |
110 |
Correct |
978 ms |
137328 KB |
Output is correct |
111 |
Correct |
1296 ms |
193548 KB |
Output is correct |
112 |
Correct |
1384 ms |
169052 KB |
Output is correct |
113 |
Correct |
1089 ms |
173800 KB |
Output is correct |
114 |
Correct |
851 ms |
120104 KB |
Output is correct |
115 |
Correct |
825 ms |
120528 KB |
Output is correct |
116 |
Correct |
955 ms |
125028 KB |
Output is correct |
117 |
Correct |
1261 ms |
182880 KB |
Output is correct |
118 |
Correct |
822 ms |
121752 KB |
Output is correct |
119 |
Correct |
1288 ms |
183116 KB |
Output is correct |
120 |
Correct |
1275 ms |
191276 KB |
Output is correct |
121 |
Correct |
1278 ms |
183200 KB |
Output is correct |
122 |
Correct |
1071 ms |
138200 KB |
Output is correct |
123 |
Correct |
1339 ms |
184536 KB |
Output is correct |
124 |
Correct |
1077 ms |
164992 KB |
Output is correct |