Submission #1125423

#TimeUsernameProblemLanguageResultExecution timeMemory
1125423NguyenPhucThangValley (BOI19_valley)C++20
23 / 100
199 ms44056 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<pair<int, int>> vii; typedef pair<int, pair<int, int>> piii; #define forr(i, a, b) for (int i = (a); i <= (b); i++) #define ford(i, a, b) for (int i = (a); i >= (b); i--) #define forf(i, a, b) for (int i = (a); i < (b); i++) #define fi first #define se second #define pb push_back #define all(v) v.begin(), v.end() #define mask(i) (1LL << (i)) #define bit(x, i) (((x) >> (i)) & 1) #define bp __builtin_popcountll #define file "test" template<class X, class Y> bool minz(X &x, const Y &y) { if (x > y) { x = y; return true; } return false; } template<class X, class Y> bool maxz(X &x, const Y &y) { if (x < y) { x = y; return true; } return false; } const int base = 31; const ll mod = 1e9 + 7; // 998244353; const ll oo = (ll) 1e16; const int N = 1e5 + 5; bool isShop[N]; vii g[N]; int h[N], up[N][25]; ll magic[N], min_magic[N][25], cnt[N], dis[N]; struct Edge{ int u, v, w; } edge[N]; void dfs(int u, int pre) { for(pii e : g[u]) { int v = e.fi, w = e.se; if (v == pre) continue; h[v] = h[u] + 1; dis[v] = dis[u] + ll(w); up[v][0] = u; forr(j, 1, 19) up[v][j] = up[up[v][j - 1]][j - 1]; dfs(v, u); } } void dfs2(int u, int pre) { for(pii e : g[u]) { int v = e.fi; if (v == pre) continue; dfs2(v, u); } min_magic[u][0] = magic[u]; forr(j, 1, 19) min_magic[u][j] = min(min_magic[u][j - 1], min_magic[up[u][j - 1]][j - 1]); } int lca(int u, int v) { if (h[u] != h[v]) { if (h[u] < h[v]) swap(u, v); int k = h[u] - h[v]; for(int j = 0; (1 << j) <= k; j++) if (bit(k, j)) u = up[u][j]; } if (u == v) return u; int k = __lg(h[u]); ford(j, k, 0) if (up[u][j] != up[v][j]) u = up[u][j], v = up[v][j]; return up[u][0]; } void buildMagic(int u, int pre) { if (isShop[u]) magic[u] = dis[u], cnt[u] = 1; else magic[u] = oo, cnt[u] = 0; for(pii e : g[u]) { int v = e.fi; if (v == pre) continue; buildMagic(v, u); minz(magic[u], magic[v]); cnt[u] += cnt[v]; } } ll jump(int u, int k) { ll res = oo; for(int j = 0; (1 << j) <= k; j++){ if (bit(k, j)) { minz(res, min_magic[u][j]); u = up[u][j]; } } return min(res, min_magic[u][0]); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, s, q, E; cin >> n >> s >> q >> E; forf(i, 1, n) { int x, y, w; cin >> x >> y >> w; g[x].pb({y, w}); g[y].pb({x, w}); edge[i] = {x, y, w}; } forr(i, 1, s) { int x; cin >> x; isShop[x] = true; } dfs(E, E); buildMagic(E, E); forr(i, 1, n) magic[i] = magic[i] - 2 * dis[i]; dfs2(E, E); forf(i, 1, n) if (h[edge[i].u] > h[edge[i].v]) swap(edge[i].u, edge[i].v); while (q--) { int I, R; cin >> I >> R; int u = edge[I].u, v = edge[I].v; if (lca(v, R) == v){ if (cnt[v] == 0) cout << "oo\n"; else { cout << dis[R] + jump(R, h[R] - h[v]) << '\n'; } } else cout << "escaped\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...