#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=5e5+9;
int st[maxn][20];
int dep[maxn];
vector<int>a[maxn];
int s[maxn];
int last[maxn];
int anc[maxn];
int in[maxn];
int ans=0,tme=0;
void dfs(int u,int par){
in[u]=++tme;
for (auto v:a[u]){
if (v==par)continue;
dep[v]=dep[u]+1;
st[v][0]=u;
for1(i,1,19)st[v][i]=st[st[v][i-1]][i-1];
dfs(v,u);
}
}
int lca(int u,int v){
if (u==0)return v;
if (v==0)return u;
if (dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for1(i,0,19){
if (k>>i&1)u=st[u][i];
}
if(u==v)return u;
for2(i,19,0){
if (!st[u][i]||!st[v][i])continue;
if (st[u][i]!=st[v][i]){
u=st[u][i];
v=st[v][i];
}
}
return st[u][0];
}
int dp[maxn];
void redfs(int u,int par){
anc[u]=last[s[u]];
for (auto v:a[u]){
if (v==par)continue;
redfs(v,u);
anc[u]=lca(anc[u],anc[v]);
dp[u]+=dp[v];
}
if (anc[u]==u&&u!=1){
if (dp[u]==0||par==1&&sz(a[1])==1){
ans++;
}
dp[u]++;
//cout<<u<<" "<<st[u][0]<<'\n';
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,k;
cin>>n>>k;
for1(i,1,n-1){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
dfs(1,0);
for1(i,1,n){
cin>>s[i];
last[s[i]]=lca(last[s[i]],i);
}
redfs(1,0);
cout<<(ans+1)/2;
//cout<<ans+dp[1];
}
Compilation message
mergers.cpp: In function 'void redfs(int, int)':
mergers.cpp:65:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
65 | if (dp[u]==0||par==1&&sz(a[1])==1){
| ~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12000 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
12088 KB |
Output is correct |
15 |
Correct |
6 ms |
12088 KB |
Output is correct |
16 |
Correct |
6 ms |
12080 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12100 KB |
Output is correct |
19 |
Correct |
6 ms |
12116 KB |
Output is correct |
20 |
Correct |
6 ms |
12116 KB |
Output is correct |
21 |
Correct |
6 ms |
12116 KB |
Output is correct |
22 |
Correct |
8 ms |
12080 KB |
Output is correct |
23 |
Correct |
6 ms |
12084 KB |
Output is correct |
24 |
Correct |
8 ms |
12084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12000 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
12088 KB |
Output is correct |
15 |
Correct |
6 ms |
12088 KB |
Output is correct |
16 |
Correct |
6 ms |
12080 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12100 KB |
Output is correct |
19 |
Correct |
6 ms |
12116 KB |
Output is correct |
20 |
Correct |
6 ms |
12116 KB |
Output is correct |
21 |
Correct |
6 ms |
12116 KB |
Output is correct |
22 |
Correct |
8 ms |
12080 KB |
Output is correct |
23 |
Correct |
6 ms |
12084 KB |
Output is correct |
24 |
Correct |
8 ms |
12084 KB |
Output is correct |
25 |
Correct |
5 ms |
12116 KB |
Output is correct |
26 |
Correct |
7 ms |
12500 KB |
Output is correct |
27 |
Correct |
8 ms |
12476 KB |
Output is correct |
28 |
Correct |
8 ms |
12628 KB |
Output is correct |
29 |
Correct |
9 ms |
12500 KB |
Output is correct |
30 |
Correct |
8 ms |
12500 KB |
Output is correct |
31 |
Correct |
6 ms |
12024 KB |
Output is correct |
32 |
Correct |
8 ms |
12704 KB |
Output is correct |
33 |
Correct |
6 ms |
12116 KB |
Output is correct |
34 |
Correct |
7 ms |
12488 KB |
Output is correct |
35 |
Correct |
9 ms |
12608 KB |
Output is correct |
36 |
Incorrect |
7 ms |
12500 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12000 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
12088 KB |
Output is correct |
15 |
Correct |
6 ms |
12088 KB |
Output is correct |
16 |
Correct |
6 ms |
12080 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12100 KB |
Output is correct |
19 |
Correct |
6 ms |
12116 KB |
Output is correct |
20 |
Correct |
6 ms |
12116 KB |
Output is correct |
21 |
Correct |
6 ms |
12116 KB |
Output is correct |
22 |
Correct |
8 ms |
12080 KB |
Output is correct |
23 |
Correct |
6 ms |
12084 KB |
Output is correct |
24 |
Correct |
8 ms |
12084 KB |
Output is correct |
25 |
Correct |
6 ms |
12116 KB |
Output is correct |
26 |
Correct |
52 ms |
26440 KB |
Output is correct |
27 |
Correct |
99 ms |
26500 KB |
Output is correct |
28 |
Correct |
7 ms |
12464 KB |
Output is correct |
29 |
Correct |
6 ms |
12116 KB |
Output is correct |
30 |
Correct |
6 ms |
12008 KB |
Output is correct |
31 |
Correct |
69 ms |
26464 KB |
Output is correct |
32 |
Correct |
7 ms |
12500 KB |
Output is correct |
33 |
Correct |
102 ms |
33760 KB |
Output is correct |
34 |
Correct |
73 ms |
26456 KB |
Output is correct |
35 |
Incorrect |
9 ms |
12500 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
25008 KB |
Output is correct |
2 |
Correct |
59 ms |
25736 KB |
Output is correct |
3 |
Correct |
8 ms |
12480 KB |
Output is correct |
4 |
Correct |
8 ms |
12460 KB |
Output is correct |
5 |
Correct |
6 ms |
12084 KB |
Output is correct |
6 |
Correct |
6 ms |
12012 KB |
Output is correct |
7 |
Correct |
8 ms |
12524 KB |
Output is correct |
8 |
Correct |
77 ms |
26956 KB |
Output is correct |
9 |
Incorrect |
7 ms |
12500 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12000 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
12088 KB |
Output is correct |
15 |
Correct |
6 ms |
12088 KB |
Output is correct |
16 |
Correct |
6 ms |
12080 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12100 KB |
Output is correct |
19 |
Correct |
6 ms |
12116 KB |
Output is correct |
20 |
Correct |
6 ms |
12116 KB |
Output is correct |
21 |
Correct |
6 ms |
12116 KB |
Output is correct |
22 |
Correct |
8 ms |
12080 KB |
Output is correct |
23 |
Correct |
6 ms |
12084 KB |
Output is correct |
24 |
Correct |
8 ms |
12084 KB |
Output is correct |
25 |
Correct |
5 ms |
12116 KB |
Output is correct |
26 |
Correct |
7 ms |
12500 KB |
Output is correct |
27 |
Correct |
8 ms |
12476 KB |
Output is correct |
28 |
Correct |
8 ms |
12628 KB |
Output is correct |
29 |
Correct |
9 ms |
12500 KB |
Output is correct |
30 |
Correct |
8 ms |
12500 KB |
Output is correct |
31 |
Correct |
6 ms |
12024 KB |
Output is correct |
32 |
Correct |
8 ms |
12704 KB |
Output is correct |
33 |
Correct |
6 ms |
12116 KB |
Output is correct |
34 |
Correct |
7 ms |
12488 KB |
Output is correct |
35 |
Correct |
9 ms |
12608 KB |
Output is correct |
36 |
Incorrect |
7 ms |
12500 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |