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 inf = 1e6 + 42;
int n;
vector<vector<int> > storedist;
int dist(int a, int b) {
if(storedist[a][b] != -1) {
return storedist[a][b];
}
return storedist[a][b] = storedist[b][a] = getDistance(a - 1, b - 1);
}
void reset() {
storedist.clear();
storedist.assign(1 + n, vector<int> (1 + n, -1));
for(int i = 1; i <= n; i++) {
storedist[i][i] = 0;
}
}
int hubDistance(int N, int sub) {
n = N;
reset();
int ans = inf;
int X = 1;
for(int i = 2; i <= n; i++) {
if(dist(1, i) > dist(1, X)) {
X = i;
}
}
int Y = X;
for(int i = 1; i <= n; i++) {
if(dist(X, i) > dist(X, Y)) {
Y = i;
}
}
int diam = dist(X, Y);
for(int i = 1; i <= n; i++) {
int a = dist(i, X);
int b = dist(i, Y);
int from = (a + b - diam) / 2;
ans = min(ans, max(a - from, b - from));
}
bool from_X = false, from_Y = false;
for(int i = 1; i <= n; i++) {
if(i == X || i == Y) {
continue;
}
int a = dist(i, X);
int b = dist(i, Y);
int from = (a + b - diam) / 2;
if(a - from == ans) {
from_X = true;
} else if(b - from == ans) {
from_Y = true;
}
}
vector<int> Z = {X, Y};
int idx = 0;
if(from_Y) {
idx = 1;
}
vector<set<int> > s(2);
vector<bool> balanced(2, true);
for(int j = 0; j < 2; j++) {
int less_cnt = 0, more_cnt = 0;
for(int i = 1; i <= n; i++) {
int a = dist(i, X);
int b = dist(i, Y);
int from = (a + b - diam) / 2;
vector<int> d = {a - from, b - from};
if(d[j] == ans) {
s[j].insert(i);
}
if(d[j] < ans) {
less_cnt++;
}
if(d[j] > ans) {
more_cnt++;
}
}
if(less_cnt > n / 2 || more_cnt > n / 2 || s[j].size() > n / 2) {
balanced[j] = false;
}
}
/*if(from_X && from_Y) {
return ans;
}*/
vector<int> di(1 + n, 0);
/*for(int k = 0; k < 2; k++) {
for(int i : s[k]) {
int a = dist(i, X);
int b = dist(i, Y);
int from = (a + b - diam) / 2;
di[i] = from;
}
for(int i : s[k]) {
int cnt = 0;
for(int j : s[k]) {
int dij = dist(i, j);
if(dij < di[i] + di[j]) {
cnt++;
}
}
if(cnt > n / 2) {
balanced[k] = false;
}
}
}*/
if(!from_X) {
balanced[0] = false;
}
if(!from_Y) {
balanced[1] = false;
}
if(!balanced[0] && !balanced[1]) {
return -ans;
}
return ans;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:86:58: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
86 | if(less_cnt > n / 2 || more_cnt > n / 2 || s[j].size() > n / 2) {
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:63:6: warning: variable 'idx' set but not used [-Wunused-but-set-variable]
63 | int idx = 0;
| ^~~
towns.cpp:25:28: warning: unused parameter 'sub' [-Wunused-parameter]
25 | 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... |