제출 #493816

#제출 시각아이디문제언어결과실행 시간메모리
493816Marceantasy경주 (Race) (IOI11_race)C++17
0 / 100
871 ms262148 KiB
#include "race.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define ar array const int mxN = 2e5+5, M = 1e9+7; int k, ans = 1e9, sz[mxN]; vector<pair<ll, ll>> adj[mxN]; // getting the size of our current tree void dfs_size(int u, int p){ sz[u] = 1; for(pair<int, int> v : adj[u]){ if(v.first != p){ dfs_size(v.first, u); sz[u] += sz[v.first]; } } } // traversing subtrees without the centroid and check for possible answers void dfs_get(map<int, int>& mp, int p, int cnt, int len){ if(cnt == k){ ans = min(ans, cnt); return; }else{ if(mp[cnt] == 0){ mp[cnt] = len; }else{ mp[cnt] = min(mp[cnt], len); } } if(mp[k-cnt] > 0){ ans = min(ans, mp[k-cnt]+mp[cnt]); } for(pair<int, int> v : adj[p]){ if(v.first != p){ dfs_get(mp, v.first, cnt+v.second, len+1); } } } //finding the centroid int dfs_find(int u, int p, int total_size){ for(pair<int, int> v : adj[u]){ if(v.first != p && sz[v.first] >= total_size/2) return dfs_find(v.first, u, total_size); } return u; } // get the size of the current tree, find its centroid and then check for possible answers considering // that the centroid is in the answer path, then, check for the case when its not, deleting the centroid // and doing the same process for the centroid adjacent's trees void find_centroid(int u = 0, int p = -1){ memset(sz, 0, sizeof(sz)); dfs_size(u, p); int total_size = sz[u]; int C = dfs_find(u, p, total_size); map<int, int> mp; dfs_get(mp, C, 0, 0); for(pair<int, int> v : adj[C]){ find_centroid(v.first, C); } } int best_path(int N, int K, int H[][2], int L[]){ k = K; for(int i = 0; i<N-1; ++i){ adj[H[i][0]].push_back(make_pair(H[i][1], L[i])); adj[H[i][1]].push_back(make_pair(H[i][0], L[i])); } find_centroid(); return (ans >= 1e9?-1:ans); } /* int main(){ int _N, _K; cin >> _N >> _K; int _H[mxN][2], _L[mxN]; for(int i = 0; i<_N-1; ++i){ cin >> _H[i][0] >> _H[i][1] >> _L[i]; } cout << best_path(_N, _K, _H, _L) << endl; } */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...