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;
const int maxN = 1e2 + 20;
int memo[maxN][maxN];
int depth[maxN];
int dist(int u, int v) {
if (memo[u][v] != -1) {
return memo[u][v];
}
else {
return memo[u][v] = memo[v][u] = getDistance(u, v);
}
}
bool equal(int u, int v) {
if (u == v) {
return true;
}
else if (u == -1 || v == -1) {
return false;
}
else {
return depth[u] + depth[v] != dist(u, v);
}
}
bool check(vector<int> comp) {
vector<int> bucket;
vector<int> list;
for (auto u: comp) {
if (list.empty() || !equal(list.back(), u)) {
list.push_back(u);
if (!bucket.empty()) {
list.push_back(bucket.back());
bucket.pop_back();
}
}
else {
bucket.push_back(u);
}
}
int u = list.back();
while (!list.empty()) {
int v = list.back();
list.pop_back();
if (equal(u, v)) {
if (!list.empty()) {
list.pop_back();
}
else {
bucket.push_back(v);
}
}
else {
if (bucket.empty()) {
return true;
}
bucket.pop_back();
}
}
return bucket.empty();
}
int hubDistance(int N, int sub) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
memo[i][j] = (i == j ? 0 : -1);
}
}
int X = 0;
for (int i = 0; i < N; i++) {
if (dist(0, i) > dist(0, X)) {
X = i;
}
}
int Y = 0;
for (int i = 0; i < N; i++) {
if (dist(X, i) > dist(X, Y)) {
Y = i;
}
}
int R = dist(X, Y);
map<int, vector<int>> comps;
for (int i = 0; i < N; i++) {
depth[i] = (dist(X, i) + dist(0, i) - dist(X, 0)) / 2;
int left = dist(X, i) - depth[i];
int right = dist(X, Y) - left;
comps[left].push_back(i);
R = min(R, max(left, right));
}
int pref = 0;
int suf = N;
for (auto p: comps) {
suf -= (int)p.second.size();
if (max(p.first, dist(X, Y) - p.first) == R && pref <= N / 2 && suf <= N / 2) {
if ((int)p.second.size() <= N / 2) {
return R;
}
else {
p.second.resize(N, -1);
if (check(p.second)) {
return R;
}
}
}
pref += (int)p.second.size();
}
return -R;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:67:28: warning: unused parameter 'sub' [-Wunused-parameter]
67 | int hubDistance(int N, int sub) {
| ~~~~^~~
# | 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... |