Submission #916902

#TimeUsernameProblemLanguageResultExecution timeMemory
916902OAleksaValley (BOI19_valley)C++14
59 / 100
3028 ms40472 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], mn[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; 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; } 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;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, w}); } for (int i = 1;i <= s;i++) { int x; cin >> x; cnt[x] = 1; shop.push_back(x); } dfs(e, 0); 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]; 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; while (t != b && t != 0) { ans = min(ans, mn[t] + dep[st] - dep[t]); t = par[t]; } } } if (ans == inf) cout << "oo\n"; else if (ans == -1) cout << "escaped\n"; else cout << ans << '\n'; } } return 0; }

Compilation message (stderr)

valley.cpp: In function 'int main()':
valley.cpp:78:9: warning: unused variable 'x' [-Wunused-variable]
   78 |     int x, a, b, w;
      |         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...