Submission #402228

#TimeUsernameProblemLanguageResultExecution timeMemory
402228kshitij_sodaniTowns (IOI15_towns)C++14
35 / 100
22 ms460 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];
/*void solve(int l,int r){
	for(int i=0;i<2*n;i++){
		adj[i].clear();
		val[i]=0;
	}
	adj[l].pb({r,dist[l][r]});
	adj[r].pb({l,dist[l][r]});
	int ind2=n;
	int qq=-1;
	for(int i=0;i<n;i++){
		int mi=-1;
		if(l==i or r==i){
			continue;
		}
		pair<int,int> cur={l,r};

		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});
		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++;
		}
	}
	for(int i=n;i<ind2;i++){
		maa=0;
		kk=1;
		dfs2(i);
		if(qq!=-1){
			kk=-1;
		}

		//if(r>1){
			if(maa<ans){
				ans=maa;
				st=kk;
			}
			else if(maa==ans){
				st=max(st,kk);
			}
		//}
		//ans=min(ans,ma);
	}
 
 
 
 
 
}*/
pair<int,int> ma={-1,-1};
int vis[201];
bool get(int i,int j){
	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{
		return true;
		//cc.pb(aa[i]);
	//	cc.pb(aa[i+1]);
	}
}
vector<int> solve(vector<int> aa){

	if(aa.size()==1){
		return aa;
	}
/*	for(auto j:aa){
		cout<<j<<",";
	}
	cout<<endl;*/
	vector<int> ee2;
	vector<int> ee3;
	int mid=aa.size()/2;
	for(int i=0;i<mid;i++){
		ee2.pb(aa[i]);
	}
	for(int i=mid;i<aa.size();i++){
		ee3.pb(aa[i]);
	}
	vector<int> le=solve(ee2);
	vector<int> re=solve(ee3);
	if(le.size()==0 and re.size()==0){
		return {};
	}
	for(int i=0;i<n;i++){
		vis[i]=0;
	}
	for(auto j:le){
		vis[j]=1;
	}
	for(auto j:re){
		vis[j]=1;
	}
	if(le.size()>0 and re.size()>0){
		if(get(le[0],re[0])){
			for(auto j:re){
				le.pb(j);
			}
			return le;
		}
	}
	if(re.size()){
		vector<int> kk;
		for(auto j:re){
			kk.pb(j);
		}
		for(auto j:le){
			if(vis[j]==0){
				if(get(j,re[0])){
					kk.pb(j);
				}
			}
		}
		if(kk.size()>(aa.size())/2){
			return kk;
		}
	}
	swap(le,re);
	if(re.size()){
		vector<int> kk;
		for(auto j:re){
			kk.pb(j);
		}
		for(auto j:le){
			if(vis[j]==0){
				if(get(j,re[0])){
					kk.pb(j);
				}
			}
		}
		if(kk.size()>(aa.size())/2){
			return kk;
		}
	}



	return {};







	vector<vector<int>> kk;
	for(auto j:aa){
		int ind2=-1;
		int ind3=-1;
		for(auto i:kk){
			ind2++;
			if(get(i.back(),j)){
				ind3=ind2;
			}
		}
		if(ind3==-1){
			kk.pb({j});
		}
		else{
			kk[ind3].pb(j);
		}
	}
	for(auto j:kk){
		if(j.size()>aa.size()/2){
			return j;
		}
	}
	return {};
	if(aa.size()==1){
		return aa;
	}
	vector<pair<int,int>> bb;
	vector<int> cc;

	for(int i=0;i+1<aa.size();i+=2){
		if(get(aa[i],aa[i+1])){
			bb.pb({aa[i],aa[i+1]});
		}
		else{
			cc.pb(aa[i]);
			cc.pb(aa[i+1]);
		}
	}
	/*if(aa.size(_%2==1){

	}*/
	
	vector<int> dd;
	for(auto j:bb){
		dd.pb(j.a);
	}
	if(aa.size()%2==1){
		
		dd.pb(aa.back());
	}
	if(dd.size()==0){
		return {};
	}
	vector<int> ee=solve(dd);

	if(ee.size()==0){
		return ee;
	}

	for(int i=0;i<n;i++){
		vis[i]=0;
	}
	for(auto j:ee){
		vis[j]=1;
	}
	for(auto j:bb){
		if(vis[j.a]){
			ee.pb(j.b);
		}
	}
	for(auto j:cc){
		if(get(ee[0],j)){
			ee.pb(j);
		}
	}
	if(ee.size()*2<=aa.size()){
		return {};
	}
	return ee;
}
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();
	}
	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});
		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<<".";
		}
		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;
		if(ss.size()>(n/2)){
			vector<int> xx=solve(ss);
			if(xx.size()>(n/2)){
				kk=-1;
			}
		/*	for(auto j:ss){
				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 'std::vector<int> solve(std::vector<int>)':
towns.cpp:186:19: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  186 |  int mid=aa.size()/2;
      |          ~~~~~~~~~^~
towns.cpp:190:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  190 |  for(int i=mid;i<aa.size();i++){
      |                ~^~~~~~~~~~
towns.cpp:216:15: warning: declaration of 'kk' shadows a global declaration [-Wshadow]
  216 |   vector<int> kk;
      |               ^~
towns.cpp:32:5: note: shadowed declaration is here
   32 | int kk=0;
      |     ^~
towns.cpp:233:15: warning: declaration of 'kk' shadows a global declaration [-Wshadow]
  233 |   vector<int> kk;
      |               ^~
towns.cpp:32:5: note: shadowed declaration is here
   32 | int kk=0;
      |     ^~
towns.cpp:259:22: warning: declaration of 'kk' shadows a global declaration [-Wshadow]
  259 |  vector<vector<int>> kk;
      |                      ^~
towns.cpp:32:5: note: shadowed declaration is here
   32 | int kk=0;
      |     ^~
towns.cpp:288:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  288 |  for(int i=0;i+1<aa.size();i+=2){
      |              ~~~^~~~~~~~~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:396: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]
  396 |   while(ind+1<pp2.size()){
      |         ~~~~~^~~~~~~~~~~
towns.cpp:415:18: warning: declaration of 'kk' shadows a global declaration [-Wshadow]
  415 |    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:370:7: warning: unused variable 'mi' [-Wunused-variable]
  370 |   int mi=-1;
      |       ^~
towns.cpp:457:15: warning: declaration of 'ss' shadows a global declaration [-Wshadow]
  457 |   vector<int> ss;
      |               ^~
towns.cpp:33:5: note: shadowed declaration is here
   33 | int ss[301];
      |     ^~
towns.cpp:465:15: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  465 |   if(ss.size()>(n/2)){
      |      ~~~~~~~~~^~~~~~
towns.cpp:467:16: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  467 |    if(xx.size()>(n/2)){
      |       ~~~~~~~~~^~~~~~
towns.cpp:368:6: warning: unused variable 'qq' [-Wunused-variable]
  368 |  int qq=-1;
      |      ^~
towns.cpp:444:6: warning: unused variable 'ind' [-Wunused-variable]
  444 |  int ind=-1;
      |      ^~~
towns.cpp:339:29: warning: unused parameter 'sub' [-Wunused-parameter]
  339 | 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...