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 <bits/stdc++.h>
#include "towns.h"
using namespace std;
int hubDistance(int n, int sub) {
vector<int> dist0(n);
for (int i = 1; i < n; ++i) {
dist0[i] = getDistance(0, i);
}
int a = max_element(dist0.begin(), dist0.end()) - dist0.begin();
vector<int> dist(n);
dist[0] = dist0[a];
for (int i = 1; i < n; ++i) {
dist[i] = i == a ? 0 : getDistance(i, a);
}
int b = max_element(dist.begin(), dist.end()) - dist.begin();
vector<int> pos(n);
pos[0] = (dist0[a] - dist0[b] + dist[b]) >> 1;
for (int i = 1; i < n; ++i) {
pos[i] = min(pos[0], (dist0[a] - dist0[i] + dist[i]) >> 1);
}
int ans = numeric_limits<int>::max();
map<int, vector<int>> mp;
for (int i = 0; i < n; ++i) {
ans = min(ans, max(pos[i], dist[b] - pos[i]));
mp[pos[i]].push_back(i);
}
int L = 0, R = n;
for (auto [len, adj]: mp) {
R -= adj.size();
if (max(len, dist[b] - len) == ans && max(L, R) <= n / 2) {
if (adj.size() <= n / 2) {
return ans;
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
auto get = [&](int x) {
while (x != p[x]) x = p[x] = p[p[x]];
return x;
};
auto unite = [&](int x, int y) {
x = get(x), y = get(y);
if (x != y) {
p[y] = x;
}
};
auto same = [&](int x, int y) {
return get(x) == get(y);
};
int x = adj[0], siz = 1;
vector<int> S, T;
for (int y: adj) {
if (x != y) {
if (dist[x] + dist[y] - 2 * len > getDistance(x, y)) {
unite(x, y);
S.push_back(y);
} else {
T.push_back(y);
if (T.size() > S.size()) {
x = y;
swap(S, T);
}
}
}
}
int sz = 0;
for (int y : S) {
if (same(x, y) || dist[x] + dist[y] - 2 * len > getDistance(x, y)) {
unite(x, y);
++sz;
}
}
if (sz * 2 <= n) {
return ans;
} else {
return -ans;
}
}
L += adj.size();
}
return -ans;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:12:53: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
12 | int a = max_element(dist0.begin(), dist0.end()) - dist0.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
towns.cpp:20:51: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
20 | int b = max_element(dist.begin(), dist.end()) - dist.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:39:23: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
39 | R -= adj.size();
| ^
towns.cpp:42:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
42 | if (adj.size() <= n / 2) {
| ~~~~~~~~~~~^~~~~~~~
towns.cpp:62:29: warning: unused variable 'siz' [-Wunused-variable]
62 | int x = adj[0], siz = 1;
| ^~~
towns.cpp:91:23: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
91 | L += adj.size();
| ^
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... |