이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "towns.h"
template <class T>
using Vec = std::vector<T>;
int memo[110][110];
int getDist(const int i, const int j) {
if (i == j) return 0;
if (memo[i][j] == 0) {
memo[i][j] = memo[j][i] = getDistance(i, j);
}
return memo[i][j];
}
int hubDistance(int N, int sub) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
memo[i][j] = 0;
}
}
const auto farthest = [&](const int src) {
Vec<int> v(N);
std::iota(v.begin(), v.end(), 0);
return *std::max_element(v.begin(), v.end(), [&](const int i, const int j) {
return getDist(src, i) < getDist(src, j);
});
};
const auto X = farthest(0);
const auto Y = farthest(X);
int R = 1000005;
std::map<int, Vec<int>> map;
Vec<int> near(N);
const auto Diameter = getDist(X, Y);
for (int u = 0; u < N; ++u) {
const auto x = getDist(X, u);
const auto z = ((x + getDist(0, u)) - getDist(X, 0)) / 2;
const auto l = x - z;
map[l].push_back(u);
R = std::min(R, std::max(l, Diameter - l));
near[u] = z;
}
if (sub <= 2) {
return R;
}
Vec<Vec<int>> cand;
{
int left = 0;
for (const auto &[l, v]: map) {
if (std::max(left, N - (int) v.size() - left) * 2 <= N && std::max(l, Diameter - l) == R) {
cand.push_back(v);
}
left += (int) v.size();
}
}
if (cand.empty()) {
return -R;
}
for (const auto &v: cand) {
if ((int) v.size() * 2 <= N) {
return R;
}
}
const auto same = [&](const int x, const int y) {
return getDist(x, y) < near[x] + near[y];
};
Vec<std::pair<int, int>> alive, dead;
for (const auto x: cand[0]) {
alive.emplace_back(x, 1);
}
while (alive.size() > 2) {
if (alive.size() % 2 == 1) {
dead.push_back(alive.back());
alive.pop_back();
}
Vec<std::pair<int, int>> next;
while (!alive.empty()) {
const auto [x, a] = alive.back();
alive.pop_back();
const auto [y, b] = alive.back();
alive.pop_back();
if (same(x, y)) {
next.emplace_back(x, a + b);
}
else {
dead.emplace_back(x, a);
dead.emplace_back(y, b);
}
}
alive = std::move(next);
}
if (alive.empty()) {
return R;
}
for (const auto [leader, _]: alive) {
int size = 0;
for (const auto [x, a]: alive) {
if (same(leader, x)) {
size += a;
}
}
for (const auto [x, a]: dead) {
if (same(leader, x)) {
size += a;
}
}
if (size * 2 > N) {
return -R;
}
}
return R;
}
#ifdef LOCAL
int dist[110][110];
int getDistance(int i, int j) {
return dist[i][j];
}
int main() {
int sub, tests;
std::cin >> sub >> tests;
while (tests--) {
int N;
std::cin >> N;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
std::cin >> dist[i][j];
}
}
std::cout << hubDistance(N, sub) << '\n';
}
return 0;
}
#endif
# | 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... |