이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int hubDistance(int n, int sub) {
vector<int> d0(n), d1(n);
int mx = 0, R = 1e9;
for (int i = 1; i < n; ++i) {
d0[i] = getDistance(0, i);
if (d0[i] > d0[mx]) mx = i;
}
d1[0] = d0[mx];
int d = d0[mx];
map<int, vector<int>> groups;
for (int i = 1; i < n; ++i) if (i != mx) {
d1[i] = getDistance(mx, i);
groups[(d1[i] - d0[i] + d0[mx]) / 2].push_back(i);
d = max(d, d1[i]);
}
int hub = 0, lft = 1, rht = n-1;
for (auto g: groups) {
R = min(R, max(g.first, d-g.first));
}
for (auto g: groups) {
rht -= g.second.size();
if (max(g.first, d-g.first) == R) {
if (lft <= n/2 && rht <= n/2) {
//for (int i: g.second) cerr << i << ' ';
//cerr << endl;
vector<int> head(n), sz(n,1);
int mst = g.second[0], cnt=1, qcnt=0;
auto same_st = [&](int a, int b) {qcnt++;return getDistance(a, b) != d1[a]+d1[b] - 2*g.first;};
for (int i = 1; i < g.second.size(); ++i) {
if (same_st(mst, g.second[i])) {
// cerr << "same " << mst << ' ' << g.second[i] << endl;
cnt++;
head[g.second[i]] = mst;
sz[mst]++;
} else {
// cerr << "not same " << mst << ' ' << g.second[i] << endl;
head[g.second[i]] = g.second[i];
if (cnt == 0) {
mst = g.second[i];
cnt = 1;
} else cnt--;
}
}
//cerr << mst << endl;
for (int i = g.second.size(); i--;) if (g.second[i] != mst) {
// cerr << g.second[i] << ' ' << head[g.second[i]] << endl;
if (g.second[i] == head[g.second[i]]) {
if (same_st(mst, g.second[i])) {
// cerr << "same " << mst << ' ' << g.second[i] << endl;
sz[mst] += sz[g.second[i]];
}
//if (qcnt > n) return -R;
}
}
if (sz[mst] >= n/2) return -R;
return R;
}
}
lft += g.second.size();
}
return -R;
}
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:25:24: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
25 | rht -= g.second.size();
| ^
towns.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 1; i < g.second.size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
towns.cpp:49:31: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
49 | for (int i = g.second.size(); i--;) if (g.second[i] != mst) {
| ~~~~~~~~~~~~~^~
towns.cpp:63:24: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
63 | lft += g.second.size();
| ^
towns.cpp:20:6: warning: unused variable 'hub' [-Wunused-variable]
20 | int hub = 0, lft = 1, rht = n-1;
| ^~~
towns.cpp:5:28: warning: unused parameter 'sub' [-Wunused-parameter]
5 | 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... |