# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
640791 | piOOE | Towns (IOI15_towns) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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), sz(n, 1);
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;
sz[x] += sz[y];
}
};
auto same = [&](int x, int y) {
return get(x) == get(y);
};
int x = adj[0], siz = 1;
vector<vector<int>> pl = {{x}}, mn = {{}};
for (int y: adj) {
if (y != x) {
if (dist[x] + dist[y] - 2 * len > getDistance(x, y)) {
unite(x, y);
pl.back().push_back(y);
++siz;
} else {
if (siz == 0) {
siz = 1;
x = y;
pl.emplace_back();
mn.emplace_back();
pl.back().push_back(x);
} else {
mn.back().push_back(y);
--siz;
}
}
}
}
for (int y: adj) if (!same(x, y) && dist[x] + dist[y] - 2 * len > getDistance(x, y)) unite(c, y);
if (*max_element(sz.begin(), sz.end()) * 2 <= n) {
return ans;
} else {
return -ans;
}
}
L += adj.size();
}
return -ans;
}