This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=2000+5;
const int inf=1e9;
vector<int> adj[mxn],adj2[mxn],adjr[mxn];
bool no[mxn];
int c[mxn];
vector<vector<int>> belong(mxn);
int n,k;
vector<vector<int>> mp(mxn,vector<int>(mxn));
vector<bool> did(mxn);
vector<int> ord;
vector<bool> visited(mxn);
int cnt[mxn];
int scc[mxn];
int in[mxn];
int top[mxn];
void dfs(int v){
visited[v]=true;
for(auto u:adj2[v]){
if(visited[u]) continue;
dfs(u);
}
ord.push_back(v);
}
void dfsr(int v,int id){
visited[v]=true;
scc[v]=id;
cnt[id]++;
for(auto u:adjr[v]){
if(visited[u]) continue;
dfsr(u,id);
}
}
void dfs2(int v,int p=0){
for(auto u:adj[v]){
if(u==p) continue;
dfs2(u,v);
for(int i=0;i<k;i++){
mp[v][i]+=mp[u][i];
}
}
mp[v][c[v]]++;
for(int i=0;i<k;i++){
if(mp[v][i]==(int)belong[i].size() and top[i]==0) top[i]=v;
}
}
void dfs3(int v,int C,int p=0){
int x=c[v];
if(x!=C and !did[x]){
did[x]=true;
for(auto u:belong[x]){
if(no[u]){
adj2[C].push_back(x);
adjr[x].push_back(C);
break;
}
}
}
for(auto u:adj[v]){
if(u==p or no[u]) continue;
dfs3(u,C,v);
}
}
int main() {_
cin>>n>>k;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1;i<=n;i++){
cin>>c[i];
c[i]--;
belong[c[i]].push_back(i);
}
dfs2(1);
for(int i=0;i<k;i++){
for(int j=1;j<=n;j++){
no[j]=false;
if(mp[j][i]==0 or mp[j][i]==mp[1][i] and j!=top[i]){
no[j]=true;
}
}
for(int j=0;j<k;j++){
did[j]=false;
}
/*cout<<i+1<<'\n';
for(int j=1;j<=n;j++){
if(!no[j]) cout<<j<<' ';
}
cout<<'\n';*/
for(int j=1;j<=n;j++){
if(!no[j]){
dfs3(j,i);
break;
}
}
}
for(int i=0;i<k;i++){
if(!visited[i]){
dfs(i);
}
}
reverse(all(ord));
fill(all(visited),false);
int sccnt=0;
for(int i=0;i<k;i++){
if(!visited[ord[i]]){
dfsr(ord[i],sccnt);
sccnt++;
}
}
for(int i=0;i<k;i++){
for(auto u:adj2[i]){
//cout<<i+1<<' '<<u+1<<'\n';
if(scc[i]==scc[u]) continue;
in[scc[i]]++;
}
}
int ans=inf;
for(int i=0;i<sccnt;i++){
if(in[i]==0) ans=min(ans,cnt[i]-1);
}
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
capital_city.cpp: In function 'int main()':
capital_city.cpp:103:50: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
103 | if(mp[j][i]==0 or mp[j][i]==mp[1][i] and j!=top[i]){
capital_city.cpp: In function 'void setIO(std::string)':
capital_city.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |