#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], sg, 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 (u==0) return 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 (u!=1&&dp[u]==0) ed[u]=1, sg=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]]=lca(l[s[i]], i);
for (int i=1; i<=n; i++) dp[l[s[i]]]--, dp[i]++;
dfs2(1, 1);
if (!sg) return cout<<0, 0;
dfs3(1, 1, 1);
ans+=(t[1]==1);
cout<<(ans+1)/2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21080 KB |
Output is correct |
2 |
Incorrect |
5 ms |
19036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21080 KB |
Output is correct |
2 |
Incorrect |
5 ms |
19036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21080 KB |
Output is correct |
2 |
Incorrect |
5 ms |
19036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
31176 KB |
Output is correct |
2 |
Correct |
52 ms |
33528 KB |
Output is correct |
3 |
Incorrect |
6 ms |
21084 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21080 KB |
Output is correct |
2 |
Incorrect |
5 ms |
19036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |