제출 #466571

#제출 시각아이디문제언어결과실행 시간메모리
466571dattranxxx경주 (Race) (IOI11_race)C++11
0 / 100
8 ms12068 KiB
#include "race.h" /* * Author : shora */ #include <bits/stdc++.h> #define print(_v) for (auto &_ : _v) {cerr << _ << ' ';} cerr << endl; using namespace std; using ll = long long; const int oo = 1e9; const int N = 5e5, MAXK = 1e6; struct C { int v, w; C(int v, int w): v(v), w(w) {} }; vector<C> G[N]; int vis[N], size[N], f[MAXK + 1], dep[N]; int k; int res = oo; void find_size(int u, int e = -1) { size[u] = 1; for (C& c : G[u]) if (c.v != e && !vis[c.v]) { find_size(c.v, u); size[u] += size[c.v]; } } int centroid(int s, int u, int e = -1) { for (C& c : G[u]) if (c.v != e && !vis[c.v]) if (size[c.v] > s / 2) return centroid(s, c.v, u); return u; } void dfs(int u, int e = -1, int dis = 0) { if (k < dis) return; if (e != -1) dep[u] = dep[e] + 1; if (f[k - dis] != oo) res = min(res, f[k - dis] + dep[u]); for (C& c : G[u]) if (c.v != e && !vis[c.v]) { dfs(c.v, u, dis + c.w); } f[dis] = min(f[dis], dep[u]); } void clear(int u, int e = -1, int dis = 0) { if (k < dis) return; dep[u] = 0; for (C& c : G[u]) if (c.v != e && !vis[c.v]) { clear(c.v, u, dis + c.w); } f[dis] = oo; } void solve(int u) { find_size(u); u = centroid(size[u], u); vis[u] = 1; dfs(u); clear(u); for (C& c : G[u]) if (!vis[c.v]) solve(c.v); } int best_path(int n, int K, int H[][2], int L[]) { k = K; for (int i = 0, u, v, w; i < n-1; ++i) { u = H[i][0]; v = H[i][1]; w = L[i]; G[u].push_back(C(v, w)); G[v].push_back(C(u, w)); } for (int i = 1; i <= k; ++i) f[i] = oo; solve(0); return res == oo ? -1 : res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...