Submission #784676

#TimeUsernameProblemLanguageResultExecution timeMemory
784676vjudge1Race (IOI11_race)C++17
0 / 100
203 ms262144 KiB
#include <bits/stdc++.h> #include "race.h" using namespace std; #define endl '\n' #define ll long long #define all(x) x.begin(), x.end() vector<pair<int, int>> g[200005]; bool processed[200005]; int sz[200005]; int getSize(int cur, int par = -1) { sz[cur] = 1; for (auto next : g[cur]) { if (next.first != par && !processed[next.first]) sz[cur] += getSize(next.first, cur); } return sz[cur]; } int getCentroid(int cur, int des, int par = -1) { for (auto next : g[cur]) { if (next.first != par && !processed[cur] && sz[cur] > des / 2) return getCentroid(next.first, des, cur); } return cur; } int ans = 10e7, k; int depths[200005]{(int) 10e7}; set<int> s; unordered_map<int, int> h; void solve(int cur, int par, int depth, int sum, int t) { if (sum > k || depth >= ans) return; if (sum == k) { ans = depth; return; } if (t == 0 && h.count(k - sum)) ans = min(ans, depth + depths[h[k - sum]]); if (t == 1) depths[h[sum]] = min(depths[h[sum]], depth); if (t == 2) s.insert(sum); for (auto next : g[cur]) { if (next.first != par && !processed[next.first] && sum + next.second <= k) { solve(next.first, cur, depth + 1, sum + next.second, t); } } } void decompose(int cur = 0) { getSize(cur); cur = getCentroid(cur, getSize(cur)); processed[cur] = 1; for (auto next : g[cur]) { if (!processed[next.first] && next.second <= k) solve(next.first, -1, 1, next.second, 2); } int cnt = 0; for (auto x : s) h[x] = cnt++; for (auto next : g[cur]) { if (!processed[next.first] && next.second <= k) { solve(next.first, -1, 1, next.second, 0); solve(next.first, -1, 1, next.second, 1); } } for (auto x : s) depths[x] = 10e7; s.clear(); h.clear(); for (auto next : g[cur]) { if (!processed[next.first]) decompose(next.first); } } int best_path(int N, int K, int H[][2], int L[]) { k = K; ans = N; for (int i = 0; i < N - 1; i++) { g[H[i][0]].push_back({H[i][1], L[i]}); g[H[i][1]].push_back({H[i][0], L[i]}); } decompose(); if (ans == N) 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...