제출 #833839

#제출 시각아이디문제언어결과실행 시간메모리
833839ToniBRace (IOI11_race)C++17
100 / 100
871 ms248916 KiB
#include <bits/stdc++.h> #include "race.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; const int MAXN = 2e5 + 2; vector<pii> adj[MAXN]; map<ll, int> m[MAXN]; map<ll, int> has[MAXN]; int n, k, ans = MAXN, sub[MAXN], inc_val[MAXN], cnt[MAXN]; ll inc_path[MAXN]; void dfs(int node, int anc){ sub[node] = 1; for(pii x : adj[node]){ if(x.first == anc) continue; dfs(x.first, node); sub[node] += sub[x.first]; } if(adj[node].size() == 1 && anc != -1){ m[node][0] = 0; has[node][0] = 1; } for(pii x : adj[node]){ if(x.first == anc) continue; ++inc_val[x.first]; inc_path[x.first] += x.second; } m[node][0] = 0; has[node][0] = 1; cnt[node] = 1; for(pii x : adj[node]){ if(x.first == anc) continue; if(cnt[x.first] >= cnt[node]){ swap(m[node], m[x.first]); swap(has[node], has[x.first]); swap(inc_val[node], inc_val[x.first]); swap(inc_path[node], inc_path[x.first]); swap(cnt[node], cnt[x.first]); } for(pair<ll, int> p : m[x.first]){ ll len = p.first + inc_path[x.first]; int val = p.second + inc_val[x.first]; if(k >= len && has[node][k - len - inc_path[node]]){ ans = min(ans, val + m[node][k - len - inc_path[node]] + inc_val[node]); } } for(pair<ll, int> p : m[x.first]){ ll len = p.first + inc_path[x.first] - inc_path[node]; int val = p.second + inc_val[x.first]; if(has[node][len]){ m[node][len] = min(m[node][len], val - inc_val[node]); } else { ++cnt[node]; has[node][len] = 1; m[node][len] = val - inc_val[node]; } } } } int best_path(int _n, int _k, int h[][2], int l[]){ n = _n; k = _k; for(int i = 0; i < n - 1; ++i){ adj[h[i][0]].push_back({h[i][1], l[i]}); adj[h[i][1]].push_back({h[i][0], l[i]}); } dfs(0, -1); if(ans == MAXN) return -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...