# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
943838 | guagua0407 | Capital City (JOI20_capital_city) | C++17 | 262 ms | 36124 KiB |
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=2e5+5;
const int inf=1e9;
vector<int> adj[mxn];
bool visited[mxn];
int sz[mxn];
vector<int> belong[mxn];
bool now[mxn];
int par[mxn];
int c[mxn];
bool did[mxn];
int ans=inf;
vector<int> st;
int dfs(int v,int p=0){
sz[v]=1;
st.push_back(v);
now[v]=true;
par[v]=p;
for(auto u:adj[v]){
if(visited[u] or u==p) continue;
sz[v]+=dfs(u,v);
}
return sz[v];
}
int find(int v,int val,int p=0){
for(auto u:adj[v]){
if(visited[u] or u==p) continue;
if(sz[u]*2>val) return find(u,val,v);
}
return v;
}
void centroid(int v){
v=find(v,dfs(v));
queue<int> q;
vector<int> tmp;
q.push(c[v]);
did[c[v]]=true;
tmp.push_back(c[v]);
bool ok=true;
while(!q.empty()){
int C=q.front();
q.pop();
for(auto x:belong[C]){
if(!now[x]){
ok=false;
break;
}
if(par[x]!=0 and !did[c[par[x]]]){
did[c[par[x]]]=true;
tmp.push_back(c[par[x]]);
q.push(c[par[x]]);
}
}
if(!ok){
break;
}
}
for(auto C:tmp){
did[C]=false;
}
for(auto x:st){
now[x]=false;
}
st.clear();
if(ok){
ans=min(ans,(int)tmp.size()-1);
}
visited[v]=true;
for(auto u:adj[v]){
if(!visited[u]){
centroid(u);
}
}
}
int main() {_
int n,k;
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];
belong[c[i]].push_back(i);
}
centroid(1);
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
# | 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... |