제출 #1272288

#제출 시각아이디문제언어결과실행 시간메모리
1272288trandangquang수도 (JOI20_capital_city)C++20
0 / 100
315 ms36736 KiB
#include<bits/stdc++.h> using namespace std; #define foru(i,a,b) for(int i=(a); i<=(b); ++i) #define ford(i,a,b) for(int i=(a); i>=(b); --i) #define rep(i,a) for(int i=0; i<(a); ++i) #define sz(a) (int)(a).size() #define all(a) (a).begin(),(a).end() #define bit(s,i) (((s)>>(i))&1) #define ii pair<int,int> #define fi first #define se second #define pb push_back #define eb emplace_back #define ll long long template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;} template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;} const int N=2e5+5; int n,k,cntTot[N],col[N],ans=1e9; vector<int> adj[N]; int sz[N]; bool del[N]; void get_sz(int u, int p){ sz[u]=1; for(int v:adj[u]) if(!del[v] && v!=p){ get_sz(v,u); sz[u]+=sz[v]; } } int centroid(int u, int p, int szA){ for(int v:adj[u]) if(!del[v] && v!=p){ if(sz[v]>szA/2) return centroid(v,u,szA); } return u; } int par[N]; bool visCol[N]; vector<int> lst[N],lstCol; void dfs(int u, int p){ lst[col[u]].eb(u); lstCol.eb(col[u]); par[u]=p; for(int v:adj[u]) if(!del[v] && v!=p){ dfs(v,u); } } void decomp(int u){ get_sz(u,-1); int cen=centroid(u,-1,sz[u]); dfs(cen,-1); vector<int> hi; hi=lst[col[u]]; if(sz(hi) == cntTot[col[u]]){ bool chk=1; int cnt=0; visCol[col[u]]=true; while(hi.size()){ int p=par[hi.back()]; hi.pop_back(); if(p!=-1){ if(!visCol[col[p]]){ visCol[col[p]]=1; ++cnt; if(sz(lst[col[p]]) < cntTot[col[p]]){ chk=0; break; } for(int i:lst[col[p]]){ hi.eb(i); } } } } if(chk) mini(ans, cnt); } for(int i:lstCol){ lst[i].clear(); visCol[i]=0; } lstCol.clear(); del[cen]=true; for(int v:adj[cen]) if(!del[v]) decomp(v); } void solve(){ cin>>n>>k; rep(i,n-1){ int u,v; cin>>u>>v; adj[u].eb(v); adj[v].eb(u); } foru(i,1,n) cin>>col[i], cntTot[col[i]]++; decomp(1); cout<<ans<<'\n'; } int32_t main(){ #define task "test" if(fopen(task".inp","r")){ freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } cin.tie(0)->sync_with_stdio(0); int tc=1; //cin>>tc; rep(_,tc){ solve(); } }

컴파일 시 표준 에러 (stderr) 메시지

capital_city.cpp: In function 'int32_t main()':
capital_city.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...