| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 | 
|---|---|---|---|---|---|---|---|
| 1073491 | Zicrus | 도시들 (IOI15_towns) | C++17 | 0 ms | 0 KiB | 
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "towns.h"
using namespace std;
typedef long long ll;
unordered_map<ll, int> cache;
int getDist(int a, int b) {
    if (a == b) return 0;
    if (a < b) swap(a, b);
    ll hash = ((ll)a << 31) | b;
    if (cache.count(hash)) return cache[hash];
    return cache[hash] = getDistance(a, b);
}
vector<int> lnk, sz;
int find(int a) {
    if (lnk[a] != a) return lnk[a] = find(lnk[a]);
    return lnk[a];
}
void unite(int a, int b) {
    a = find(a); b = find(b);
    if (a == b) return;
    lnk[a] = b;
    sz[b] += sz[a];
}
int hubDistance(int n, int sub) {
    cache.clear();
    ll a = 0, b = 0;
    ll mxDist = 0;
    for (int i = 1; i < n; i++) {
        ll val = getDist(0, i);
        if (val > mxDist) {
            mxDist = val;
            a = i;
        }
    }
    mxDist = 0;
    for (int i = 0; i < n; i++) {
        ll val = getDist(a, i);
        if (val > mxDist) {
            mxDist = val;
            b = i;
        }
    }
    ll res = 1ll << 62ll;
    unordered_map<ll, vector<ll>> hubs;
    for (int i = 0; i < n; i++) {
        if (i == a || i == b) continue;
        ll distA = getDist(i, a);
        ll distB = getDist(i, b);
        ll nw = (distA + distB - mxDist) / 2;
        distA -= nw;
        distB -= nw;
        ll val = max(distA, distB);
        if (val <= res) {
            res = val;
            hubs[distA].push_back(i);
        }
        else hubs.clear();
    }
    ll bad = 0;
    for (auto &e : hubs) {
        sz = vector<int>(n, 1);
        lnk = vector<int>(n);
        for (int i = 0; i < n; i++) lnk[i] = i;
        for (auto &x : e.second) {
            ll distX = getDist(x, a) - e.first;
            for (auto &y : e.second) {
                if (find(x) == find(y)) continue;
                ll distY = getDist(y, a) - e.first;
                if (getDist(x, y) < distX + distY) {
                    unite(x, y);
                }
            }
        }
        ll low = 0, high = 0;
        for (int i = 0; i < n; i++) {
            ll distA = getDist(i, a);
            ll distB = getDist(i, b);
            ll nw = (distA + distB - mxDist) / 2;
            distA -= nw;
            if (distA < e.first) low++;
            if (distA > e.first) high++;
        }
        if (low > n/2 || high > n/2) {
            bad++;
            continue;
        }
        bool no = false;
        for (int i = 0; i < n; i++) {
            if (lnk[i] != i) continue;
            if (sz[i] > n/2) no = true;
        }
        if (no) bad++;
    }
    return bad == hubs.size() ? -res : res;
}
#include "grader.cpp"
