This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 2e5 + 10;
const int MOD = 120717661;
#define int long long
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { // read int128
int x; cin >> x; return (ll)x;
}
long long bm(long long b, long long p) {
if(p==0) return 1 % MOD;
long long r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
long long inv(long long b) {
return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) {
long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() {
for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD);
}
vector<pair<int, int> > adj[MAXN];
int oh[MAXN];
int shop[MAXN];
int x, y; // edge removed
vector<pair<int, int> > edges;
int dep[MAXN];
int tin[MAXN], tout[MAXN];
int par[MAXN];
int wow = 0;
vector<int> euler;
void dfs(int node, int prv, int cur) {
oh[node] = 1e18;
dep[node] = cur;
tin[node] = ++wow;
par[node] = prv;
euler.push_back(node);
for(auto x: adj[node]) {
if(x.first != prv) {
dfs(x.first, node, cur + x.second);
oh[node] = min(oh[node], oh[x.first]);
euler.push_back(node);
}
}
tout[node] = ++wow;
if(shop[node]) oh[node] = cur;
}
pair<int, int> st[14][MAXN];
int l[MAXN], r[MAXN];
int lca_idx(int x, int y) {
int m1 = min(l[x], l[y]);
int m2 = max(r[x], r[y]);
int k = 32 - __builtin_clz(m2 - m1 + 1) - 1;
return min(st[k][m1], st[k][m2 - (1<<k) + 1]).second;
}
void solve(int tc) {
int n, s, q, e;
cin >> n >> s >> q >> e;
for(int i=1; i<n; i++) {
int a, b, w;
cin >> a >> b >> w;
edges.push_back({a, b});
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
for(int i=0; i<s; i++) {
int c; cin >> c;
shop[c] = 1;
}
dfs(e, -1, 0);
for(int i=0; i<euler.size(); i++) {
st[0][i] = {dep[euler[i]], euler[i]};
}
for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
for(int i=euler.size() - 1; i>=0; i--) l[euler[i]] = i;
for(int i=1; i<14; i++) {
for(int j=0; j<euler.size(); j++) {
if(j + (1<<i) - 1 < euler.size()) {
st[i][j] = min(st[i-1][j], st[i-1][j + (1<<(i-1))]);
}
}
}
while(q--) {
int i, r;
cin >> i >> r;
if(n == 1) {
cout << "escaped\n";
continue;
}
i--;
x = edges[i].first, y = edges[i].second;
if(tin[x] > tin[y]) swap(x, y);
if(!(tin[y] <= tin[r] && tout[r] <= tout[y])) {
cout << "escaped\n";
}
else {
int ans = 1e18;
for(int i=1; i<=n; i++) {
if(tin[y] <= tin[i] && tout[i] <= tout[y]) {
if(shop[i]) {
ans = min(ans, dep[i] + dep[r] - 2 * dep[lca_idx(i, r)]);
}
}
}
cout << (ans == 1e18 ? "oo" : to_string(ans)) << "\n";
}
}
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
10 10 5 4
7 2 3
4 8 3
9 10 1
6 7 3
9 2 3
10 1 2
8 2 2
5 2 1
3 8 2
1 2 3 4 5 6 7 8 9 10
2 1
1 5
8 4
6 2
7 7
*/
Compilation message (stderr)
valley.cpp: In function 'void solve(long long int)':
valley.cpp:86:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i=0; i<euler.size(); i++) {
| ~^~~~~~~~~~~~~
valley.cpp:89:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
| ~^~~~~~~~~~~~~
valley.cpp:92:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int j=0; j<euler.size(); j++) {
| ~^~~~~~~~~~~~~
valley.cpp:93:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | if(j + (1<<i) - 1 < euler.size()) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |