# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
105209 | CaroLinda | Towns (IOI15_towns) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include "towns.h"
#include <bits/stdc++.h>
#define lp(i,a,b) for(int i = a ; i < b ; i++)
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define MAXN 120
const int inf = 1e9 + 5 ;
using namespace std;
//
int N , A , B , R , man;
int perp[MAXN] ;
int v[] = {0,0,0} ;
int d[MAXN][MAXN] ;
bool marc[MAXN][MAXN] ;
//
//union-find stuff
int pai[MAXN] , h[MAXN];
void init()
{
memset( v , 0 , sizeof v ) ;
lp(i,0,N) pai[i] = i ;
memset( h , 0 , sizeof h ) ;
memset(marc , 0 , sizeof marc ) ;
}
void query( int x )
{
lp(i,0,N) {
d[x][i] = d[i][x] = getDistance(x,i) ;
marc[x][i] = marc[i][x] = 1 ;
}
}
int findMax( int x )
{ return max_element(d[x] , d[x] + N) - d[x] ; }
int findPerpendicular( int x )
{ return (d[A][x] + d[B][x] - d[A][B])/2 ; }
int findRadio()
{
int ans = inf ;
lp(i,0,N)
{
int k = max( d[A][i] , d[B][i] ) - perp[i] ;
int Man = d[A][i] > d[B][i] ? A : B ;
if( k < ans ) ans = k , man = Man ;
}
return ans ;
}
int checkPosition( int x )
{
int k = d[man][x] - perp[x] ;
if( k < R ) return 0 ;
if( k == R ) return 1 ;
return 2 ;
}
int find(int x)
{
if(pai[x] == x) return x ;
return pai[x] = find( pai[x] ) ;
}
void join( int x, int y )
{
x = find(x) ;
y = find(y) ;
if(x == y) return ;
pai[y] = x;
}
int sub4()
{
lp(i,0,N)
v[ checkPosition(i) ] ++ ;
lp(i,0,3)
if( v[i] > N/2 ) return -R ;
return R ;
}
int sub3()
{
vector<int> good_vector ;
lp(i,0,N)
if( checkPosition(i) == 1 ) good_vector.pb(i) ;
int tam = good_vector.size() ;
lp(i,0,N)
lp(j,0,N)
if(!marc[i][j])
{
d[i][j] = d[j][i] = getDistance(i,j) ;
marc[i][j] = marc[j][i] = 1 ;
}
lp(i,0,tam)
lp(j,0,tam)
{
int x = good_vector[i] , y = good_vector[j] ;
if( x == y ) continue ;
if( d[A][B] + d[x][y] != d[A][x] + d[B][y] ) join( x , y ) ;
}
lp(i,0,N) h[ find(i) ] ++ ;
lp(i,0,N)
if( h[i] > N/2 ) return - R ;
sub4() ;
if( v[0] > N/2 or v[2] > N/2 ) return -R ;
return R ;
}
int hubDistance( int n , int sub)
{
N = n ;
init() ;
query(0) ;
A = findMax(0) ;
query(A) ;
B = findMax(A) ;
query(B) ;
lp(i,0,n) perp[i] = findPerpendicular(i) ;
R = findRadio() ;
if( sub < 3 ) return R ;
if( sub == 3 ) return sub3() ;
if( sub == 4 ) return sub4() ;
}