# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1278613 | IBory | 경주 (Race) (IOI11_race) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int MAX = 100007, LIM = 1000007;
int Z[MAX], V[MAX], D[LIM];
vector<pii> G[MAX];
int GetSize(int cur, int prev) {
int& ret = Z[cur] = 1;
for (auto [next, _] : G[cur]) {
if (V[next] || next == prev) continue;
ret += GetSize(next, cur);
}
return ret;
}
int GetCent(int cur, int prev, int lim) {
for (auto [next, _] : G[cur]) {
if (V[next] || next == prev) continue;
if (Z[next] * 2 > lim) return GetCent(next, cur, lim);
}
return cur;
}
int temp;
vector<int> ST;
void DFS(int cur, int prev, int cnt, int cd, int K, bool upd) {
if (cd > K) return;
if (upd) temp = min(temp, cnt + D[K - cd]);
else {
D[cd] = min(D[cd], cnt);
ST.push_back(cd);
}
for (auto [next, dist] : G[cur]) {
if (V[next] || next == prev) continue;
DFS(next, cur, cnt + 1, cd + dist, upd);
}
}
int DnC(int cur, int K) {
int C = GetCent(cur, -1, GetSize(cur, -1));
V[C] = 1;
int ret = 1e9;
temp = 1e9;
for (auto& [next, dist] : G[C]) {
if (V[next] || dist > K) continue;
DFS(next, -1, 1, dist, K, 1);
DFS(next, -1, 1, dist, K, 0);
}
ret = min({ret, temp, D[K]});
for (int n : ST) D[n] = 1e9;
ST.clear();
for (auto [next, _] : G[C]) if (!V[C]) ret = min(ret, DnC(next, K));
return ret;
}