#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;
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)
{
if (l[s[u]]==0) l[s[u]]=u;
else l[s[u]]=lca(l[s[u]], u);
for (auto v:d[u]) if (v!=p) dfs2(v, u);
}
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);
dfs2(1, 1);
//for (int i=1; i<=k; i++) cout<<i<<' '<<l[i]<<'\n';
cout<<(cnt+1)/2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
16984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
16984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
16984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
30408 KB |
Output is correct |
2 |
Incorrect |
46 ms |
30928 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
16984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |