#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
vi adj[555555];
int tot[555555];
int a[555555];
int bad[555555];
int ct;
int act[555555];
void prep(int u, int p=-1)
{
for(int v:adj[u])
{
if(v==p) continue;
prep(v,u);
}
if(act[a[u]]==-1)
{
ct++; act[a[u]]=tot[a[u]];
}
act[a[u]]--;
if(act[a[u]]==0) ct--;
if(ct==0) bad[u]=1;
}
int dp[555555][2];
const int INF = int(1e9);
void dfs(int u, int p=-1)
{
vi pre(2,INF);
pre[0]=0;
for(int v:adj[u])
{
if(v==p) continue;
dfs(v,u);
vi nw(2,INF);
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
if(j==0&&bad[v]) continue;
if(j==0) nw[i]=min(nw[i],pre[i]+dp[v][j]);
else nw[i^1]=min(nw[i^1],pre[i]+dp[v][j]+(i==1?-1:0));
//cerr<<i<<' '<<j<<' '<<nw[0]<<' '<<nw[1]<<'\n';
}
}
pre=nw;
//cerr<<u<<' '<<pre[0]<<' '<<pre[1]<<'\n';
}
dp[u][0]=min(pre[0],pre[1]); dp[u][1]=min(pre[0]+1,pre[1]);
if(bad[u]) dp[u][0]=INF;
//cerr<<"DP : "<<u<<' '<<dp[u][0]<<' '<<dp[u][1]<<'\n';
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,k; cin>>n>>k;
if(n==1){cout<<0<<'\n'; return 0;}
memset(act,-1,sizeof(act));
for(int i=0;i<n-1;i++)
{
int u,v; cin>>u>>v; u--; v--;
adj[u].pb(v); adj[v].pb(u);
}
for(int i=0;i<n;i++)
{
cin>>a[i]; a[i]--;
tot[a[i]]++;
}
prep(0); bad[0]=0;
dfs(0);
cout<<dp[0][0]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
15616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
15616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
15616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
21104 KB |
Output is correct |
2 |
Correct |
103 ms |
21912 KB |
Output is correct |
3 |
Incorrect |
19 ms |
15744 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
15616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |