Submission #386102

#TimeUsernameProblemLanguageResultExecution timeMemory
386102cpp219Race (IOI11_race)C++14
100 / 100
1078 ms33456 KiB
#pragma GCC optimization "Ofast" #pragma GCC optimization "unroll-loop" #pragma GCC target ("avx2") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back #define mp make_pair #define pii pair <int, int> #define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define F first #define S second const int maxn = 2e5 + 5, maxk = 1e6 + 5; vector <pii> adj[maxn]; int w[maxn], sz[maxn]; int h[maxn], d[maxn], mn[maxk]; int n, k; int inf = 1e9 + 7, ans = 1e9 + 7; bool dead[maxn]; void dfs(int v, int p = -1){ sz[v] = 1; for(auto u : adj[v]){ if(u.F != p && !dead[u.F]){ d[u.F] = d[v] + w[u.S]; h[u.F] = h[v] + 1; dfs(u.F, v); sz[v] += sz[u.F]; } } } int find_centroid(int v, int p, int z){ for(auto u : adj[v]){ if(u.F != p && !dead[u.F] && sz[u.F] > z / 2){ return find_centroid(u.F, v, z); } } return v; } void get(int v, int p = -1){ if(k - d[v] >= 0 && k - d[v] < maxk - 2){ ans = min(mn[k - d[v]] + h[v], ans); } for(auto u : adj[v]){ if(u.F != p && !dead[u.F]){ get(u.F, v); } } } void upd(int v, int p = -1){ if(d[v] >= 0 && d[v] < maxk - 2){ mn[d[v]] = min(h[v], mn[d[v]]); } for(auto u : adj[v]){ if(u.F != p && !dead[u.F]){ upd(u.F, v); } } } void clr(int v, int p = -1){ if(d[v] >= 0 && d[v] < maxk - 2){ mn[d[v]] = inf; } for(auto u : adj[v]){ if(u.F != p && !dead[u.F]){ clr(u.F, v); } } } void solve(int v){ dfs(v); int c = find_centroid(v, -1, sz[v]); dead[c] = true; h[c] = d[c] = 0; dfs(c); for(auto x : adj[c]){ int u = x.F; if(!dead[u]){ get(u, c); upd(u, c); } } ans = min(ans, mn[k]); clr(c); for(auto x : adj[c]){ if(!dead[x.F]){ solve(x.F); } } } 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 x = H[i][0], y = H[i][1]; adj[x].push_back({y, i}); adj[y].push_back({x, i}); } for(int i = 0; i < n - 1; i ++) w[i] = L[i]; for(int i = 0; i < maxk - 2; i ++){ mn[i] = inf; } solve(0); if(ans >= inf){ ans = -1; } return ans; }

Compilation message (stderr)

race.cpp:1: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    1 | #pragma GCC optimization "Ofast"
      | 
race.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      |
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...