#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int LOG = 20;
int n, s, q, e, timer = 0;
vector<vector<pii> > graph;
vector<int> store, in, out, depth, par;
vector<vector<int> > up;
vector<vector<ll> > dp;
vector<ll> dist;
void dfs(int u, int p) {
in[u] = timer++;
if(store[u]) dp[u][0] = dist[u];
else dp[u][0] = 1e17;
for(auto &[v, w] : graph[u]) {
if(v == p) continue;
dist[v] = dist[u] + w;
par[v] = u;
dfs(v, u);
dp[u][0] = min(dp[u][0], dp[v][0]);
}
out[u] = timer;
}
bool anc(int u, int v) { return (in[u] <= in[v] && out[u] >= out[v]); }
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s >> q >> e;
graph.resize(n+1);
par.resize(n+1);
in.resize(n+1);
out.resize(n+1);
store.resize(n+1);
dist.resize(n+1);
depth.resize(n+1);
up.resize(n+1, vector<int>(LOG));
dp.resize(n+1, vector<ll>(LOG, 1e18));
vector<pii> edges;
for(int i=0; i<n-1; i++) {
int a, b, w;
cin >> a >> b >> w;
edges.push_back({ a, b });
graph[a].push_back({ b, w });
graph[b].push_back({ a, w });
}
for(int i=0; i<s; i++) {
int x;
cin >> x;
store[x] = 1;
}
dfs(e, 0);
for(int u=1; u<=n; u++) dp[u][0] -= 2 * dist[u];
for(pii &e : edges)
if(in[e.first] < in[e.second]) swap(e.first, e.second);
while(q--) {
int I, R;
cin >> I >> R;
pii edge = edges[I-1];
if(anc(edge.first, R) == anc(edge.first, e)) {
cout << "escaped\n";
continue;
}
ll ans = 1e17;
int u = R;
while(anc(edge.first, u)) {
ans = min(ans, dist[R] + dp[u][0]);
u = par[u];
}
if(ans == 1e17) cout << "oo\n";
else cout << ans << '\n';
}
return 0;
}
Compilation message
valley.cpp: In function 'void dfs(int, int)':
valley.cpp:21:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
21 | for(auto &[v, w] : graph[u]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
45840 KB |
Output is correct |
2 |
Correct |
107 ms |
45588 KB |
Output is correct |
3 |
Correct |
130 ms |
45504 KB |
Output is correct |
4 |
Correct |
1374 ms |
47544 KB |
Output is correct |
5 |
Correct |
2966 ms |
47408 KB |
Output is correct |
6 |
Execution timed out |
3035 ms |
47972 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |