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<pair<int, int>> f;
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;
res = min(res, max(dv, du));
f.emplace_back(dv, 1);
}
sort(f.begin(), f.end());
vector<pair<int, int>> new_f;
for (auto& p : f) {
if (new_f.empty() || new_f.back().first != p.first) {
new_f.push_back(p);
} else {
new_f.back().second += 1;
}
}
swap(f, new_f);
int L = 1, R = n - 1;
for (auto& p : f) {
R -= p.second;
if (L <= n / 2 && R <= n / 2 && p.second <= n / 2) {
return res;
}
L += p.second;
}
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)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:20:47: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
20 | v = max_element(dist.begin(), dist.end()) - dist.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:27:47: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
27 | u = max_element(dist.begin(), dist.end()) - dist.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:6:28: warning: unused parameter 'sub' [-Wunused-parameter]
6 | 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... |