Submission #916425

#TimeUsernameProblemLanguageResultExecution timeMemory
916425OAleksaValley (BOI19_valley)C++14
59 / 100
3054 ms38912 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 n, s, q, e, cnt[N], dep[N], par[N]; int tin[N], tout[N], timer, cost[N]; vector<int> shop; vector<pair<int, int>> g[N]; vector<tuple<int, 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; 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); } tout[v] = timer; } 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 = 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, w}); } dfs(1, 0); for (int i = 1;i <= s;i++) { int x; cin >> x; cnt[x] = 1; shop.push_back(x); } for (int j = 0;j < q;j++) { int i, st; cin >> i >> st; --i; int x, a, b, w; tie(a, b, w) = edges[i]; x = (dep[a] > dep[b] ? a : b); int ans = inf; if (s == n) { if (anc(x, e) && anc(x, st)) ans = -1; else if (!anc(x, e) && !anc(x, st)) ans = -1; else ans = 0; } else { if (anc(x, e) && anc(x, st)) ans = -1; else if (!anc(x, e) && !anc(x, st)) ans = -1; else { for (auto u : shop) { if ((!anc(x, u) && !anc(x, st)) || (anc(x, u) && anc(x, st))) ans = min(ans, distance(u, st)); } } } 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...