제출 #308194

#제출 시각아이디문제언어결과실행 시간메모리
308194HynDufValley (BOI19_valley)C++11
100 / 100
341 ms38008 KiB
#include <bits/stdc++.h> #define task "V" #define all(v) (v).begin(), (v).end() #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define Rep(i, r, l) for (int i = (r); i >= (l); --i) #define DB(X) { cerr << #X << " = " << (X) << '\n'; } #define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; } #define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; } #define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; } #define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';} #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back #define pf push_front #define F first #define S second #define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a)); #define next ___next #define prev ___prev #define y1 ___y1 #define left ___left #define right ___right #define y0 ___y0 #define div ___div #define j0 ___j0 #define jn ___jn using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<ll> vl; const int N = 1e5 + 2; const ll INF = 1e18; int n, m, r, q, pa[N][18], dep[N]; bool market[N]; ll d[N], mn[N], mnA[N][18]; vii g[N]; ii edge[N]; void dfs(int u, int p) { dep[u] = dep[p] + 1; pa[u][0] = p; rep(k, 1, 17) pa[u][k] = pa[pa[u][k - 1]][k - 1]; mn[u] = INF; for (ii v : g[u]) if (v.F != p) { d[v.F] = d[u] + v.S; dfs(v.F, u); mn[u] = min(mn[u], mn[v.F]); } if (market[u]) mn[u] = min(mn[u], d[u]); } int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); Rep(k, 17, 0) if (dep[pa[x][k]] >= dep[y]) x = pa[x][k]; if (x == y) return x; Rep(k, 17, 0) if (pa[x][k] != pa[y][k]) x = pa[x][k], y = pa[y][k]; return pa[x][0]; } void buildmnA(int u, int p) { mnA[u][0] = mn[u]; rep(k, 1, 17) mnA[u][k] = min(mnA[u][k - 1], mnA[pa[u][k - 1]][k - 1]); for (ii v : g[u]) if (v.F != p) buildmnA(v.F, u); } int main() { #ifdef HynDuf freopen(task".in", "r", stdin); //freopen(task".out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> n >> m >> q >> r; rep(i, 2, n) { int u, v, w; cin >> u >> v >> w; g[u].eb(v, w); g[v].eb(u, w); edge[i - 1] = {u, v}; } rep(i, 1, m) { int u; cin >> u; market[u] = 1; } dfs(r, 0); rep(i, 1, n) if (mn[i] != INF) mn[i] -= 2 * d[i]; buildmnA(r, 0); while (q--) { int e, u, uu; cin >> e >> u; uu = u; int v = (dep[edge[e].F] > dep[edge[e].S]) ? edge[e].F : edge[e].S; if (lca(u, v) != v) cout << "escaped\n"; else { ll ans = mn[v]; Rep(k, 17, 0) if (dep[pa[u][k]] >= dep[v]) { ans = min(ans, mnA[u][k]); u = pa[u][k]; } if (ans == INF) cout << "oo\n"; else cout << ans + d[uu] << '\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...