Submission #402635

#TimeUsernameProblemLanguageResultExecution timeMemory
402635kshitij_sodaniTowns (IOI15_towns)C++14
100 / 100
25 ms948 KiB
//#pragma GCC optimize("Ofast,unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long llo; #define mp make_pair #define pb push_back #define a first #define b second #define endl '\n' #include "towns.h" int dist[201][201]; vector<pair<int,int>> adj[301]; int pa=-1; int n; vector<pair<int,int>> pp; vector<pair<int,int>> pp2; void dfs(int no,int par=-1){ if(no==pa){ pp2=pp; } for(auto j:adj[no]){ if(j.a!=par){ pp.pb({no,j.b}); dfs(j.a,no); pp.pop_back(); } } } int maa=0; int kk=0; int ss[301]; void dfs2(int no,int par=-1,int lev=0){ ss[no]=0; if(adj[no].size()==1){ ss[no]++; } maa=max(maa,lev); for(auto j:adj[no]){ if(j.a!=par){ dfs2(j.a,no,lev+j.b); ss[no]+=ss[j.a]; if(ss[j.a]>(n/2)){ kk=-1; } } } //cout<<no<<"<"<<ss[no]<<endl; } int ans=1e9; int st=-1; //pair<int,int> ma={-1,-1}; int val[301]; pair<int,int> ma={-1,-1}; int vis[201]; int par[201]; int find(int no){ if(par[no]==no){ return no; } par[no]=find(par[no]); return par[no]; } bool get(int i,int j){ i=find(i); j=find(j); if(i==j){ return true; } int yy; if(dist[i][j]==0){ dist[i][j]=getDistance(i,j); dist[j][i]=dist[i][j]; } yy=dist[i][j]; int xx=dist[ma.b][i]+dist[0][i]-dist[0][ma.b]; xx/=2; int xx2=dist[ma.b][j]+dist[0][j]-dist[0][ma.b]; xx2/=2; if(xx+xx2==yy){ return false; // bb.pb({aa[i],aa[i+1]}); } else{ par[i]=j; return true; //cc.pb(aa[i]); // cc.pb(aa[i+1]); } } int ww=-1; int mm=-1; vector<int> ww2; int lev2[201]; int solve(vector<int> aa,int lev=1){ vector<int> bb; vector<pair<int,int>> yy; for(auto j:aa){ if(bb.size()==0){ bb.pb(j); } else{ if(get(bb.back(),j)==false){ yy.pb({bb.back(),j}); bb.pop_back(); } else{ bb.pb(j); } } } if(bb.size()){ int xx=bb.size(); for(auto j:yy){ if(get(j.a,bb.back())){ xx++; } else if(get(j.b,bb.back())){ xx++; } /*if(j!=bb[0]){ if(get(bb[0],j)){ xx++; } }*/ } return xx; } else{ return 0; } } int hubDistance(int nn, int sub) { n=nn; ma={-1,-1}; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ dist[i][j]=0; dist[j][i]=0; if(i==0){ dist[i][j]=getDistance(i,j); dist[j][i]=dist[i][j]; ma=max(ma,{dist[i][j],j}); } } } pair<int,int> ma2={-1,-1}; for(int i=0;i<n;i++){ if(i!=0 and i!=ma.b){ dist[i][ma.b]=getDistance(i,ma.b); dist[ma.b][i]=dist[i][ma.b]; ma2=max(ma2,{dist[i][ma.b],i}); } } for(int i=0;i<2*n;i++){ adj[i].clear(); } for(int i=0;i<n;i++){ par[i]=i; } adj[0].pb({ma.b,ma.a}); adj[ma.b].pb({0,ma.a}); int ind2=n; int qq=-1; for(int i=0;i<n;i++){ int mi=-1; if(i==0 or i==ma.b){ continue; } pair<int,int> cur={0,ma.b}; /* for(int j=0;j<i;j++){ for(int k=j+1;k<i;k++){ int xx=dist[i][j]+dist[i][k]-dist[j][k]; if(mi==-1){ mi=xx; cur={j,k}; } if(xx<mi){ mi=xx; cur={j,k}; } } }*/ int xx=(dist[cur.a][i]+dist[cur.b][i]-dist[cur.a][cur.b])/2; pa=cur.b; pp.clear(); pp2.clear(); dfs(cur.a); int ind=0; int su=0; pp2.pb({pa,0}); //cout<<xx<<"<<"<<i<<endl; while(ind+1<pp2.size()){ su+=pp2[ind].b; ind++; if(su>=dist[cur.a][i]-xx){ break; } } //cout<<xx<<endl; if(su==dist[cur.a][i]-xx){ ma=max(ma,{xx,i}); adj[pp2[ind].a].pb({i,xx}); /*val[pp2[ind].a]++; if(val[pp2[ind].a]>(n/2)){ qq=pp2[ind].a; }*/ adj[i].pb({pp2[ind].a,xx}); } else{ pair<int,int> kk={pp2[ind-1].a,pp2[ind].a}; su-=pp2[ind-1].b; int le=(dist[cur.a][i]-xx)-su; vector<pair<int,int>> yy; for(auto j:adj[kk.a]){ if(j.a!=kk.b){ yy.pb(j); } } adj[kk.a]=yy; yy.clear(); for(auto j:adj[kk.b]){ if(j.a!=kk.a){ yy.pb(j); } } adj[kk.b]=yy; adj[kk.a].pb({ind2,le}); adj[kk.b].pb({ind2,pp2[ind-1].b-le}); adj[ind2].pb({kk.a,le}); adj[ind2].pb({kk.b,pp2[ind-1].b-le}); adj[ind2].pb({i,xx}); adj[i].pb({ind2,xx}); ind2++; } } ans=1e9; st=-1; int ind=-1; /* cout<<0<<":"<<ma.b<<endl; for(int i=n;i<ind2;i++){ cout<<i<<endl; for(auto j:adj[i]){ cout<<j.a<<"."<<j.b<<" "; } cout<<endl; }*/ for(int i=n;i<ind2;i++){ maa=0; kk=1; dfs2(i); vector<int> ss; for(auto j:adj[i]){ if(j.a!=0 and j.a!=ma.b and j.a<n){ ss.pb(j.a); } } // cout<<i<<":"<<maa<<":"<<kk<<endl; /* for(auto j:ss){ cout<<j<<"."; } cout<<endl;*/ if(ss.size()>(n/2) ){ ww=-1; ww2.clear(); int cot=solve(ss); if(cot>(n/2)){ kk=-1; } /*if(ww!=-1){ int cot=mm; for(auto j:ww2){ if(get(ww,j)){ cot+=lev2[j]; } } cout<<cot<<":"<<endl; if(cot>(n/2)){ kk=-1; } }*/ /*if(xx.size()>(n/2)){ kk=-1; }*/ /*for(auto j:xx){ cout<<j<<","; } cout<<endl;*/ } /*if(qq!=-1){ kk=-1; }*/ /* if(qq!=i){ val[i]=0; }*/ //if(r>1){ if(maa<ans){ ans=maa; st=kk; } else if(maa==ans){ st=max(st,kk); } //} //ans=min(ans,ma); } return ans*st; //return R; }

Compilation message (stderr)

towns.cpp: In function 'int solve(std::vector<int>, int)':
towns.cpp:118:17: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  118 |   int xx=bb.size();
      |          ~~~~~~~^~
towns.cpp:99:30: warning: unused parameter 'lev' [-Wunused-parameter]
   99 | int solve(vector<int> aa,int lev=1){
      |                          ~~~~^~~~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:201:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  201 |   while(ind+1<pp2.size()){
      |         ~~~~~^~~~~~~~~~~
towns.cpp:220:18: warning: declaration of 'kk' shadows a global declaration [-Wshadow]
  220 |    pair<int,int> kk={pp2[ind-1].a,pp2[ind].a};
      |                  ^~
towns.cpp:32:5: note: shadowed declaration is here
   32 | int kk=0;
      |     ^~
towns.cpp:174:7: warning: unused variable 'mi' [-Wunused-variable]
  174 |   int mi=-1;
      |       ^~
towns.cpp:262:15: warning: declaration of 'ss' shadows a global declaration [-Wshadow]
  262 |   vector<int> ss;
      |               ^~
towns.cpp:33:5: note: shadowed declaration is here
   33 | int ss[301];
      |     ^~
towns.cpp:274:15: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  274 |   if(ss.size()>(n/2) ){
      |      ~~~~~~~~~^~~~~~
towns.cpp:171:6: warning: unused variable 'qq' [-Wunused-variable]
  171 |  int qq=-1;
      |      ^~
towns.cpp:249:6: warning: unused variable 'ind' [-Wunused-variable]
  249 |  int ind=-1;
      |      ^~~
towns.cpp:139:29: warning: unused parameter 'sub' [-Wunused-parameter]
  139 | int hubDistance(int nn, int sub) {
      |                         ~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...