Submission #217079

#TimeUsernameProblemLanguageResultExecution timeMemory
217079PajarajaCapital City (JOI20_capital_city)C++17
100 / 100
729 ms34296 KiB
#include <bits/stdc++.h> #define MAXN 200007 using namespace std; int c[MAXN],ccnt[MAXN],p[MAXN],sz,ctr,rez=1000000000,br,n,k; bool vi[MAXN],vc[MAXN],fas,vb[MAXN]; vector<int> g[MAXN],b[MAXN]; queue<int> q; void dfssize(int s,int f) { sz++; vi[s]=false; vb[c[s]]=false; ccnt[c[s]]=0; for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfssize(g[s][i],s); } int fndctr(int s,int f) { int sum=1; bool mb=true; for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) { int x=fndctr(g[s][i],s); if(x>sz/2) mb=false; else sum+=x; } if(sz-sum>sz/2) mb=false; if(mb) ctr=s; return sum; } void dfsctr(int s,int f) { ccnt[c[s]]++; p[s]=f; for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfsctr(g[s][i],s); } void dodajboju(int co) { if(vb[co] || fas) return; if(ccnt[co]!=b[co].size()) {fas=true; return;} vb[co]=true; br++; for(int i=0;i<b[co].size();i++) q.push(b[co][i]); } void dizanje(int s) { if(vi[s] || fas) return; vi[s]=true; dodajboju(c[s]); dizanje(p[s]); } void centrodecomp(int s) { sz=0; dfssize(s,s); fndctr(s,s); int cent=ctr; dfsctr(cent,cent); br=0; vc[cent]=true; fas=false; vi[cent]=true; dodajboju(c[cent]); while(!fas && !q.empty()) { int u=q.front(); dizanje(u); q.pop(); } while(!q.empty()) q.pop(); if(!fas) rez=min(rez,br); for(int i=0;i<g[cent].size();i++) if(!vc[g[cent][i]]) centrodecomp(g[cent][i]); } int main() { scanf("%d%d",&n,&k); for(int i=0;i<n-1;i++) { int t1,t2; scanf("%d%d",&t1,&t2); g[t1].push_back(t2); g[t2].push_back(t1); } for(int i=1;i<=n;i++) scanf("%d",&c[i]); for(int i=1;i<=n;i++) b[c[i]].push_back(i); centrodecomp(1); printf("%d",rez-1); }

Compilation message (stderr)

capital_city.cpp: In function 'void dfssize(int, int)':
capital_city.cpp:11:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfssize(g[s][i],s);
              ~^~~~~~~~~~~~
capital_city.cpp: In function 'int fndctr(int, int)':
capital_city.cpp:17:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++)  if(g[s][i]!=f && !vc[g[s][i]])
              ~^~~~~~~~~~~~
capital_city.cpp: In function 'void dfsctr(int, int)':
capital_city.cpp:31:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfsctr(g[s][i],s);
              ~^~~~~~~~~~~~
capital_city.cpp: In function 'void dodajboju(int)':
capital_city.cpp:36:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(ccnt[co]!=b[co].size()) {fas=true; return;}
     ~~~~~~~~^~~~~~~~~~~~~~
capital_city.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<b[co].size();i++) q.push(b[co][i]);
              ~^~~~~~~~~~~~~
capital_city.cpp: In function 'void centrodecomp(int)':
capital_city.cpp:61:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[cent].size();i++) if(!vc[g[cent][i]]) centrodecomp(g[cent][i]);
              ~^~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&k);
  ~~~~~^~~~~~~~~~~~~~
capital_city.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&t1,&t2);
   ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:73:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;i++) scanf("%d",&c[i]);
                        ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...