제출 #1307067

#제출 시각아이디문제언어결과실행 시간메모리
1307067saken03Valley (BOI19_valley)C++20
100 / 100
302 ms63976 KiB
#include <iostream> #include <map> #include <vector> #define pb push_back #define fi first #define se second typedef long long ll; using namespace std; const int N = 1e5 + 5; const ll INF = 1e18; int n, s, q, e; pair<int, int> edge[N]; vector<int> g[N]; map<int, bool> shop; map<pair<int, int>, ll> cost; bool is_esc[N]; int in[N], out[N], clk, par[N]; ll dist[N]; void dfs(int v, int p = 0) { in[v] = ++clk; par[v] = p; for (int to : g[v]) { if (to != p) { dist[to] = dist[v] + max(cost[{v, to}], cost[{to, v}]); dfs(to, v); } } out[v] = ++clk; } bool subtree_of(int a, int b) { if (in[a] <= in[b] && out[b] <= out[a]) return 1; return 0; } ll magic[N]; void buildMagic(int v) { for (int to : g[v]) { if (to == par[v]) continue; buildMagic(to); } if (shop[v] == 1) { magic[v] = dist[v]; } else magic[v] = INF; for (int to : g[v]) { if (to == par[v]) continue; magic[v] = min(magic[v], magic[to]); } } void solve() { cin >> n >> s >> q >> e; for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; cost[{a, b}] = c; g[a].pb(b); g[b].pb(a); edge[i] = {a, b}; } while (s--) { int c; cin >> c; shop[c] = 1; } dfs(e); buildMagic(e); vector<vector<ll>> jumpM(n + 1, vector<ll> (18, INF)); vector<vector<int>> jumpV(n + 1, vector<int> (18)); for (int i = 1; i <= n; i++) { magic[i] -= 2 * 1ll * dist[i]; jumpV[i][0] = par[i]; jumpM[i][0] = magic[i]; } for (int j = 1; j < 18; j++) { for (int i = 1; i <= n; i++) { jumpV[i][j] = jumpV[jumpV[i][j - 1]][j - 1]; jumpM[i][j] = min(jumpM[i][j - 1], jumpM[jumpV[i][j - 1]][j - 1]); } } while (q--) { int i, r; cin >> i >> r; int a, b; a = edge[i].fi; b = edge[i].se; if (par[a] == b) swap(a, b); // a -> b (in tree) //cout << "a is " << a << ", b is " << b << '\n'; if (!subtree_of(b, r)) { cout << "escaped\n"; continue; } ll ans = INF; int v = r; for (int l = 17; l >= 0; l--) { if (subtree_of(a, jumpV[v][l])) { ans = min(ans, jumpM[v][l]); v = jumpV[v][l]; } } if (ans < 1e17) cout << ans + dist[r]; else cout << "oo"; cout << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); 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...