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];
bool flag[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);
}
}
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(Y, i) - dist(X, Y)) / 2;
int left = dist(X, i) - depth[i];
int right = dist(Y, i) - depth[i];
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;
}
bool ok = true;
for (auto u: p.second) {
if (flag[u]) {
continue;
}
flag[u] = true;
int sz = 0;
deque<int> q;
q.push_back(u);
while (!q.empty()) {
int u = q.front();
q.pop_front();
sz++;
for (auto v: p.second) {
if (!flag[v] && depth[u] + depth[v] != dist(u, v)) {
flag[v] = true;
q.push_back(v);
}
}
}
if (sz > N / 2) {
ok = false;
break;
}
}
if (ok) {
return R;
}
}
pref += (int)p.second.size();
}
return -R;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:64:10: warning: declaration of 'u' shadows a previous local [-Wshadow]
64 | int u = q.front();
| ^
towns.cpp:55:14: note: shadowed declaration is here
55 | for (auto u: p.second) {
| ^
towns.cpp:19:28: warning: unused parameter 'sub' [-Wunused-parameter]
19 | 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... |