제출 #890328

#제출 시각아이디문제언어결과실행 시간메모리
890328anarch_y경주 (Race) (IOI11_race)C++17
100 / 100
828 ms56144 KiB
#include <bits/stdc++.h> #include "race.h" using namespace std; using ll = long long; #define all(x) begin(x), end(x) #define sz(x) (int)x.size() #define pb push_back const int maxn = 200001; int N, K; vector<int> adj[maxn]; vector<pair<int, int>> adj2[maxn]; int sub[maxn]; bool done[maxn]; int subtree_size(int s, int p){ sub[s] = 1; for(auto u: adj[s]){ if(u == p or done[u]) continue; sub[s] += subtree_size(u, s); } return sub[s]; } int get_centroid(int s, int p, int num){ for(auto u: adj[s]){ if(u == p or done[u]) continue; if(2*sub[u] > num) return get_centroid(u, s, num); } return s; } ll ans = 1e18; map<ll, ll> best; void calc(int s, int p, bool fill, int d, int wt){ if(wt > K) return; if(fill){ if(best.count(wt)) best[wt] = min(best[wt], 1LL*d); else best[wt] = d; } else{ if(best.count(K-wt)){ ans = min(ans, best[K-wt] + 1LL*d); } } for(auto &[u, t]: adj2[s]){ if(u == p or done[u]) continue; calc(u, s, fill, d+1, wt+t); } } void centroid_decomp(int s){ int num = subtree_size(s, 0); int cent = get_centroid(s, 0, num); best[0] = 0; for(auto &[u, w]: adj2[cent]){ if(done[u]) continue; calc(u, cent, false, 1, w); calc(u, cent, true, 1, w); } best.clear(); done[cent] = true; for(auto u: adj[cent]){ if(!done[u]) centroid_decomp(u); } } int best_path(int _N, int _K, int H[][2], int L[]){ N = _N, K = _K; for(int i=0; i<N-1; i++){ int a = H[i][0], b = H[i][1], w = L[i]; a++; b++; adj[a].pb(b); adj[b].pb(a); adj2[a].pb({b, w}); adj2[b].pb({a, w}); } centroid_decomp(1); if(ans > 1e18/4) ans = -1; 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...