Submission #1067063

#TimeUsernameProblemLanguageResultExecution timeMemory
1067063ThunnusRace (IOI11_race)C++17
100 / 100
296 ms75092 KiB
#include "race.h" #include<bits/stdc++.h> using namespace std; using i64 = long long; #define int i64 #define vi vector<int> #define vvi vector<vi> #define vb vector<bool> #define pii pair<int, int> #define fi first #define se second #define sz(x) (int)(x).size() const int MAXN = 2e5 + 5; int ans = LLONG_MAX, k; vector<map<int, int>> mp(MAXN); inline void dfs(vector<vector<pii>> &adj, int v = 0, int p = -1, int dist = 0, int dep = 0){ mp[v][dist] = dep; for(pii &u : adj[v]){ if(u.fi == p) continue; dfs(adj, u.fi, v, dist + u.se, dep + 1); if(sz(mp[u.fi]) > sz(mp[v])) mp[v].swap(mp[u.fi]); for(auto &it : mp[u.fi]){ if(mp[v].count(k - (it.fi - 2 * dist))){ // distance with lca ans = min(ans, mp[v][k - (it.fi - 2 * dist)] + it.se - 2 * dep); } } for(auto &it : mp[u.fi]){ if(!mp[v].count(it.fi)) mp[v][it.fi] = it.se; else mp[v][it.fi] = min(mp[v][it.fi], it.se); if(it.fi - dist == k){ ans = min(ans, it.se - dep); } } mp[u.fi].clear(); } } int32_t best_path(int32_t n, int32_t K, int32_t h[][2], int32_t l[]){ k = K; vector<vector<pii>> adj(n); for(int i = 0; i < n - 1; i++){ adj[h[i][0]].emplace_back(h[i][1], l[i]); adj[h[i][1]].emplace_back(h[i][0], l[i]); } dfs(adj); return (ans == LLONG_MAX ? -1 : 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...