#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;
vector<ll> dp, dist;
void dfs(int u, int p) {
in[u] = timer++;
for(auto &[v, w] : graph[u]) {
if(v == p) continue;
dist[v] = dist[u] + w;
dfs(v, u);
}
out[u] = timer;
}
void dfs2(int u, int p) {
for(auto &[v, _] : graph[u]) {
if(v == p) continue;
dfs2(v, u);
}
dp[u] = (store[u] ? dist[u] : 1e17);
for(auto &[v, _] : graph[u]) {
if(v == p) continue;
dp[u] = min(dp[u], dp[v]);
}
}
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);
in.resize(n+1);
out.resize(n+1);
store.resize(n+1);
dp.resize(n+1);
dist.resize(n+1);
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);
dfs2(e, 0);
for(int i=1; i<=n; i++) dp[i] -= 2 * dist[i];
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;
}
cout << "0\n";
}
return 0;
}
Compilation message
valley.cpp: In function 'void dfs(int, int)':
valley.cpp:16:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for(auto &[v, w] : graph[u]) {
| ^
valley.cpp: In function 'void dfs2(int, int)':
valley.cpp:26:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for(auto &[v, _] : graph[u]) {
| ^
valley.cpp:33:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for(auto &[v, _] : graph[u]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
12744 KB |
Output is correct |
2 |
Correct |
84 ms |
12516 KB |
Output is correct |
3 |
Correct |
77 ms |
12496 KB |
Output is correct |
4 |
Correct |
66 ms |
13764 KB |
Output is correct |
5 |
Correct |
70 ms |
14020 KB |
Output is correct |
6 |
Correct |
85 ms |
15552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |