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>
#define ll long long
#define pb push_back
#define task "tests"
#define pll pair<ll, ll>
#define pi pair<ll, pll>
#define fi first
#define se second
using namespace std;
const ll mod = 1e15+7;
const ll N = 2e5+5;
const int base = 313;
ll n, m, t, k, T, ans, tong, c[N], a[N], b[N], h[N], mn[N][20], cnt, P[N][20], e, in[N], out[N], dep[N];
vector<pll> adj[N];
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void dfs(ll u, ll p)
{
in[u] = ++cnt;
for(pll v: adj[u])
{
if(v.se == p)continue;
dep[v.se] = dep[u] + v.fi;
h[v.se] = h[u] + 1;
P[v.se][0] = u;
dfs(v.se, u);
a[u] = min(a[u], a[v.se]+v.fi);
}
out[u] = cnt;
}
void cal(ll u, ll p)
{
for(int i = 1; (1<<i) <= h[u]; i ++)
{
P[u][i] = P[P[u][i-1]][i-1];
mn[u][i] = min(mn[u][i-1], mn[P[u][i-1]][i-1]);
}
for(pll v : adj[u])
{
if(v.se == p)continue;
mn[v.se][0] = a[u] - dep[u];
cal(v.se, u);
}
}
ll getans(ll u, ll dis)
{
ll total = a[u] - dep[u];
for(int i = 18; i >= 0; i --)
{
if((dis >> i) & 1)
{
total = min(total, mn[u][i]);
u = P[u][i];
}
}
return total;
}
void sol()
{
cin >> n >> k >> m >> e;
fill_n(a, n+1, mod);
for(int i = 1; i < n; i ++)
{
ll x, y, w;
cin >> x >> y >> w;
adj[x].pb({w, y});
adj[y].pb({w, x});
b[i] = x;
c[i] = y;
}
for(int i = 1; i <= k; i ++)
{
cin >> t;
a[t] = 0;
}
dfs(e, 0);
cal(e, 0);
while(m -- > 0)
{
ll x, y;
cin >> x >> y;
//cout << b[x] <<" "<<c[x]<<" "<<x <<'\n';
if(h[c[x]] < h[b[x]])x = b[x];
else x = c[x];
//cout << x <<" "<<y<<'\n';
if(in[x] <= in[y] && out[y] <= out[x])
{
ans = getans(y, h[y]-h[x]);
if(ans+dep[y] < mod)cout << ans+dep[y] << '\n';
else cout <<"oo"<<'\n';
}
else cout << "escaped" << '\n';
}
}
int main()
{
if(fopen(task".in", "r"))
{
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
5 1
0 1 2
0 2 3
1 3 5
2 4 6
1 1
4 3
*/
Compilation message (stderr)
valley.cpp: In function 'int main()':
valley.cpp:109:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(task".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:110:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | 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... |