#include <bits/stdc++.h>
using namespace std;
const int nx=5e5+5, kx=19;
int lvl[nx], pa[nx][kx], n, k, u, v, s[nx], l[nx], cnt, dp[nx], ed[nx], ans, t[nx];
vector<int> d[nx];
void dfs(int u, int p)
{
lvl[u]=lvl[p]+1; pa[u][0]=p;
for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
for (auto v:d[u]) if (v!=p) dfs(v, u);
}
int lca(int u, int v)
{
if (lvl[u]>lvl[v]) swap(u, v);
for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
if (u==v) return u;
for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
return pa[u][0];
}
void dfs2(int u, int p)
{
for (auto v:d[u]) if (v!=p) dfs2(v, u), dp[u]+=dp[v];
if (dp[u]==0) ed[u]=1;
}
void dfs3(int u, int p, int c)
{
for (auto v:d[u])
{
if (v==p) continue;
if (ed[v]) t[u]++, dfs3(v, u, v), ans+=(t[v]==0);
else dfs3(v, u, c);
}
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
for (int i=1; i<=n; i++) cin>>s[i];
dfs(1, 1);
for (int i=1; i<=n; i++) l[s[i]]=i;
for (int i=1; i<=n; i++) l[s[i]]=lca(l[s[i]], i);
for (int i=1; i<=n; i++) dp[l[s[i]]]--, dp[i]++;
dfs2(1, 1);
dfs3(1, 1, 1);
ans+=(t[1]==1);
cout<<(ans+1)/2;
}
/*
8 8
1 2
2 3
2 4
3 5
3 6
4 7
4 8
1 3 4 5 6 7 8
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
23128 KB |
Output is correct |
2 |
Correct |
6 ms |
23128 KB |
Output is correct |
3 |
Correct |
6 ms |
23128 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
5 ms |
23128 KB |
Output is correct |
6 |
Correct |
5 ms |
23128 KB |
Output is correct |
7 |
Correct |
7 ms |
23132 KB |
Output is correct |
8 |
Correct |
6 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23128 KB |
Output is correct |
10 |
Correct |
6 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23160 KB |
Output is correct |
12 |
Incorrect |
7 ms |
23128 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
23128 KB |
Output is correct |
2 |
Correct |
6 ms |
23128 KB |
Output is correct |
3 |
Correct |
6 ms |
23128 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
5 ms |
23128 KB |
Output is correct |
6 |
Correct |
5 ms |
23128 KB |
Output is correct |
7 |
Correct |
7 ms |
23132 KB |
Output is correct |
8 |
Correct |
6 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23128 KB |
Output is correct |
10 |
Correct |
6 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23160 KB |
Output is correct |
12 |
Incorrect |
7 ms |
23128 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
23128 KB |
Output is correct |
2 |
Correct |
6 ms |
23128 KB |
Output is correct |
3 |
Correct |
6 ms |
23128 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
5 ms |
23128 KB |
Output is correct |
6 |
Correct |
5 ms |
23128 KB |
Output is correct |
7 |
Correct |
7 ms |
23132 KB |
Output is correct |
8 |
Correct |
6 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23128 KB |
Output is correct |
10 |
Correct |
6 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23160 KB |
Output is correct |
12 |
Incorrect |
7 ms |
23128 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
35272 KB |
Output is correct |
2 |
Correct |
56 ms |
35276 KB |
Output is correct |
3 |
Incorrect |
7 ms |
23128 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
23128 KB |
Output is correct |
2 |
Correct |
6 ms |
23128 KB |
Output is correct |
3 |
Correct |
6 ms |
23128 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
5 ms |
23128 KB |
Output is correct |
6 |
Correct |
5 ms |
23128 KB |
Output is correct |
7 |
Correct |
7 ms |
23132 KB |
Output is correct |
8 |
Correct |
6 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23128 KB |
Output is correct |
10 |
Correct |
6 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23160 KB |
Output is correct |
12 |
Incorrect |
7 ms |
23128 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |