# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
888401 | vjudge1 | 경주 (Race) (IOI11_race) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 1e6 + 7;
const LL INF = 1e18;
int n, k;
vector<PII> adj[N];
int sz[N];
LL ans = 0;
bool vis[N];
map<LL, LL> best;
int dfs(int u, int p = 0) {
sz[u] = 1;
for (auto [v, w] : adj[u])
if (!vis[v] && v != p) sz[u] += dfs(v, u);
return sz[u];
}
int centroid(int u, int p, int mx) {
for (auto [v, w] : adj[u])
if (!vis[v] && v != p && sz[v] * 2 > mx) return centroid(v, u, mx);
return u;
}
void go(int u, int p, int depth, int level, bool filling) {
if (filling) {
if (best.count(depth)) best[depth] = min(best[depth], 1LL*level);
else best[depth] = level;
} else {
if (best.count(k-depth)) {
ans = min(ans, best[k - depth] + level);
}
}
for (auto [v, w] : adj[u])
if (!vis[v] && v != p) go(v, u, depth + w, level + 1 ,filling);
}
void decompose(int u) {
int mx = dfs(u, u);
int c = centroid(u, u, mx);
vis[c] = true;
best[0] = 0;
for (auto [v, w] : adj[c]) {
if (!vis[v]) {
go(v, c, w, 1, false);
go(v, c, w, 1, true);
}
}
best.clear();
for (auto[v,w] : adj[c])
if (!vis[v]) decompose(v);
}
int best_paht(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for(int i=0; i<n-1; i++){
int u = H[i][0];
int v = H[i][1];
int w = L[i];
adj[u].emplace_back(v,w);
adj[v].emplace_back(u,w);
}
ans = INF;
decompose(1);
if(ans > INF/4) ans = -1;
return ans;
}