Submission #1079019

#TimeUsernameProblemLanguageResultExecution timeMemory
1079019coolboy19521Race (IOI11_race)C++17
21 / 100
1053 ms262144 KiB
#include "race.h" #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #define ll long long #define int ll using namespace std; using namespace __gnu_pbds; const int sz = 2e5 + 10; gp_hash_table<int,int> mc[sz], ban; vector<pair<int,int>> aj[sz]; int ts[sz], k, r; int uk(int v, int p = -1) { ts[v] = 1; for (auto& [u, _] : aj[v]) if (u != p) ts[v] += uk(u, v); return ts[v]; } int fC(int v, int p = -1) { for (auto& [u, _] : aj[v]) if (u != p && !ban[u]) if (ts[u] > ts[v] / 2) { ts[v] -= ts[u]; ts[u] += ts[v]; return fC(u, v); } return v; } void marj(int v, int u) { for (auto& [tr, mn] : mc[u]) if (mc[v].find(tr) != end(mc[v])) mc[v][tr] = min(mc[v][tr], mn); else mc[v][tr] = mn; mc[u].clear(); } void dfs(int v, int p, int l, int d = 1) { mc[v][l] = d; for (auto& [u, w] : aj[v]) if (u != p && !ban[u]) { dfs(u, v, l + w, d + 1); if (mc[v].size() < mc[u].size()) mc[v].swap(mc[u]); marj(v, u); } } void cD(int v) { v = fC(v); mc[v].clear(); for (auto& [u, w] : aj[v]) if (!ban[u]) { dfs(u, v, w); if (mc[v].size() < mc[u].size()) mc[v].swap(mc[u]); for (auto& [tr, mn] : mc[u]) if (mc[v].find(k - tr) != end(mc[v])) r = min(r, mn + mc[v][k - tr]); marj(v, u); } if (mc[v].find(k) != end(mc[v])) r = min(r, mc[v][k]); ban[v] = 1; for (auto& [u, _] : aj[v]) if (!ban[u]) cD(u); } signed best_path(signed N, signed K, signed H[][2], signed L[]) { if (1 == N) { if (0 == K) return 0; else return -1; } for (int i = 0; i < N - 1; i ++) { int a = H[i][0], b = H[i][1]; aj[a].emplace_back(b, L[i]); aj[b].emplace_back(a, L[i]); } r = N, k = K; uk(0); cD(0); return r < N ? r : -1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...