제출 #1148407

#제출 시각아이디문제언어결과실행 시간메모리
1148407KluydQValley (BOI19_valley)C++20
59 / 100
3097 ms38072 KiB
#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], ans[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], up[N][21], tim, d[N];

void dfs( int v, int p = 0 )
{
	up[v][0] = p;
	FOR( i, 1, 20, 1 ) up[v][i] = up[up[v][i - 1]][i - 1];
	tin[v] = ++ tim;
	for( auto to : g[v] ) if( to.F != p ) d[to.F] = d[v] + to.S, dfs( to.F, v );
	tout[v] = tim;
}
bool upper( int v, int u ) { return ( tin[v] <= tin[u] && tout[u] <= tout[v] );}

int lca( int v, int u )
{
	if( upper(v, u) ) return v;
	if( upper(u, v) ) return u;
	
	FORR( i, 20, 0, 1 ) if( up[v][i] && !upper( up[v][i], u )) v = up[v][i];
	
	return up[v][0];
}
int dist( int v, int u )
{
	return d[u] + d[v] - 2 * d[lca( v, u )];
}
bool check( int y, int v, int u )
{
	if( upper( v, y ) && upper( u, y ) ) return 1;
	return 0;
}
void solve()
{
	cin >> n >> s >> q >> e;
	
	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);
	}
	dfs(1);
	
	FOR( i, 1, q, 1 )
	{
		cin >> x >> y;
		
		int v, u; tie( v, u ) = edge[x];
		
		if( upper( e, y ) && !check( e, v, u ) && check( y, v, u ) ) {}
		else if( upper( y, e ) && check( e, v, u ) && !check( y, v, u ) ) {}
		else if( ( check( y, v, u ) || check( e, v, u ) ) && !check( lca( e, y ), v, u ) ) {}
		
		else 
		{
			cout << "escaped\n";
			continue;
		}
		if( s == n )
		{
			cout << "0\n";
			continue;
		}
		int dis = inf;
		
		for( auto sh : shop )
		{
			if( upper( sh, y ) && !check( sh, v, u ) && check( y, v, u ) ) continue;
			else if( upper( y, sh ) && check( sh, v, u ) && !check( y, v, u ) ) continue;
			else if( ( check( y, v, u ) || check( sh, v, u ) ) && !check( lca( sh, y ), v, u ) ) continue;
			
			dis = min( dis, dist( sh, y ));
		}
		if( dis == inf ) cout << "oo\n";
		else cout << dis << '\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  
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...