This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))
template<class T> bool minimize(T& a, T b){
if(a > b) return a = b, true;
return false;
}
template<class T> bool maximize(T& a, T b){
if(a < b) return a = b, true;
return false;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int N = 1e5 + 5;
const ll inf = 1e15;
int n, s, q, root, lift[20][N], h[N], tin[N], tout[N], timerDFS;
ll depth[N], dp[N], rmq[20][N];
array<int, 3> e[N];
vector<pair<int, int>> g[N];
void dfsLCA(int u, int e){
tin[u] = ++timerDFS;
for(auto [v, w] : g[u]) if(v != e){
h[v] = h[u] + 1;
depth[v] = depth[u] + w;
lift[0][v] = u;
for(int i = 1; i <= 17; ++i){
lift[i][v] = lift[i - 1][lift[i - 1][v]];
}
dfsLCA(v, u);
minimize(dp[u], dp[v] + w);
}
tout[u] = timerDFS;
}
void dfsLift(int u, int e){
for(auto [v, w] : g[u]) if(v != e){
rmq[0][v] = -depth[u] + dp[u];
for(int i = 1; i <= 17; ++i){
rmq[i][v] = min(rmq[i - 1][v], rmq[i - 1][lift[i - 1][v]]);
}
dfsLift(v, u);
}
}
bool inSubtree(int par, int u){
return tin[par] <= tin[u] and tout[u] <= tout[par];
}
ll upMin(int u, int d){
ll ans = inf;
for(int i = 0; i <= 17; ++i){
if(d >> i & 1){
minimize(ans, rmq[i][u]);
u = lift[i][u];
}
}
return ans;
}
void Zero_OP(){
cin >> n >> s >> q >> root;
for(int i = 1; i < n; ++i){
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
e[i] = {u, v, w};
}
fill(dp + 1, dp + 1 + n, inf);
for(int i = 1; i <= s; ++i){
int x; cin >> x;
dp[x] = 0;
}
dfsLCA(root, -1);
dfsLift(root, -1);
for(int i = 1; i < n; ++i){
if(tin[e[i][0]] > tin[e[i][1]]) swap(e[i][0], e[i][1]);
}
while(q--){
int i, u;
cin >> i >> u;
int cur = e[i][1];
//cout << cur << ' ' << u << '\n';
if(!inSubtree(cur, u)) cout << "escaped\n";
else if(dp[cur] == inf) cout << "oo";
else cout << min(dp[u], depth[u] + upMin(u, h[u] - h[cur])) << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "antuvu"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
Zero_OP();
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void dfsLCA(int, int)':
valley.cpp:32:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for(auto [v, w] : g[u]) if(v != e){
| ^
valley.cpp: In function 'void dfsLift(int, int)':
valley.cpp:46:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for(auto [v, w] : g[u]) if(v != e){
| ^
valley.cpp: In function 'int main()':
valley.cpp:110:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:111:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |