/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define mk make_pair
typedef long long ll;
const int N=5e5+7,lg=21;
int n,k,st[N],fn[N],t,s[N],mark[N],sum[N],ans,h[N],mn[N],sp[N][lg],P[N];
vector<int>vec[N],ver[N],x;
int lca(){
int m=N,g;
for(auto u:x){
if(h[u]<m){
m=h[u];
g=u;
}
}
for(int i=lg-1;i>=0;i--){
for(int j=0;j<x.size();j++){
int u=x[j];
if(h[sp[u][i]]>=m){
x[j]=sp[u][i];
}
}
}
int f=1;
for(auto u:x){
if(u!=g)f=0;
}
if(f)return g;
for(int i=lg-1;i>=0;i--){
f=1;
for(int j=1;j<x.size();j++){
if(sp[x[j]][i]==sp[x[0]][i])f=0;
}
if(f){
for(int j=0;j<x.size();j++){
x[j]=sp[x[j]][i];
}
}
}
return sp[x[0]][0];
}
void dfs1(int v=1,int p=0){
h[v]=h[p]+1;
sp[v][0]=p;
st[v]=t++;
for(auto u:vec[v]){
if(u!=p) dfs1(u,v);
}
fn[v]=t++;
}
void dfs2(int v=1,int p=0){
mn[v]=h[P[s[v]]];
for(auto u:vec[v]){
if(u==p)continue;
dfs2(u,v);
sum[v]+=sum[u];
mn[v]=min(mn[v],mn[u]);
}
if(v!=1 && h[v]<=mn[v])mark[v]=1;
sum[v]+=mark[v];
}
void dfs3(int v=1,int p=0){
if(mark[v] && (sum[v]==1 || sum[v]==sum[1]))ans++;
for(auto u:vec[v]){
if(u==p)continue;
dfs3(u,v);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>k;
int u,v;
for(int i=0;i<n-1;i++){
cin>>u>>v;
vec[u].pb(v);
vec[v].pb(u);
}
for(int i=1;i<=n;i++){
cin>>s[i];
ver[s[i]].pb(i);
}
dfs1();
for(int j=1;j<lg;j++){
for(int i=1;i<=n;i++){
sp[i][j]=sp[sp[i][j-1]][j-1];
}
}
for(int i=1;i<=k;i++){
x.clear();
for(auto u:ver[i])x.pb(u);
P[i]=lca();
}
dfs2();
dfs3();
cout<<ans/2+(ans%2==1);
}
Compilation message
mergers.cpp: In function 'int lca()':
mergers.cpp:25:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int j=0;j<x.size();j++){
| ~^~~~~~~~~
mergers.cpp:39:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int j=1;j<x.size();j++){
| ~^~~~~~~~~
mergers.cpp:43:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int j=0;j<x.size();j++){
| ~^~~~~~~~~
mergers.cpp:17:10: warning: 'g' may be used uninitialized in this function [-Wmaybe-uninitialized]
17 | int m=N,g;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
13 ms |
23924 KB |
Output is correct |
5 |
Correct |
12 ms |
23852 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23832 KB |
Output is correct |
8 |
Correct |
13 ms |
23820 KB |
Output is correct |
9 |
Correct |
13 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23760 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23808 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23808 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23808 KB |
Output is correct |
21 |
Correct |
13 ms |
23860 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
13 ms |
23924 KB |
Output is correct |
5 |
Correct |
12 ms |
23852 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23832 KB |
Output is correct |
8 |
Correct |
13 ms |
23820 KB |
Output is correct |
9 |
Correct |
13 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23760 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23808 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23808 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23808 KB |
Output is correct |
21 |
Correct |
13 ms |
23860 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23760 KB |
Output is correct |
25 |
Correct |
13 ms |
23808 KB |
Output is correct |
26 |
Correct |
14 ms |
24404 KB |
Output is correct |
27 |
Correct |
13 ms |
24336 KB |
Output is correct |
28 |
Correct |
13 ms |
24532 KB |
Output is correct |
29 |
Correct |
14 ms |
24424 KB |
Output is correct |
30 |
Correct |
14 ms |
24332 KB |
Output is correct |
31 |
Correct |
11 ms |
23812 KB |
Output is correct |
32 |
Correct |
13 ms |
24532 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24336 KB |
Output is correct |
35 |
Correct |
13 ms |
24404 KB |
Output is correct |
36 |
Correct |
13 ms |
24328 KB |
Output is correct |
37 |
Incorrect |
13 ms |
24268 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
13 ms |
23924 KB |
Output is correct |
5 |
Correct |
12 ms |
23852 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23832 KB |
Output is correct |
8 |
Correct |
13 ms |
23820 KB |
Output is correct |
9 |
Correct |
13 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23760 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23808 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23808 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23808 KB |
Output is correct |
21 |
Correct |
13 ms |
23860 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23760 KB |
Output is correct |
25 |
Correct |
11 ms |
23892 KB |
Output is correct |
26 |
Correct |
59 ms |
39836 KB |
Output is correct |
27 |
Correct |
82 ms |
39516 KB |
Output is correct |
28 |
Correct |
13 ms |
24276 KB |
Output is correct |
29 |
Correct |
11 ms |
23764 KB |
Output is correct |
30 |
Correct |
11 ms |
23816 KB |
Output is correct |
31 |
Correct |
72 ms |
39604 KB |
Output is correct |
32 |
Correct |
14 ms |
24264 KB |
Output is correct |
33 |
Correct |
94 ms |
46780 KB |
Output is correct |
34 |
Correct |
74 ms |
39660 KB |
Output is correct |
35 |
Correct |
13 ms |
24332 KB |
Output is correct |
36 |
Correct |
85 ms |
40536 KB |
Output is correct |
37 |
Correct |
13 ms |
24284 KB |
Output is correct |
38 |
Correct |
13 ms |
24276 KB |
Output is correct |
39 |
Correct |
58 ms |
39872 KB |
Output is correct |
40 |
Correct |
13 ms |
24512 KB |
Output is correct |
41 |
Correct |
69 ms |
39824 KB |
Output is correct |
42 |
Correct |
92 ms |
41848 KB |
Output is correct |
43 |
Correct |
12 ms |
23852 KB |
Output is correct |
44 |
Correct |
89 ms |
47328 KB |
Output is correct |
45 |
Correct |
88 ms |
43352 KB |
Output is correct |
46 |
Correct |
13 ms |
24340 KB |
Output is correct |
47 |
Correct |
14 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
39800 KB |
Output is correct |
2 |
Correct |
74 ms |
43532 KB |
Output is correct |
3 |
Correct |
14 ms |
24404 KB |
Output is correct |
4 |
Correct |
14 ms |
24276 KB |
Output is correct |
5 |
Correct |
15 ms |
23856 KB |
Output is correct |
6 |
Correct |
15 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
24252 KB |
Output is correct |
8 |
Incorrect |
84 ms |
41496 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
13 ms |
23924 KB |
Output is correct |
5 |
Correct |
12 ms |
23852 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23832 KB |
Output is correct |
8 |
Correct |
13 ms |
23820 KB |
Output is correct |
9 |
Correct |
13 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23760 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23884 KB |
Output is correct |
13 |
Correct |
12 ms |
23808 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23816 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23808 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23808 KB |
Output is correct |
21 |
Correct |
13 ms |
23860 KB |
Output is correct |
22 |
Correct |
11 ms |
23812 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23760 KB |
Output is correct |
25 |
Correct |
13 ms |
23808 KB |
Output is correct |
26 |
Correct |
14 ms |
24404 KB |
Output is correct |
27 |
Correct |
13 ms |
24336 KB |
Output is correct |
28 |
Correct |
13 ms |
24532 KB |
Output is correct |
29 |
Correct |
14 ms |
24424 KB |
Output is correct |
30 |
Correct |
14 ms |
24332 KB |
Output is correct |
31 |
Correct |
11 ms |
23812 KB |
Output is correct |
32 |
Correct |
13 ms |
24532 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24336 KB |
Output is correct |
35 |
Correct |
13 ms |
24404 KB |
Output is correct |
36 |
Correct |
13 ms |
24328 KB |
Output is correct |
37 |
Incorrect |
13 ms |
24268 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |