제출 #943360

#제출 시각아이디문제언어결과실행 시간메모리
943360THXuan경주 (Race) (IOI11_race)C++14
9 / 100
995 ms62800 KiB
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <queue> #include <set> #include <map> #define INF 1e9 using namespace std; typedef long long ll; ll n, k; ll ans = INF; vector<pair<ll, ll>>adj[200005]; bool visited[200005]; ll dp[200005]; vector<ll>v; vector<ll>cnt(1e6 + 5, 1e9); ll get_size(int s, int e) { dp[s] = 1; for (auto u : adj[s]) { if (u.first == e || visited[u.first]) continue; get_size(u.first, s); dp[s] += dp[u.first]; } return dp[s]; } int get_centroid(int s, int e, int n) { for (auto u : adj[s]) { if (u.first == e || visited[u.first]) continue; if (dp[u.first] * 2 > n)return get_centroid(u.first, s, n); } return s; } void dfs(int s, int e, bool flag, ll sum, ll dep = 1) { if (sum > k) return; for (auto u : adj[s]) { if (u.first == e || visited[u.first]) continue; dfs(u.first, s, flag, sum + u.second, dep + 1); } if (!flag) { ll need = k - sum; if (cnt[need] != 1e9) { ans = min(ans, dep + cnt[need]); } } else { cnt[sum] = min(cnt[sum], dep); v.push_back(sum); } } void centroid_decomposition(int node) { int centroid = get_centroid(node, 0, get_size(node, 0)); visited[centroid] = true; cnt[0] = 0; for (auto u : adj[centroid]) { if (!visited[u.first]) { dfs(u.first, centroid, false, u.second); dfs(u.first, centroid, true, u.second); } } for (auto u : v) cnt[u] = INF; v.clear(); for (auto u : adj[centroid]) { if (!visited[u.first])centroid_decomposition(u.first); } } int best_path(int a, int b, int h[][2], int l[]) { n = a; k = b; for (int i = 1; i <= n - 1; i++) { int u = h[i][0], v = h[i][1], w = l[i]; adj[u].push_back({ v, w }); adj[v].push_back({ u, w }); } centroid_decomposition(1); if (ans == INF) return -1; else return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...