제출 #1148463

#제출 시각아이디문제언어결과실행 시간메모리
1148463gazizmadi11Valley (BOI19_valley)C++20
23 / 100
141 ms64508 KiB
//gm  --- akezhon
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define pb push_back
#define pf push_front
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define tm (tl+tr)/2
#define TL v+v, tl, tm
#define TR v+v+1, tm+1, tr
#define DA l <= tl && tr <= r
#define NE r < tl || tr < l
#define double long double
#define int long long 
using namespace std;

const int N=1e5+7;
const int mod=998244353;
const int inf=2e18;

int n, s, q, e;
vector<pii>g[N];
bool shop[N];
pair<pii,int>reb[N];
int dp[N];
int tin[N], tout[N], timer;
int ans[N];
pii par[N];
pii up[20][N];
int mn[20][N];
void dfs(int v, int p){
	if(shop[v])dp[v]=0;
	else dp[v]=inf;
	tin[v]=++timer;
	for(auto [u, w] : g[v]){
		if(u != p){
			dfs(u, v);
			dp[v] = min(dp[v], dp[u]+w);
		}
	}
	tout[v]=timer;

}
void dfs2(int v, int p){
	for(int i=1; i < 20; i++){
		up[i][v].F = up[i-1][up[i-1][v].F].F;
		up[i][v].S = up[i-1][v].S + up[i-1][up[i-1][v].F].S;
	}
	mn[0][v] = min(dp[v], up[0][v].S+dp[up[0][v].F]);
	for(int i=1; i < 20; i++){
		mn[i][v] = min(mn[i-1][v], up[i-1][v].S + mn[i-1][up[i-1][v].F]);
	}	
	for(auto [u, w] : g[v]){
		if(u != p){
			up[0][u] = {v, w};
			dfs2(u, v);
		}
	}
}
void AlemAmenov(){
	cin >> n >> s >> q >> e;
	for(int i=1; i < n; i++){
		cin >> reb[i].F.F >> reb[i].F.S >> reb[i].S;
		g[reb[i].F.F].pb({reb[i].F.S, reb[i].S});
		g[reb[i].F.S].pb({reb[i].F.F, reb[i].S});
	}
	for(int x, i=1; i <= s; i++){
		cin >> x;
		shop[x]=1;
	}
	dfs(e, 0);
	dfs2(e, 0);
	for(int i=1; i <= q; i++){
		int j, v, anc;
		cin >> j >> v;
		if(tin[reb[j].F.F] < tin[reb[j].F.S])anc = reb[j].F.S;
		else anc = reb[j].F.F;
		if(tin[anc] <= tin[v] && tout[v] <= tout[anc]){
			if(dp[anc] == inf)cout << "oo\n";
			else{
				if(shop[v]){
					cout << 0 << '\n';
					continue;
				}
				int ans=dp[v];
				for(int i=19; i >= 0; i--){
					if(tin[anc] <= tin[up[i][v].F] && tout[up[i][v].F] <= tout[anc]){
						ans = min(ans, mn[i][v]);
						v = up[i][v].F;
					}
				}
				cout << ans << '\n';
			}
		}else{
			cout << "escaped\n";
		}
	}
}
signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int RealName=1;
    // cin >> RealName;

    // freopen("connect.in", "r", stdin);
    // freopen("connect.out", "w", stdout);

    while(RealName--)
        AlemAmenov();
    
return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...