# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
990737 | starchan | Towns (IOI15_towns) | C++17 | 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<bits/stdc++.h>
#include "towns.h"
using namespace std;
#define in array<int, 2>
#define pb push_back
#define pob pop_back
int hubDistance(int n, int sub)
{
in opt = {0, 0}
for(int i = 1; i < n; i++)
opt = max(opt, {getDistance(i, 0), i});
int a = opt[1]; opt = {0, a};
vector<int> d1(n), d2(n); d1[a] = 0;
for(int i = 0; i < n; i++)
{
if(i == a) continue;
opt = max(opt, {d1[i] = getDistance(i, a), i});
}
auto [D, b] = opt; d2[b] = 0;
map<int, vector<int>> diam; diam[0].pb(a); diam[D].pb(b);
for(int i = 0; i < n; i++)
{
if(i == a || i == b)
continue;
d2[i] = getDistance(i, b);
diam[(d1[i]-d2[i]+D)/2].pb(i);
}
int R = 1e9+1;
vector<vector<int>> vi;
vector<int> dist;
for(auto [X, v]: diam)
{
R = min(R, max(X, D-X));
vi.pb(v); dist.pb(X);
}
int N = vi.size();
//0, 1, .., N-1
//has information for stuff on diameter.
int pre = 0;
for(int i = 0; i < N-2; i++)
{
pre+=vi[i].size();
if(R != max(dist[i+1], D-dist[i+1]))
continue;
if(2*pre > n)
break;
if(2*((int)n-pre-vi[i+1].size()) > n)
continue;
if(2*((int)vi[i+1].size()) <= n)
return R;
//Now we need to check if the i+1th thingie, (as we are accessing) is centroid or not.
//This must be done in <= N/2 queries for AC. <= 2N queries is ok too, if we want good partial score.
int g = vi[i+1][0]; int rt = d1[g]-dist[i+1]; int cnt = 0;
for(auto x: vi[i+1])
{
if(x == g)
continue;
int q_rt = d1[x]-dist[i+1];
if((rt+q_rt) == getDistance(x, g))
{
if(cnt)
cnt--;
else
{
cnt = 1; rt = q_rt;
g = x;
}
}
else
cnt++;
}
int SP = 1;
for(auto x: vi[i+1])
{
if(x == g)
continue;
int q_rt = d1[x]-dist[i+1];
if((rt+q_rt) == getDistance(x, g))
SP++;
}
if((2*SP) <= n)
return R;
}
return -R;
}