Submission #1148588

#TimeUsernameProblemLanguageResultExecution timeMemory
1148588adlinValley (BOI19_valley)C++20
0 / 100
61 ms18760 KiB
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 5;

int n,s,q,e,a[maxn],x[maxn],y[maxn],z[maxn],c[maxn],z1[maxn],z2[maxn];

vector <pair<int,int>> g[maxn];

ll d[maxn];

int xx;

void dfs(int v, int u){
	for(auto i : g[v]){
		if(i.F == u) continue;
		if(min(i.F,v) == x[z1[xx]] && max(i.F,v) == y[z1[xx]]){
			continue;
		}
		d[i.F] = d[v] + i.S;
		dfs(i.F,v);
	}
}

int up[maxn][21],tin[maxn],tout[maxn],timer;

int dep[maxn];

void dfs2(int v, int u){
	tin[v] = ++timer;
	dep[v] = dep[u] + 1;
	up[v][0] = u;
	
	for(int i = 1; i <= 20; i++) up[i][v] = up[up[i - 1][v]][i - 1];

	for(auto i : g[v]){
		if(i.F == u) continue;
		dfs2(i.F,v);
	}
		
	tout[v] = timer;	
}

bool check(int v, int u){
	return tin[v] <= tin[u] && tout[u] <= tin[v];
}

int lca(int v, int u){
	if(check(v,u)) return v;
	if(check(u,v)) return u;
	for(int i = 20; i >= 0; i--){
		if(!check(up[v][i], u)) {
			v = up[v][i];
		}
	}
	
	return up[v][0];
}

int main(){
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> s >> q >> e;
	
	for(int i = 1; i < n; i++){
		cin >> x[i] >> y[i] >> z[i];
		if(x[i] > y[i]) swap(x[i],y[i]);
		g[x[i]].pb({y[i],z[i]});
		g[y[i]].pb({x[i],z[i]});
	}
	
	for(int i = 1; i <= s; i++){
		cin >> c[i];
	}
	
	for(int i = 1; i <= q; i++){
		cin >> z1[i] >> z2[i];
	}
	
	if(s == n){
			
		dfs2(1,1);
			
		for(int i = 1; i <= q; i++){
			int papa = lca(z2[i],e);
			
			int v1,v2;
			v1 = x[z1[i]];
			v2 = y[z1[i]];
			if(dep[v1] > dep[v2]) swap(v1,v2);
			if(check(papa,v1) && (check(v2,e) || check(v2,z2[i]))){
				cout << "escaped\n";
			}
			else {
				cout << "0\n";
			}
				
		}
			
	}
	else{
		for(int i = 1; i <= q; i++){
			for(int j = 1; j <= n; j++) d[j] = -1;
			xx = i;
			d[z2[i]] = 0;
			dfs(z2[i],z2[i]);
			if(d[e] != -1){
				cout << "escaped\n";
			}
			else {	
				ll mn = 1e18;
				for(int j = 1; j <= s; j++){
					if(d[c[j]] != -1) mn = min(mn, d[c[j]]); 
				}
				if(mn == 1e18) cout << "oo\n";
				else cout << mn << '\n';
			}
		}	
	}
	
	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...