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>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
const int maxn = 120;
int m, n;
int dist0[maxn];
int distu[maxn];
int add[maxn], bran[maxn];
map<int, int> cnt;
map<int, vector<int> > faker;
map<int, int> on;
int hub;
bool isSame(int u, int v)
{
if(u == -1 || v == -1) return false;
return getDistance(u, v) != distu[u]+distu[v]-2*hub;
}
vector<int> gon;
vector<int> buck;
void proc(int x)
{
if(gon.empty() || !isSame(gon.back(), x))
{
gon.push_back(x);
if(!buck.empty())
{
gon.pb(buck.back());
buck.pop_back();
}
}
else
{
buck.pb(x);
}
}
int hubDistance(int N, int sub)
{
gon.clear();
buck.clear();
memset(add, 0, sizeof add);
memset(bran, 0, sizeof bran);
cnt.clear();
faker.clear();
on.clear();
n = N;
int best = 0, dat = -1;
for(int i = 1; i< N; i++)
{
dist0[i] = getDistance(0, i);
if(dist0[i]> best)
{
best = dist0[i];
dat = i;
}
}
int U = dat;
best = 0; dat = -1;
for(int i = 0; i< N; i++)
{
if(i == U) continue;
if(i == 0)
{
distu[0] = dist0[U];
}
distu[i] = getDistance(U, i);
if(distu[i]> best)
{
best = distu[i];
dat = i;
}
}
int V = dat;
int diam = distu[V];
// printf("(%d->%d) %d\n", U, V, dist[V]);
distu[0] = dist0[U];
for(int i = 0; i< N; i++)
{
if(i == U || i == 0) continue;
int br = dist0[i]+distu[i]-dist0[U];
br /= 2;
int pos = distu[i]-br;
add[i] = pos;
bran[i] = br;
cnt[pos]++;
faker[pos].pb(i);
}
int rad = diam;
bool good = false;
int cm = 1;
for(auto kk : cnt)
{
int x = kk.X;
rad = min(rad, max(x, diam-x));
}
if(sub == 2) return rad;
vector<int> hubs;
for(auto kk : cnt)
{
int x = kk.X, y = kk.Y;
if(rad == max(x, diam-x))
{
hubs.pb(x);
if(max(max(cm, cnt[x]), n-cm-cnt[x]) <= n/2) good = true;
}
cm += cnt[x];
on[x] = cm;
}
vector<int> doubt;
while(!hubs.empty())
{
int u = hubs.back(); hubs.pop_back();
if(max(on[u]-cnt[u], n-on[u])> n/2) continue;
if(max(cnt[u], max(on[u]-cnt[u], n-on[u]))<= n/2) return rad;
doubt.pb(u);
}
if(doubt.empty()) return -rad;
hub = doubt[0];
int tweet = n-cnt[hub];
while(tweet--) proc(1);
for(int i = 0; i< (int) faker[hub].size(); i++) proc(faker[hub][i]);
int T = gon.back(); gon.pop_back();
while(!gon.empty())
{
int S = gon.back();
if(isSame(S, T))
{
if(gon.size() == 1)
{
buck.pb(gon[0]); gon.pop_back();
}
else
{
gon.pop_back(); gon.pop_back();
}
}
else
{
gon.pop_back();
if(buck.empty()) return rad;
buck.pop_back();
}
}
if(buck.empty()) return rad;
return -rad;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:115:17: warning: unused variable 'y' [-Wunused-variable]
int x = kk.X, y = kk.Y;
^
towns.cpp:104:7: warning: variable 'good' set but not used [-Wunused-but-set-variable]
bool good = false;
^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |