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 int long long
#define pii pair<int,int>
#define fi first
#define se second
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")*/
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const int N = (int)5e5;
const int maxN = (int)5e5 + 5;
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll infty = 1e17;
const ll logn = 18;
const int base = 311;
const int Block_size = 500;
const int ep = 'a';
int cu[] = {0,0,1,-1};
int cv[] = {-1,1,0,0};
int du[] = {-1,-1,+1,1};
int dv[] = {-1,+1,-1,1};
int cled[] = {6,2,5,5,4,5,6,3,7,6};
void InputFile()
{
freopen(".inp","r",stdin);
freopen(".out","w",stdout);
//freopen("test.out","r",stdin);
}
void FastInput()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
int n,s,q,e;
vector<vector<pii>> adj;
int spec[maxN];
int ispec[maxN];
int x[maxN],y[maxN],w[maxN];
void Read()
{
cin >> n >> s >> q >> e;
adj.resize(n+1);
for(int i = 1;i < n;i++)
{
cin >> x[i] >> y[i] >> w[i];
adj[x[i]].push_back({y[i],w[i]});
adj[y[i]].push_back({x[i],w[i]});
}
for(int i = 1;i <= s;i++)
{
cin >> spec[i];
ispec[spec[i]] = 1;
}
}
int TimeDFS = 0;
int in[maxN];
int jdep[maxN];
int depth[maxN];
int f[30][maxN];
int lca[30][maxN];
int dak[maxN];
int lg[maxN];
int subsz[maxN];
void ScanLog()
{
lg[1] = 0;
for(int i = 2;i <= n;i++)
lg[i] = lg[i/2] + 1;
}
void preDFS(int u,int p)
{
in[u] = ++TimeDFS;
subsz[u] = 1;
if(ispec[u]) dak[u] = 0;
else dak[u] = infty;
for(pii x : adj[u])
{
if(x.fi == p) continue;
depth[x.fi] = depth[u] + x.se;
jdep[x.fi] = jdep[u] + 1;
preDFS(x.fi,u);
dak[u] = min(dak[u],dak[x.fi] + x.se);
subsz[u] += subsz[x.fi];
}
}
void preDFS2(int u,int p)
{
for(pii x : adj[u])
{
if(x.fi == p) continue;
lca[0][x.fi] = u;
f[0][x.fi] = dak[u] - depth[u];
for(int i = 1;i <= lg[n];i++)
{
lca[i][x.fi] = lca[i-1][lca[i-1][x.fi]];
f[i][x.fi] = min(f[i-1][x.fi],f[i-1][lca[i-1][x.fi]]);
}
preDFS2(x.fi,u);
}
}
int Jump(int u,int p)
{
int k = jdep[u] - jdep[p];
int res = dak[u] - depth[u];
for(int i = lg[n];i >= 0;i--)
{
if(k & (1 << i))
{
res = min(res,f[i][u]);
u = lca[i][u];
k ^= (1 << i);
}
}
return res;
}
void Solve()
{
ScanLog();
preDFS(e,0);
preDFS2(e,0);
//cout << f[0][3];
//cout << Jump(5,3);
while(q--)
{
int id,r;
cin >> id >> r;
if(jdep[x[id]] < jdep[y[id]]) swap(x[id],y[id]);
if(in[r] >= in[x[id]] && in[r] <= in[x[id]] + subsz[x[id]] - 1)
{
int res = depth[r] + Jump(r,x[id]);
if(res >= infty) cout <<"oo"<<'\n';
else cout << res <<'\n';
}
else cout <<"escaped"<<'\n';
}
}
void Debug()
{
//Main_sub();
//Naive();
}
int32_t main()
{
FastInput();
//InputFile();
//int sub_type;
//cin >> sub_type;
//Sieve();
int test;
//cin >> test;
test = 1;
while(test--)
//for(int prc = 1; prc <= test; prc++)
{
Read();
Solve();
//Debug();
}
}
Compilation message (stderr)
valley.cpp: In function 'void InputFile()':
valley.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
valley.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(".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... |