제출 #1026050

#제출 시각아이디문제언어결과실행 시간메모리
1026050vanea경주 (Race) (IOI11_race)C++17
9 / 100
3060 ms262144 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mxN = 2e5+10; const int mxK = 1e6+10; const int INF = 1e9+10; int ans = INF, k; vector<array<int, 2>> adj[mxN]; bool vis[mxN]; int sz[mxN]; void get_sz(int node, int p) { sz[node] = 1; for(auto [it, w] : adj[node]) { if(it == p || vis[it]) continue; get_sz(it, node); sz[node] += sz[it]; } } int get_c(int node, int p, int n) { for(auto [it, w] : adj[node]) { if(vis[it] || it == p) continue; if(sz[it] * 2 > n) return get_c(it, node, p); } return node; } void get_min(int node, int p, bool filling, int dist, int s, vector<int>& d) { if(s > k) return ; if(filling) { d[s] = min(d[s], dist); } else { ans = min(ans, dist+d[k-s]); } for(auto [it, w] : adj[node]) { if(it == p || vis[it]) continue; get_min(it, node, filling, dist+1, s+w, d); } } void build(int node) { get_sz(node, -1); int c = get_c(node, -1, sz[node]); vis[c] = true; vector<int> d(k+1, INF); d[0] = 0; for(auto [it, w] : adj[c]) { if(vis[it]) continue; get_min(it, c, 0, 1, w, d); get_min(it, c, 1, 1, w, d); } for(auto [it, w] : adj[c]) { if(vis[it]) continue; build(it); } } int best_path(int n, int K, int h[][2], int l[]) { 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]}); } k = K; build(0); return (ans == INF ? -1 : ans); } /* int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << best_path(4, 3, {{0, 1}, {1, 2}, {1, 3}}, {1, 2, 4}); }*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...