#include <bits/stdc++.h>
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define ll long long
#define int long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
using namespace std;
const int N = 3e5 + 123;
const int inf = 2e18;
const int MOD1 = 1e9 + 7;
const int MOD = 998244353;
const int P = 6547;
int a[N], b[N], n, m, q, x, y, z, w[N], s, e;
vector <int> shop;
pii edge[N];
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rand( int l, int r )
{
uniform_int_distribution <int> uid( l, r );
return uid( rng );
}
vector <pii> g[N];
int tin[N], tout[N], tim, d[N], dd[N], up[N][20], dis[N][20], dp[N][20];
void dfs( int v, int p = 0, int ds = 0 )
{
tin[v] = ++ tim;
for( auto to : g[v] )
{
if( to.F == p ) continue;
d[to.F] = d[v] + 1;
dfs( to.F, v, to.S );
dd[v] = min( dd[v], dd[to.F] + to.S );
}
tout[v] = tim;
}
void dfs1( int v, int p = 0, int ds = 0 )
{
up[v][0] = p;
dis[v][0] = ds;
dp[v][0] = ds + dd[p];
FOR( i, 1, 20, 1 )
{
up[v][i] = up[up[v][i - 1]][i - 1];
dis[v][i] = dis[v][i - 1] + dis[up[v][i - 1]][i - 1];
dp[v][i] = min( dp[v][i - 1], dp[up[v][i - 1]][i - 1] + dis[v][i - 1] );
}
for( auto to : g[v] )
{
if( to.F == p ) continue;
dfs1( to.F, v, to.S );
}
}
void solve()
{
cin >> n >> s >> q >> e;
FOR( i, 1, n, 1 ) dd[i] = inf;
FOR( i, 1, n - 1, 1 )
{
cin >> x >> y >> z;
edge[i] = { x, y };
g[x].pb({ y, z });
g[y].pb({ x, z });
}
FOR( i, 1, s, 1 )
{
cin >> x;
shop.pb(x);
dd[x] = 0;
}
dfs(e);
dfs1(e);
FOR( i, 1, n - 1, 1 ) if( d[edge[i].F] > d[edge[i].S] ) swap( edge[i].F, edge[i].S );
FOR( i, 1, q, 1 )
{
cin >> x >> y;
int v, u; tie( v, u ) = edge[x];
if( tin[u] > tin[y] || tout[u] < tout[y] )
{
cout << "escaped\n";
continue;
}
int dist = d[y] - d[u], ans = dd[y];
FORR( j, 20, 0, 1 )
{
if( (dist >> j) & 1 ) ans = min( ans, dp[y][j] ), y = up[y][j];
}
if( ans == inf ) cout << "oo\n";
else cout << ans << '\n';
}
}
signed main()
{
// freopen("connect.in", "r", stdin);
// freopen("connect.out", "w", stdout);
respagold
int test = 1;
if( !test ) cin >> test;
while( test -- )
{
solve();
}
}
// solved by KluydQ
Compilation message (stderr)
valley.cpp: In function 'void dfs1(long long int, long long int, long long int)':
valley.cpp:66:26: warning: iteration 19 invokes undefined behavior [-Waggressive-loop-optimizations]
66 | dp[v][i] = min( dp[v][i - 1], dp[up[v][i - 1]][i - 1] + dis[v][i - 1] );
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:7:45: note: within this loop
7 | #define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
| ^
valley.cpp:62:9: note: in expansion of macro 'FOR'
62 | FOR( i, 1, 20, 1 )
| ^~~
# | 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... |