Submission #564177

#TimeUsernameProblemLanguageResultExecution timeMemory
564177tamthegodRace (IOI11_race)C++14
21 / 100
2853 ms262144 KiB
#include<iostream> #include<iomanip> #include<algorithm> #include<stack> #include<queue> #include<string> #include<string.h> #include<cmath> #include<vector> #include<map> #include<unordered_map> #include<set> #include<unordered_set> #include<cstdio> #include<bitset> #include<chrono> #include<random> #include<ext/rope> /* ordered_set #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> */ #define pb push_back #define fi first #define se second using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e6 + 5; const int mod = 1e9 + 7; const ll oo = 1e18; int n, k; vector<pair<int, int>> adj[maxN]; int vis[maxN], parCent[maxN], dp[maxN]; int depth[maxN], lg[maxN]; int f[maxN][21]; ll d[maxN]; map<ll, vector<ll>> mp[maxN]; ll res = oo; void Log() { for(int i=2; i<maxN; i++) lg[i] = lg[i / 2] + 1; } void predfs(int u, int par) { for(pair<int, int> a : adj[u]) { int v = a.fi, w = a.se; if(v == par) continue; d[v] = d[u] + w; depth[v] = depth[u] + 1; f[v][0] = u; for(int i=1; i<=lg[n]; i++) f[v][i] = f[f[v][i - 1]][i - 1]; predfs(v, u); } } int lca(int u, int v) { if(depth[u] < depth[v]) swap(u, v); int k = depth[u] - depth[v]; for(int i=lg[n]; i>=0; i--) { if(k & (1 << i)) { u = f[u][i]; k -= (1 << i); } } if(u == v) return u; for(int i=lg[n]; i>=0; i--) { if(f[u][i] != f[v][i]) { u = f[u][i]; v = f[v][i]; } } return f[u][0]; } ll dist(int u, int v) { return d[u] + d[v] - 2 * d[lca(u, v)]; } ll Dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; } int dfs(int u, int par) { dp[u] = 1; for(pair<int, int> a : adj[u]) { int v = a.fi; if(v == par || vis[v]) continue; dp[u] += dfs(v, u); } return dp[u]; } int Find_Centroid(int u, int par, int sz) { for(pair<int, int> a : adj[u]) { int v = a.fi; if(v == par || vis[v]) continue; if(dp[v] > sz / 2) return Find_Centroid(v, u, sz); } return u; } int Decompose(int u) { int root = Find_Centroid(u, 0, dfs(u, 0)); vis[root] = 1; for(pair<int, int> a : adj[root]) { int v = a.fi; if(vis[v]) continue; parCent[Decompose(v)] = root; } return root; } void update(int u) { for(int v=u; v; v=parCent[v]) { if(!mp[v][k - dist(u, v)].empty()) { for(int x : mp[v][k - dist(u, v)]) { if(dist(u, x) == k) res = min(res, Dist(u, x)); } } if(mp[v][dist(u, v)].empty()) mp[v][dist(u, v)].pb(u); else { if(mp[v][dist(u, v)].size() < 2) mp[v][dist(u, v)].pb(u); else if(Dist(mp[v][dist(u, v)][0], v) > Dist(u, v)) { int x = mp[v][dist(u, v)][0]; mp[v][dist(u, v)].clear(); mp[v][dist(u, v)].pb(u); mp[v][dist(u, v)].pb(x); } else if(Dist(mp[v][dist(u, v)][1], v) > Dist(u, v)) { mp[v][dist(u, v)].pop_back(); mp[v][dist(u, v)].pb(u); } sort(mp[v][dist(u, v)].begin(), mp[v][dist(u, v)].end()); } if(dist(u, v) == k) res = min(res, Dist(u, v)); } } int best_path(int N, int K, int H[][2], int L[2]) { n = N; k = K; for(int i=0; i<n; i++) { H[i][0]++; H[i][1]++; adj[H[i][0]].pb({H[i][1], L[i]}); adj[H[i][1]].pb({H[i][0], L[i]}); } Log(); predfs(1, 0); Decompose(1); for(int i=1; i<=n; i++) update(i); return res < oo ? res : -1; } void ReadInput() { cin >> n >> k; for(int i=1; i<n; i++) { int u, v, w; cin >> u >> v >> w; u++; v++; adj[u].pb({v, w}); adj[v].pb({u, w}); } } void Solve() { Log(); predfs(1, 0); Decompose(1); for(int i=1; i<=n; i++) update(i); cout << (res < oo ? res : -1); } /*int main() { freopen("x.inp", "r", stdin); ReadInput(); Solve(); } */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...