#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){
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];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12084 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
6 ms |
12120 KB |
Output is correct |
10 |
Correct |
6 ms |
12068 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12044 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
6 ms |
12084 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
6 ms |
12084 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12084 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
6 ms |
12120 KB |
Output is correct |
10 |
Correct |
6 ms |
12068 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12044 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
6 ms |
12084 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
6 ms |
12084 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12084 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
6 ms |
12120 KB |
Output is correct |
10 |
Correct |
6 ms |
12068 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12044 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
6 ms |
12084 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
6 ms |
12084 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
25460 KB |
Output is correct |
2 |
Incorrect |
86 ms |
26380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12084 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
6 ms |
12120 KB |
Output is correct |
10 |
Correct |
6 ms |
12068 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12044 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
6 ms |
12084 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
6 ms |
12084 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |