Submission #486715

#TimeUsernameProblemLanguageResultExecution timeMemory
486715Koosha_mvCapital City (JOI20_capital_city)C++14
100 / 100
848 ms44680 KiB
#include <bits/stdc++.h> using namespace std; #define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl #define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl #define eror(x) cout<<#x<<'='<<(x)<<endl #define f_(i,a,b) for(int i=a;i>=b;i--) #define f(i,a,b) for(int i=a;i<b;i++) #define nb(x) __builtin_popcount(x) #define maxm(a,b) a=max(a,b) #define minm(a,b) a=min(a,b) #define Add(x,y) x=(x+y)%mod #define lst(x) x[x.size()-1] #define sz(x) int(x.size()) #define mp make_pair #define ll long long #define pb push_back #define S second #define F first const int N=2e5+99; int n,t,k,cnt,ans=N,a[N],sz[N],col[N],nxt[N],vis[N],par[N],mark[N],prt[N]; vector<int> v,vc[N],g[N]; void dfs1(int x,int par=0){ sz[x]=1; v.pb(x); f(i,0,g[x].size()){ if(!vis[g[x][i]] && g[x][i]!=par){ dfs1(g[x][i],x); sz[x]+=sz[g[x][i]]; } } } void dfs2(int x){ mark[x]=cnt; f(i,0,g[x].size()){ if(!vis[g[x][i]] && g[x][i]!=par[x]){ par[g[x][i]]=x; dfs2(g[x][i]); } } } void solve(int x){ int mv=0,rt=0,res=0; cnt++; v.clear(); dfs1(x); f(i,0,v.size()){ if(sz[v[i]]>sz[x]/2){ rt=v[i]; } } //cout<<x<<" : "<<rt<<endl; par[rt]=rt; dfs2(rt); vector<int> q; q.pb(rt); mark[rt]=-cnt; f(i,0,q.size()){ int u=q[i],v=u; //eror(u); while(mark[par[v]]==cnt){ v=par[v]; q.pb(v); mark[v]=-cnt; } if(abs(mark[nxt[u]])!=cnt){ mv=1; break; } if(mark[nxt[u]]==cnt){ q.pb(nxt[u]); mark[nxt[u]]=-cnt; } } if(!mv){ f(i,0,q.size()){ if(prt[col[q[i]]]==0){ res++; prt[col[q[i]]]=1; } } f(i,0,q.size()){ prt[col[q[i]]]=0; } minm(ans,res); } vis[rt]=1; f(i,0,g[rt].size()){ //eror(g[rt][i]); if(!vis[g[rt][i]]){ solve(g[rt][i]); } } } int main(){ cin>>n>>k; f(i,1,n){ int u,v; cin>>u>>v; g[u].pb(v); g[v].pb(u); } f(i,1,n+1){ cin>>col[i]; col[i]--; vc[col[i]].pb(i); } f(i,0,k){ f(j,0,vc[i].size()){ nxt[vc[i][j]]=vc[i][(j+1)%vc[i].size()]; } } solve(1); cout<<ans-1; }

Compilation message (stderr)

capital_city.cpp: In function 'void dfs1(int, int)':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   28 |  f(i,0,g[x].size()){
      |    ~~~~~~~~~~~~~~~             
capital_city.cpp:28:2: note: in expansion of macro 'f'
   28 |  f(i,0,g[x].size()){
      |  ^
capital_city.cpp: In function 'void dfs2(int)':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   37 |  f(i,0,g[x].size()){
      |    ~~~~~~~~~~~~~~~             
capital_city.cpp:37:2: note: in expansion of macro 'f'
   37 |  f(i,0,g[x].size()){
      |  ^
capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   49 |  f(i,0,v.size()){
      |    ~~~~~~~~~~~~                
capital_city.cpp:49:2: note: in expansion of macro 'f'
   49 |  f(i,0,v.size()){
      |  ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   61 |  f(i,0,q.size()){
      |    ~~~~~~~~~~~~                
capital_city.cpp:61:2: note: in expansion of macro 'f'
   61 |  f(i,0,q.size()){
      |  ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   79 |   f(i,0,q.size()){
      |     ~~~~~~~~~~~~               
capital_city.cpp:79:3: note: in expansion of macro 'f'
   79 |   f(i,0,q.size()){
      |   ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   85 |   f(i,0,q.size()){
      |     ~~~~~~~~~~~~               
capital_city.cpp:85:3: note: in expansion of macro 'f'
   85 |   f(i,0,q.size()){
      |   ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   92 |  f(i,0,g[rt].size()){
      |    ~~~~~~~~~~~~~~~~            
capital_city.cpp:92:2: note: in expansion of macro 'f'
   92 |  f(i,0,g[rt].size()){
      |  ^
capital_city.cpp: In function 'int main()':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
  114 |   f(j,0,vc[i].size()){
      |     ~~~~~~~~~~~~~~~~           
capital_city.cpp:114:3: note: in expansion of macro 'f'
  114 |   f(j,0,vc[i].size()){
      |   ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...