이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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));
for (int i = 0; i < n; i++) {
w[i][i] = 0;
}
int Q = 0;
auto Ask = [&](int i, int j) {
if (w[i][j] == -1) {
++Q;
assert(Q <= n * (n - 1) / 2);
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<int> f;
vector<int> d(n);
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;
d[i] = d_vi - dv;
res = min(res, max(dv, du));
f.push_back(dv);
}
vector<int> sz(n, 1);
vector<int> par(n);
iota(par.begin(), par.end(), 0);
function<int(int)> GetRoot = [&](int x) {
return par[x] == x ? x : par[x] = GetRoot(par[x]);
};
auto Merge = [&](int x, int y) {
x = GetRoot(x);
y = GetRoot(y);
if (x != y) {
sz[x] += sz[y];
par[y] = x;
}
};
for (int i = 0; i < n; i++) {
if (i == u || i == v) {
continue;
}
for (int j = i + 1; j < n; j++) {
if (j == u || j == v) {
continue;
}
if (GetRoot(i) != i || GetRoot(j) != j) {
continue;
}
if (d[i] + d[j] > Ask(i, j)) {
Merge(i, j);
}
}
}
sort(f.begin(), f.end());
f.erase(unique(f.begin(), f.end()));
int m = (int) f.size();
vector<vector<int>> ch(m);
for (int i = 0; i < n; i++) {
if (i == u || i == v || i != GetRoot(i)) {
continue;
}
int idx = lower_bound(f.begin(), f.end(), Ask(i, v) - d[i]) - f.begin();
ch[idx].push_back(sz[i]);
}
int L = 1, R = n - 1;
for (int i = 0; i < m; i++) {
for (int x : ch[i]) {
R -= x;
}
if (res == max(f[i], diam - f[i])) {
bool ok = true;
if (L > n / 2) {
ok = false;
}
if (R > n / 2) {
ok = false;
}
for (int x : ch[i]) {
if (x > n / 2) {
ok = false;
}
}
if (ok) {
return res;
}
}
for (int x : ch[i]) {
L += x;
}
}
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
*/
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:26:47: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
26 | v = max_element(dist.begin(), dist.end()) - dist.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:33:47: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
33 | u = max_element(dist.begin(), dist.end()) - dist.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:92:65: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
92 | int idx = lower_bound(f.begin(), f.end(), Ask(i, v) - d[i]) - f.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... |