# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
682741 | MilosMilutinovic | Towns (IOI15_towns) | C++14 | 14 ms | 388 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>
using namespace std;
int hubDistance(int n, int sub) {
vector<vector<int>> w(n, vector<int>(n, -1));
auto Ask = [&](int i, int j) {
if (w[i][j] == -1) {
w[i][j] = w[j][i] = getDistance(i, j);
}
return w[i][j];
};
int v, u;
{
vector<int> dist(n);
for (int i = 1; i < n; i++) {
dist[i] = Ask(0, i);
}
v = max_element(dist.begin(), dist.end()) - dist.begin();
}
{
vector<int> dist(n);
for (int i = 0; i < n; i++) {
dist[i] = Ask(i, v);
}
u = max_element(dist.begin(), dist.end()) - dist.begin();
}
int diam = Ask(v, u);
int res = diam;
vector<int> f;
vector<int> d(n);
for (int i = 0; i < n; i++) {
if (u == i || v == i) {
continue;
}
int d_vi = Ask(v, i);
int d_ui = Ask(u, i);
int diff = d_vi - d_ui;
// dv + du = diam
// dv - du = diff
int dv = (diam + diff) / 2;
int du = diam - dv;
d[i] = d_vi - dv;
res = min(res, max(dv, du));
f.push_back(dv);
}
vector<int> sz(n, 1);
vector<int> par(n);
iota(par.begin(), par.end(), 0);
function<int(int)> GetRoot = [&](int x) {
return par[x] == x ? x : par[x] = GetRoot(par[x]);
};
auto Merge = [&](int x, int y) {
x = GetRoot(x);
y = GetRoot(y);
if (x != y) {
sz[x] += sz[y];
par[y] = x;
}
};
for (int i = 0; i < n; i++) {
if (i == u || i == v) {
continue;
}
for (int j = i + 1; j < n; j++) {
if (j == u || j == v) {
continue;
}
if (GetRoot(i) != i || GetRoot(j) != j) {
continue;
}
if (d[i] + d[j] > Ask(i, j)) {
Merge(i, j);
}
}
}
sort(f.begin(), f.end());
f.erase(unique(f.begin(), f.end()));
int m = (int) f.size();
vector<vector<int>> ch(m);
for (int i = 0; i < n; i++) {
if (i == u || i == v || i != GetRoot(i)) {
continue;
}
int idx = lower_bound(f.begin(), f.end(), Ask(i, v) - d[i]) - f.begin();
ch[idx].push_back(sz[i]);
}
int L = 1, R = n - 1;
for (int i = 0; i < m; i++) {
for (int x : ch[i]) {
R -= x;
}
if (res == f[i]) {
bool ok = true;
if (L > n / 2) {
ok = false;
}
if (R > n / 2) {
ok = false;
}
for (int x : ch[i]) {
if (x > n / 2) {
ok = false;
}
}
if (ok) {
return res;
}
}
for (int x : ch[i]) {
L += x;
}
}
return -res;
}
/*
1 1
11
0 17 18 20 17 12 20 16 23 20 11
17 0 23 25 22 17 25 21 28 25 16
18 23 0 12 21 16 24 20 27 24 17
20 25 12 0 23 18 26 22 29 26 19
17 22 21 23 0 9 21 17 26 23 16
12 17 16 18 9 0 16 12 21 18 11
20 25 24 26 21 16 0 10 29 26 19
16 21 20 22 17 12 10 0 25 22 15
23 28 27 29 26 21 29 25 0 21 22
20 25 24 26 23 18 26 22 21 0 19
11 16 17 19 16 11 19 15 22 19 0
17
*/
Compilation message (stderr)
# | 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... |