#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for (auto& a : x)
#define sz(a) (int)(a).size()
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const ll inf = 1e18 + 5;
const int mxN = 200005;
int n, m, q, root;
pii edge[mxN];
vector<pii> g[mxN];
bool mark[mxN];
int timer = 0;
int in[mxN];
int out[mxN];
ll dist[mxN];
ll best[mxN];
int deda[mxN][20];
void dfs1(int v, int p){
if(mark[v] == 1)best[v] = 0;
deda[v][0] = p;
ff(i,1,19)deda[v][i] = deda[deda[v][i - 1]][i - 1];
in[v] = ++timer;
for(auto c : g[v]){
int u = c.fi;
int w = c.se;
if(u != p){
dist[u] = dist[v] + w;
dfs1(u, v);
best[v] = min(best[v], best[u] + w);
}
}
out[v] = timer;
}
ll mn[mxN][20];
void dfs2(int v, int p){
if(p != 0 && best[p] != inf)mn[v][0] = best[p] - dist[p];
ff(i,1,19)mn[v][i] = min(mn[v][i - 1], mn[deda[v][i - 1]][i - 1]);
for(auto c : g[v]){
if(c.fi != p){
dfs2(c.fi, v);
}
}
}
// da li je y predak od x
bool predak(int x, int y){ return in[y] <= in[x] && in[x] <= out[y]; }
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> q >> root;
ff(i,1,n - 1){
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
edge[i] = {u, v};
}
ff(i,1,m){
int X;
cin >> X;
mark[X] = 1;
}
ff(i,1,n)best[i] = inf;
ff(i,0,n)ff(j,0,19)mn[i][j] = inf;
dfs1(root, 0); dfs2(root, 0);
while(q--){
int i, v;
cin >> i >> v;
int a = edge[i].fi;
int b = edge[i].se;
if(dist[a] < dist[b])swap(a, b);
if(!predak(v, a)){
cout << "escaped" << '\n';
continue;
}
ll ans = (best[v] != inf ? best[v] - dist[v] : inf); int X = v;
fb(j,19,0){
if(deda[X][j] && dist[deda[X][j]] >= dist[a]){
ans = min(ans, mn[X][j]);
X = deda[X][j];
}
}
if(ans == inf)cout << "oo" << '\n';
else cout << ans + dist[v] << '\n';
}
return 0;
}
/*
5 2 3 1
1 2 3
1 3 2
3 4 1
3 5 2
2
4
2 2
2 5
4 5
10 2 5 4
7 2 3
4 8 3
9 10 1
6 7 3
9 2 3
10 1 2
8 2 2
5 2 1
3 8 2
8
7
2 1
1 5
8 4
6 2
7 7
// probati bojenje sahovski
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5204 KB |
Output is correct |
2 |
Correct |
6 ms |
5204 KB |
Output is correct |
3 |
Correct |
6 ms |
5188 KB |
Output is correct |
4 |
Correct |
5 ms |
5180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5204 KB |
Output is correct |
2 |
Correct |
6 ms |
5204 KB |
Output is correct |
3 |
Correct |
6 ms |
5188 KB |
Output is correct |
4 |
Correct |
5 ms |
5180 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
5 ms |
5332 KB |
Output is correct |
10 |
Correct |
5 ms |
5308 KB |
Output is correct |
11 |
Correct |
4 ms |
5316 KB |
Output is correct |
12 |
Correct |
4 ms |
5332 KB |
Output is correct |
13 |
Correct |
4 ms |
5436 KB |
Output is correct |
14 |
Correct |
4 ms |
5308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
40256 KB |
Output is correct |
2 |
Correct |
172 ms |
39840 KB |
Output is correct |
3 |
Correct |
179 ms |
39892 KB |
Output is correct |
4 |
Correct |
184 ms |
41500 KB |
Output is correct |
5 |
Correct |
184 ms |
41680 KB |
Output is correct |
6 |
Correct |
215 ms |
43536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5204 KB |
Output is correct |
2 |
Correct |
6 ms |
5204 KB |
Output is correct |
3 |
Correct |
6 ms |
5188 KB |
Output is correct |
4 |
Correct |
5 ms |
5180 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
5 ms |
5332 KB |
Output is correct |
10 |
Correct |
5 ms |
5308 KB |
Output is correct |
11 |
Correct |
4 ms |
5316 KB |
Output is correct |
12 |
Correct |
4 ms |
5332 KB |
Output is correct |
13 |
Correct |
4 ms |
5436 KB |
Output is correct |
14 |
Correct |
4 ms |
5308 KB |
Output is correct |
15 |
Correct |
146 ms |
40256 KB |
Output is correct |
16 |
Correct |
172 ms |
39840 KB |
Output is correct |
17 |
Correct |
179 ms |
39892 KB |
Output is correct |
18 |
Correct |
184 ms |
41500 KB |
Output is correct |
19 |
Correct |
184 ms |
41680 KB |
Output is correct |
20 |
Correct |
215 ms |
43536 KB |
Output is correct |
21 |
Correct |
150 ms |
39624 KB |
Output is correct |
22 |
Correct |
154 ms |
39156 KB |
Output is correct |
23 |
Correct |
163 ms |
39140 KB |
Output is correct |
24 |
Correct |
215 ms |
41212 KB |
Output is correct |
25 |
Correct |
201 ms |
44028 KB |
Output is correct |
26 |
Correct |
144 ms |
39568 KB |
Output is correct |
27 |
Correct |
152 ms |
39228 KB |
Output is correct |
28 |
Correct |
183 ms |
39192 KB |
Output is correct |
29 |
Correct |
173 ms |
40632 KB |
Output is correct |
30 |
Correct |
190 ms |
42056 KB |
Output is correct |
31 |
Correct |
168 ms |
39748 KB |
Output is correct |
32 |
Correct |
146 ms |
39376 KB |
Output is correct |
33 |
Correct |
156 ms |
39400 KB |
Output is correct |
34 |
Correct |
198 ms |
41152 KB |
Output is correct |
35 |
Correct |
188 ms |
43944 KB |
Output is correct |
36 |
Correct |
163 ms |
41284 KB |
Output is correct |