Submission #898028

#TimeUsernameProblemLanguageResultExecution timeMemory
898028LCJLYValley (BOI19_valley)C++14
59 / 100
155 ms58548 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define ld long double
#define show(x,y) //cout << y << " " << #x << endl;
#define show2(x,y,i,j) //cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) //cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;

vector<pii>adj[100005];
int two[25][100005];
int mini[25][100005];
int in[100005];
int out[100005];
int ptr=0;
int d[100005];
bool shop[100005];
int dp[100005];
int h[100005];

void dfs(int index, int par){
	in[index]=++ptr;
	if(shop[index]) dp[index]=h[index];
	for(auto it:adj[index]){
		if(it.first==par) continue;
		d[it.first]=d[index]+1;
		h[it.first]=h[index]+it.second;
		dfs(it.first,index);
		dp[index]=min(dp[index],dp[it.first]);
	}
	out[index]=ptr;
}

void dfs2(int index, int par){
	for(int x=0;x<20;x++){
		two[x+1][index]=two[x][two[x][index]];
		mini[x+1][index]=min(mini[x][index],mini[x][two[x][index]]);
	}
	for(auto it:adj[index]){
		if(it.first==par) continue;
		two[0][it.first]=index;
		mini[0][it.first]=dp[index]-2*h[index];
		dfs2(it.first,index);
	}
}

int lift(int a, int b){
	//a is road b is node
	int diff=d[b]-d[a];
	
	int ans=dp[b]-h[b];
	int hold=b;
	for(int x=0;x<20;x++){
		if(diff&(1<<x)){
			ans=min(ans,mini[x][b]+h[hold]);
			b=two[x][b];
		}
	}
	return ans;
}

void solve(){	
	int n,s,q,e;
	cin >> n >> s >> q >> e;
	
	int temp,temp2,temp3;
	
	pii edge[n-1];
	for(int x=0;x<n-1;x++){
		cin >> temp >> temp2 >> temp3;
		adj[temp].push_back({temp2,temp3});
		adj[temp2].push_back({temp,temp3});
		edge[x]={temp,temp2};
	}
	
	for(int x=0;x<s;x++){
		cin >> temp;
		shop[temp]=true;
	}
	
	for(int x=0;x<=n;x++){
		dp[x]=1e15;
		for(int y=0;y<25;y++) mini[y][x]=1e15;
	}
	
	dfs(e,-1);
	dfs2(e,-1);
	
	int road,node;
	for(int x=0;x<q;x++){
		cin >> road >> node;
		road--;
		//node is in subtree of road
		if(d[edge[road].first] < d[edge[road].second]){
			temp=edge[road].second;
		}
		else{
			temp=edge[road].first;
		}
		
		show(temp,temp);
		show3(in[temp],in[temp],out[temp],out[temp],in[node],in[node]);
		
		if(in[temp]<=in[node]&&out[temp]>=in[node]){
			int ans=lift(temp,node);
			if(ans>1e12){
				cout << "oo\n";
			}
			else{
				cout << ans << "\n";
			}
		}
		else cout << "escaped\n";
	}	
	
}
 
int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	//freopen("redistricting.in", "r", stdin);
	//freopen("redistricting.out", "w", stdout);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}	
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...