#include<bits/stdc++.h>
using namespace std;
const int N=500005;
long long p[N][20],val[N],dep[N],st[N],id;
vector<vector<int>>adj;
void pre_dfs(int v,int par){
p[v][0]=par;
st[v]=id++;
for(int i=1;i<20;i++)p[v][i]=p[p[v][i-1]][i-1];
for(auto&u:adj[v]){
if(u==par)continue;
dep[u]=dep[v]+1;
pre_dfs(u,v);
}
}
int P[N],sz[N];
int f(int x){
return (P[x]==x?x:P[x]=f(P[x]));
}
void join(int a, int b){
a=f(a),b=f(b);
if(a==b)return;
if(sz[a]<sz[b])swap(a, b);
P[b]=a;
sz[a]+=sz[b];
}
void dfs_up(int v,int par){
for(auto&u:adj[v]){
if(u==par)continue;
dfs_up(u,v);
val[v]+=val[u];
}
if(val[v])join(v,par);
}
int LCA(int u,int v){
if(u==v)return u;
if(dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for(int i=__lg(k+1);i>=0;i--){
if(k&(1<<i))u=p[u][i];
}
if(u==v)return u;
for(int i=__lg(dep[u]+1);i>=0;i--){
if(p[u][i]==p[v][i])continue;
u=p[u][i];
v=p[v][i];
}
return p[u][0];
}
int cnt(int v,int par){
int res=0;
for(auto&u:adj[v]){
if(u==par)continue;
res+=cnt(u,v);
}
return max(res,(val[v]==0?1:0));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin>>n>>k;
adj.resize(n+1);
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
pre_dfs(1,0);
vector<vector<int>>state(k+1);
for(int i=1;i<=n;i++){
int x;
cin>>x;
state[x].push_back(i);
}
for(int i=1;i<=k;i++){
sort(state[i].begin(),state[i].end(),[](int i,int j){
return st[i]<st[j];
});
int lca=LCA(state[i][0],state[i].back());
for(int j=0;j<state[i].size();j++){
int u=state[i][j];
val[u]++;val[lca]--;
}
}
iota(P,P+n+1,0);
fill(sz,sz+n+1,1);
dfs_up(1,0);
vector<vector<int>>adj2(n+1);
for(int i=1;i<=n;i++){
for(auto&u:adj[i]){
if(f(i)!=f(u))adj2[f(i)].push_back(f(u));
}
}
int res=0;
for(int i=1;i<=n;i++)res+=(adj2[i].size()==1);
cout<<(res+1)/2;
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int j=0;j<state[i].size();j++){
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
336 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
336 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
3 ms |
1500 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
3 ms |
1240 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
1492 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
1236 KB |
Output is correct |
35 |
Correct |
3 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
3 ms |
1236 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
1236 KB |
Output is correct |
40 |
Correct |
2 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
3 ms |
1236 KB |
Output is correct |
43 |
Correct |
3 ms |
1364 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
3 ms |
1364 KB |
Output is correct |
46 |
Correct |
4 ms |
1240 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
2 ms |
1236 KB |
Output is correct |
49 |
Correct |
2 ms |
1492 KB |
Output is correct |
50 |
Correct |
3 ms |
1492 KB |
Output is correct |
51 |
Correct |
3 ms |
1128 KB |
Output is correct |
52 |
Correct |
2 ms |
1236 KB |
Output is correct |
53 |
Correct |
2 ms |
1236 KB |
Output is correct |
54 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
336 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
72 ms |
28720 KB |
Output is correct |
27 |
Correct |
110 ms |
28408 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
98 ms |
28268 KB |
Output is correct |
32 |
Correct |
2 ms |
1236 KB |
Output is correct |
33 |
Correct |
133 ms |
34048 KB |
Output is correct |
34 |
Correct |
93 ms |
28368 KB |
Output is correct |
35 |
Correct |
2 ms |
1112 KB |
Output is correct |
36 |
Correct |
123 ms |
28880 KB |
Output is correct |
37 |
Correct |
2 ms |
1108 KB |
Output is correct |
38 |
Correct |
3 ms |
1116 KB |
Output is correct |
39 |
Correct |
71 ms |
28672 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
129 ms |
28204 KB |
Output is correct |
42 |
Correct |
119 ms |
30028 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
125 ms |
34204 KB |
Output is correct |
45 |
Correct |
115 ms |
31180 KB |
Output is correct |
46 |
Correct |
2 ms |
1244 KB |
Output is correct |
47 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
28228 KB |
Output is correct |
2 |
Correct |
99 ms |
36732 KB |
Output is correct |
3 |
Correct |
3 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
1236 KB |
Output is correct |
8 |
Correct |
114 ms |
30668 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
96 ms |
28132 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
113 ms |
28096 KB |
Output is correct |
13 |
Correct |
124 ms |
30920 KB |
Output is correct |
14 |
Correct |
106 ms |
36092 KB |
Output is correct |
15 |
Correct |
72 ms |
28228 KB |
Output is correct |
16 |
Correct |
2 ms |
1236 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
97 ms |
35480 KB |
Output is correct |
19 |
Correct |
138 ms |
39284 KB |
Output is correct |
20 |
Correct |
3 ms |
1236 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
81 ms |
31112 KB |
Output is correct |
23 |
Correct |
2 ms |
1236 KB |
Output is correct |
24 |
Correct |
122 ms |
28572 KB |
Output is correct |
25 |
Correct |
169 ms |
37996 KB |
Output is correct |
26 |
Correct |
3 ms |
1492 KB |
Output is correct |
27 |
Correct |
3 ms |
1492 KB |
Output is correct |
28 |
Correct |
3 ms |
1364 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
336 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
3 ms |
1500 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
3 ms |
1240 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
1492 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
1236 KB |
Output is correct |
35 |
Correct |
3 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
3 ms |
1236 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
1236 KB |
Output is correct |
40 |
Correct |
2 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
3 ms |
1236 KB |
Output is correct |
43 |
Correct |
3 ms |
1364 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
3 ms |
1364 KB |
Output is correct |
46 |
Correct |
4 ms |
1240 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
2 ms |
1236 KB |
Output is correct |
49 |
Correct |
2 ms |
1492 KB |
Output is correct |
50 |
Correct |
3 ms |
1492 KB |
Output is correct |
51 |
Correct |
3 ms |
1128 KB |
Output is correct |
52 |
Correct |
2 ms |
1236 KB |
Output is correct |
53 |
Correct |
2 ms |
1236 KB |
Output is correct |
54 |
Correct |
2 ms |
1108 KB |
Output is correct |
55 |
Correct |
1 ms |
336 KB |
Output is correct |
56 |
Correct |
72 ms |
28720 KB |
Output is correct |
57 |
Correct |
110 ms |
28408 KB |
Output is correct |
58 |
Correct |
2 ms |
1236 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
98 ms |
28268 KB |
Output is correct |
62 |
Correct |
2 ms |
1236 KB |
Output is correct |
63 |
Correct |
133 ms |
34048 KB |
Output is correct |
64 |
Correct |
93 ms |
28368 KB |
Output is correct |
65 |
Correct |
2 ms |
1112 KB |
Output is correct |
66 |
Correct |
123 ms |
28880 KB |
Output is correct |
67 |
Correct |
2 ms |
1108 KB |
Output is correct |
68 |
Correct |
3 ms |
1116 KB |
Output is correct |
69 |
Correct |
71 ms |
28672 KB |
Output is correct |
70 |
Correct |
3 ms |
1364 KB |
Output is correct |
71 |
Correct |
129 ms |
28204 KB |
Output is correct |
72 |
Correct |
119 ms |
30028 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
125 ms |
34204 KB |
Output is correct |
75 |
Correct |
115 ms |
31180 KB |
Output is correct |
76 |
Correct |
2 ms |
1244 KB |
Output is correct |
77 |
Correct |
2 ms |
1108 KB |
Output is correct |
78 |
Correct |
74 ms |
28228 KB |
Output is correct |
79 |
Correct |
99 ms |
36732 KB |
Output is correct |
80 |
Correct |
3 ms |
1236 KB |
Output is correct |
81 |
Correct |
2 ms |
1236 KB |
Output is correct |
82 |
Correct |
1 ms |
336 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
2 ms |
1236 KB |
Output is correct |
85 |
Correct |
114 ms |
30668 KB |
Output is correct |
86 |
Correct |
2 ms |
1108 KB |
Output is correct |
87 |
Correct |
96 ms |
28132 KB |
Output is correct |
88 |
Correct |
1 ms |
340 KB |
Output is correct |
89 |
Correct |
113 ms |
28096 KB |
Output is correct |
90 |
Correct |
124 ms |
30920 KB |
Output is correct |
91 |
Correct |
106 ms |
36092 KB |
Output is correct |
92 |
Correct |
72 ms |
28228 KB |
Output is correct |
93 |
Correct |
2 ms |
1236 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
97 ms |
35480 KB |
Output is correct |
96 |
Correct |
138 ms |
39284 KB |
Output is correct |
97 |
Correct |
3 ms |
1236 KB |
Output is correct |
98 |
Correct |
0 ms |
340 KB |
Output is correct |
99 |
Correct |
81 ms |
31112 KB |
Output is correct |
100 |
Correct |
2 ms |
1236 KB |
Output is correct |
101 |
Correct |
122 ms |
28572 KB |
Output is correct |
102 |
Correct |
169 ms |
37996 KB |
Output is correct |
103 |
Correct |
3 ms |
1492 KB |
Output is correct |
104 |
Correct |
3 ms |
1492 KB |
Output is correct |
105 |
Correct |
3 ms |
1364 KB |
Output is correct |
106 |
Correct |
2 ms |
1236 KB |
Output is correct |
107 |
Correct |
2 ms |
1248 KB |
Output is correct |
108 |
Correct |
607 ms |
148220 KB |
Output is correct |
109 |
Correct |
965 ms |
150312 KB |
Output is correct |
110 |
Correct |
936 ms |
159220 KB |
Output is correct |
111 |
Correct |
1154 ms |
194108 KB |
Output is correct |
112 |
Correct |
1024 ms |
181256 KB |
Output is correct |
113 |
Correct |
638 ms |
174372 KB |
Output is correct |
114 |
Correct |
737 ms |
136256 KB |
Output is correct |
115 |
Correct |
744 ms |
136292 KB |
Output is correct |
116 |
Correct |
825 ms |
141300 KB |
Output is correct |
117 |
Correct |
859 ms |
177584 KB |
Output is correct |
118 |
Correct |
753 ms |
134648 KB |
Output is correct |
119 |
Correct |
915 ms |
177572 KB |
Output is correct |
120 |
Correct |
1060 ms |
188160 KB |
Output is correct |
121 |
Correct |
848 ms |
177624 KB |
Output is correct |
122 |
Correct |
920 ms |
153524 KB |
Output is correct |
123 |
Correct |
695 ms |
180508 KB |
Output is correct |
124 |
Correct |
992 ms |
168060 KB |
Output is correct |