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;
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void IOIGold2024_InshAllah() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }
const ll inf = 1e18+7, MX = LLONG_MAX, MN = LLONG_MIN;
const ll mod = 1e9+7, N = 3e5+5;
vector<pair<ll, ll>> g[N];
ll d[N], dist[N], dp[N], up[N][20], t[N][20], tin[N], tout[N], timer;
pair<ll, ll> edge[N];
void dfs(ll v, ll p) {
tin[v] = ++timer;
up[v][0] = p, dist[v] = dist[p]+1;
for(ll i=1; i<20; i++) up[v][i] = up[up[v][i-1]][i-1];
for(auto [to, w]: g[v]) {
if(to==p) continue;
d[to] = d[v]+w;
dfs(to, v);
dp[v] = min(dp[v], dp[to]+w);
}
tout[v] = ++timer;
}
void qfs(ll v, ll p) {
t[v][0] = dp[v]-d[v];
for(ll i=1; i<20; i++) t[v][i] = min(t[v][i-1], t[up[v][i-1]][i-1]);
for(auto [to, w]: g[v]) {
if(to==p) continue;
qfs(to, v);
}
}
void kigash() {
ll n, m, q, x;
cin>>n>>m>>q>>x;
for(ll i=1; i<n; i++) {
ll u, v, w;
cin>>u>>v>>w;
edge[i] = {u, v};
g[u].pb({v, w}), g[v].pb({u, w});
}
for(ll i=1; i<=n; i++) dp[i] = inf;
for(ll i=1; i<=m; i++) {
ll v;
cin>>v;
dp[v] = 0;
}
dfs(x, 0), qfs(x, 0);
while(q--) {
ll a, b;
cin>>a>>b;
ll u = edge[a].ff, v = edge[a].ss;
if(dist[u]>dist[v]) swap(u, v);
if(tin[v]<=tin[b] && tout[b]<=tout[v]) {
ll k = dist[b]-dist[v], b_ = b, mn = inf;
for(ll i=19; i>=0; i--) {
if(k&(1<<i)) {
mn = min(mn, t[b][i]);
b = up[b][i];
}
}
mn = min(mn, t[v][0]);
if(mn+d[b_]>=inf) cout<<"oo\n";
else cout<<mn+d[b_]<<"\n";
}
else cout<<"escaped\n";
}
return;
}
signed main(/*Kigash Amir*/) {
// freopen("");
IOIGold2024_InshAllah();
ll tt = 1;
// cin>>tt;
for(ll i=1; i<=tt; i++) {
kigash();
}
}
Compilation message (stderr)
valley.cpp: In function 'void freopen(std::string)':
valley.cpp:17:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:17:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "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... |