#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int ans = 1e9;
ll color[200001];
vi graph[200001];
int sub[200001];
bool odw[200001];
bool was[200001];
bool was_color[200001];
int up_vert[200001];
vl color_ls[200001];
int color_cnt[200001];
vi ls;
void dfs_sub(int v, int pop)
{
up_vert[v] = pop;
ls.pb(v);
sub[v] = 1;
forall(it,graph[v])
{
if(odw[it] || it == pop) continue;
dfs_sub(it,v);
sub[v] += sub[it];
}
}
void centroid(int v, int n)
{
int pop = -1;
dfs_sub(v,v);
while(true)
{
pii best = {-1,-1};
forall(it,graph[v])
{
if(odw[it] || it == pop) continue;
best = max(best,{sub[it],it});
}
if(best.ff > n/2)
{
pop = v;
v = best.ss;
}
else break;
}
odw[v] = 1;
ls = {};
dfs_sub(v,v);
int cur_ans = 0;
queue<int> q;
forall(it,ls)
{
color_cnt[color[it]]++;
if(color[it] == color[v]) q.push(it);
was[it] = 0;
was_color[color[it]] = 0;
}
was[v] = 1;
was_color[color[v]] = 1;
if(color_cnt[color[v]] != siz(color_ls[color[v]])) goto first_time_i_use_this_shit_xd;
while(!q.empty())
{
int t = q.front();
q.pop();
while(!was[t])
{
was[t] = 1;
if(color_cnt[color[t]] != siz(color_ls[color[t]])) goto first_time_i_use_this_shit_xd;
if(!was_color[color[t]])
{
cur_ans++;
forall(it,color_ls[color[t]]) q.push(it);
was_color[color[t]] = 1;
}
t = up_vert[t];
}
}
ans = min(ans,cur_ans);
first_time_i_use_this_shit_xd:
ls = {};
dfs_sub(v,v);
forall(it,ls) color_cnt[color[it]] = 0;
forall(it,graph[v]) if(!odw[it]) centroid(it,sub[it]);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
int n,k;
cin >> n >> k;
rep(i,n-1)
{
int a,b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
rep2(i,1,n) cin >> color[i];
rep2(i,1,n) color_ls[color[i]].pb(i);
centroid(1,n);
cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |