Submission #916915

#TimeUsernameProblemLanguageResultExecution timeMemory
916915OAleksaValley (BOI19_valley)C++14
100 / 100
198 ms52160 KiB
#include <bits/stdc++.h> //ako ovaj vaso daso misli da me pobedjuje..... using namespace std; #define int long long #define f first #define s second const int N = 1e5 + 69; const int K = 18; const int inf = 1e18; int up[N][K]; int gas[N][K]; int n, s, q, e, cnt[N], dep[N], par[N], mn[N]; int tin[N], tout[N], timer, d[N]; vector<pair<int, int>> g[N]; vector<pair<int, int>> edges; bool anc(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; } int Lca(int a, int b) { if (anc(a, b)) return a; else if (anc(b, a)) return b; for (int i = K - 1;i >= 0;i--) { if (!anc(up[a][i], b) && up[a][i] > 0) a = up[a][i]; } return up[a][0]; } int distance(int a, int b) { return dep[a] + dep[b] - 2 * dep[Lca(a, b)]; } void dfs(int v, int p) { tin[v] = ++timer; par[v] = p; up[v][0] = p; d[v] = d[p] + 1; if (cnt[v]) mn[v] = 0; for (int i = 1;i < K;i++) up[v][i] = up[up[v][i - 1]][i - 1]; for (auto u : g[v]) { if (u.f == p) continue; dep[u.f] = dep[v] + u.s; dfs(u.f, v); mn[v] = min(mn[v], mn[u.f] + u.s); } tout[v] = timer; } void dfs1(int v, int p) { gas[v][0] = min(mn[v] - dep[v], mn[p] - dep[p]); for (int i = 1;i < K;i++) gas[v][i] = min(gas[v][i - 1], gas[up[v][i - 1]][i - 1]); for (auto u : g[v]) { if (u.f == p) continue; dfs1(u.f, v); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n >> s >> q >> e; for (int i = 0;i <= n;i++) mn[i] = inf; for (int i = 1;i <= n - 1;i++) { int a, b, w; cin >> a >> b >> w; g[a].push_back({b, w}); g[b].push_back({a, w}); edges.push_back({a, b}); } for (int i = 1;i <= s;i++) { int x; cin >> x; cnt[x] = 1; } dfs(e, 0); dfs1(e, 0); for (int j = 0;j < q;j++) { int i, st; cin >> i >> st; --i; int a, b; tie(a, b) = edges[i]; if (dep[a] < dep[b]) swap(a, b); int ans = inf; if (s == n) { if (!anc(a, st) || !anc(b, st)) ans = -1; else ans = 0; } else { if (!anc(a, st) || !anc(b, st)) ans = -1; else { int t = st; int dist = d[st] - d[a]; ans = mn[st]; for (int i = 0;i < K;i++) { if (dist & (1 << i)) { ans = min(ans, gas[t][i] + dep[st]); t = up[t][i]; } } } } if (ans == inf) cout << "oo\n"; else if (ans == -1) cout << "escaped\n"; else cout << ans << '\n'; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...