Submission #938501

#TimeUsernameProblemLanguageResultExecution timeMemory
938501BaizhoValley (BOI19_valley)C++14
100 / 100
171 ms42280 KiB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
  
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 
// #pragma GCC optimize("Ofast,unroll-loops,fast-math")
// #pragma GCC target("popcnt")


typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
 
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define pb push_back

const int mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353;  // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const int N = ll(1e5)+100;
const long long inf = 2e18;
const long double eps = 1e-15L;
 
ll lcm(ll a, ll b) { return (a / __gcd(a,b))*b; }

ll binpow(ll a, ll b, ll m) { ll res=1; a %= m; while(b>0){ if(b&1)res=(res * a) % m; a=(a * a) % m; b/=2; } return res%m;}


void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }

int n, s, q, e, depth[N], up[20][N], qu[N], qv[N], qw[N];
ll lift[20][N];
bool shop[N];
ll mn[N], dis[N];
vector<pair<int, int> > g[N];

void dfs(int v, int p = 0) {
	if(!shop[v]) mn[v] = inf;
	up[0][v] = p;
	for(int j = 1; j <= 17; j ++) up[j][v] = up[j - 1][up[j - 1][v]];
	for(auto to : g[v]) {
		if(p == to.ff) continue;
		depth[to.ff] = depth[v] + 1;
		dis[to.ff] = dis[v] + to.ss;
		
		dfs(to.ff, v);
		if(mn[to.ff] != inf) mn[v] = min(mn[v], mn[to.ff] + to.ss);
	}
}

void dfs1(int v, int p = 0) {
	lift[0][v] = mn[v] - dis[v];
	for(int j = 1; j <= 17; j ++) lift[j][v] = min(lift[j - 1][v], lift[j - 1][up[j - 1][v]]);
	for(auto to : g[v]) {
		if(p == to.ff) continue;
		dfs1(to.ff, v);
	}
}

int lca(int u, int v) {
	if(depth[u] < depth[v]) swap(u, v);
	// u is down further
	int dif = depth[u] - depth[v];
	for(int i = 17; i >= 0; i --) {
		if(dif & (1<<i)) u = up[i][u];
	}
//	cout<<u<<" "<<v<<"\n";
	if(u == v) return u;
	for(int i = 17; i >= 0; i --) {
		if(up[i][u] != up[i][v]) {
			u = up[i][u];
			v = up[i][v];
		}
	}
	return up[0][v];
}

ll getmn(int v, int cnt) {
	ll res = inf;
	for(int j = 17; j >= 0; j --) {
		if(cnt & (1 << j)) {
			res = min(res, lift[j][v]);
			v = up[j][v];
		}
	}
	return res;
}

void Baizho() {	
	cin>>n>>s>>q>>e;
	for(int i = 1; i <= n - 1; i ++) {
		int u, v, w; cin>>u>>v>>w;
		qu[i] = u;
		qv[i] = v;
		g[u].pb({v, w});
		g[v].pb({u, w});
	}
	for(int i = 1; i <= s; i ++) {
		int v; cin>>v;
		shop[v] = 1;
	}
	dfs(e);
	dfs1(e);
	for(int i = 1; i <= n - 1; i ++) {
		if(depth[qu[i]] > depth[qv[i]]) swap(qu[i], qv[i]);
	}
	while(q --) {
		int id, r; cin>>id>>r;
		// qu[id] has less depth than qv[id]
//		cout<<lca(r, qv[id])<<" thelca \n";
		if(lca(r, qv[id]) != qv[id]) cout<<"escaped\n";
		else {
			if(mn[qv[id]] == inf) cout<<"oo\n";
			else {
				cout<<getmn(r, depth[r] - depth[qv[id]] + 1) + dis[r]<<"\n";
			}
		}
	}
}
 
signed main() {		
 
    ios_base::sync_with_stdio(false);   
    cin.tie(0);cout.tie(0); 
//   	precalc();
   	
    int ttt = 1;
//    cin>>ttt;

    for(int i=1; i<=ttt; i++) {Baizho(); }
}

Compilation message (stderr)

valley.cpp: In function 'void Freopen(std::string)':
valley.cpp:35:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:35:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...