제출 #1236708

#제출 시각아이디문제언어결과실행 시간메모리
1236708nerrrminRace (IOI11_race)C++20
100 / 100
266 ms76000 KiB
#include "race.h" #include<bits/stdc++.h> #define pb push_back using namespace std; const int maxn = 2e6 + 10; int n, k; vector < pair< int, int > > g[maxn]; int block[maxn], all, half, sz[maxn]; void calc(int beg, int from) { //cout << "calc " << beg << " " << from << endl; sz[beg] = 1; for (auto &[nb, w]: g[beg]) { // cout << "nb? " << nb << endl; if(nb == from || block[nb])continue; calc(nb, beg); sz[beg] += sz[nb]; } } int find_centroid(int beg, int from) { for (auto &[nb, w]: g[beg]) { if(nb == from || block[nb])continue; if(sz[nb] > half)return find_centroid(nb, beg); } return beg; } vector < pair < int, int > > p; int ans; int mp[maxn]; vector < int > rem; void dfs(int beg, int from, int cnt, int w) { if(w > k)return; if(w == k) { ans = min(ans, cnt); return; } else { int curr = cnt + mp[k - w]; ans = min(ans, curr); } p.pb({w, cnt}); for (auto &[nb, ww]: g[beg]) { if(nb == from || block[nb])continue; dfs(nb, beg, cnt + 1, w + ww); } } void decompose(int v, int from) { // cout << "decomposing "<< v << endl; calc(v, -1); all = sz[v]; half = all/2; int nc = find_centroid(v, -1); block[nc] = 1; // cout << all << " " << half << " " << nc << endl; for (auto x: rem) mp[x] = n; rem.clear(); for (auto &[nb, w]: g[nc]) { if(block[nb])continue; dfs(nb, -1, 1, w); for (auto &[value, cnt]: p) { if(mp[value] == n)rem.pb(value); mp[value] = min(mp[value], cnt); } p.clear(); } for (auto &[nb, w]: g[nc]) { if(block[nb])continue; decompose(nb, nc); } } 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 u = H[i][0], v = H[i][1], w = L[i]; g[u].pb({v, w}); g[v].pb({u, w}); } ans = n; for (int i = 1; i <= 2*k; ++ i) mp[i] = n; decompose(1, -1); if(ans == n)return -1; //cout << ans << endl; return ans; } /** 11 12 0 1 3 0 2 4 2 3 5 3 4 4 4 5 6 0 6 3 6 7 2 6 8 5 8 9 6 8 10 7 2 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...