This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 200005
using namespace std;
ll ans,ch[N],p[N],siz,center,fi[N],fix[N],ra[N],raod[N],c[N],n,k;
vector<ll>ver,v[N],fer[N];
queue<ll>q;
void calc(ll x,ll par){
ch[x] = 1;
siz++;
for(int i=0; i<v[x].size(); i++)
if(v[x][i] != par && !fix[v[x][i]]){
calc(v[x][i] , x);
ch[x] += ch[v[x][i]];
}
}
void findcenter(ll x,ll par){
if(center)return;
for(int i=0; i<v[x].size(); i++)
if(!fix[v[x][i]] && v[x][i] != par && ch[v[x][i]] >= (siz / 2))
findcenter(v[x][i] , x);
if(!center)center = x;
}
void go(ll x,ll par){
ra[c[x]]++;
fer[c[x]].pb(x);
p[x] = par;
ver.pb(x);
for(int i=0; i<v[x].size(); i++)
if(v[x][i] != par && !fix[v[x][i]])
go(v[x][i] , x);
}
void solve(ll x){
siz = 0;
calc(x , 0);
center = 0;
findcenter(x , 0);
x = center;
ver.clear();
go(x , 0);
while(q.size())q.pop();
for(int i=0; i<ver.size(); i++){
if(c[ver[i]] == c[x]){
fi[ver[i]] = 1;
q.push(ver[i]);
}
}
bool yep = true;
ll val = 0;
while(q.size()){
int t = q.front();
q.pop();
if(ra[c[t]] != raod[c[t]]){
yep = false;
break;
}
if(fi[p[t]] == 0 && p[t] != 0){
val++;
for(int i=0; i<fer[c[p[t]]].size(); i++){
int cur = fer[c[p[t]]][i];
fi[cur] = 1;
q.push(cur);
}
}
}
if(yep){
ans = min(ans , val);
}
for(int i=0; i<ver.size(); i++){
ra[c[ver[i]]] = 0;
fer[c[ver[i]]].clear();
fi[ver[i]] = 0;
}
fix[x] = 1;
for(int i=0; i<v[x].size(); i++)
if(!fix[v[x][i]])
solve(v[x][i]);
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> k;
ans = k;
for(int i=1; i<n; i++){
ll x,y;
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
for(int i=1; i<=n; i++){
cin >> c[i];
raod[c[i]]++;
}
solve(1);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
capital_city.cpp: In function 'void calc(long long int, long long int)':
capital_city.cpp:14:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
capital_city.cpp: In function 'void findcenter(long long int, long long int)':
capital_city.cpp:22:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
capital_city.cpp: In function 'void go(long long int, long long int)':
capital_city.cpp:32:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
capital_city.cpp: In function 'void solve(long long int)':
capital_city.cpp:45:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<ver.size(); i++){
~^~~~~~~~~~~
capital_city.cpp:62:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<fer[c[p[t]]].size(); i++){
~^~~~~~~~~~~~~~~~~~~~
capital_city.cpp:72:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<ver.size(); i++){
~^~~~~~~~~~~
capital_city.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
# | 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... |