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;
int sz[MAXN];
void dfs(int node, int prv, int cur) {
oh[node] = 1e18;
dep[node] = cur;
tin[node] = ++wow;
par[node] = prv;
sz[node] = 1;
for(auto x: adj[node]) {
if(x.first != prv) {
dfs(x.first, node, cur + x.second);
oh[node] = min(oh[node], oh[x.first]);
sz[node] += sz[x.first];
}
}
tout[node] = ++wow;
if(shop[node]) oh[node] = cur;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
return sz[a.first] > sz[b.first];
}
vector<int> euler2;
void dfs2(int node, int prv) {
euler2.push_back(node);
for(auto x: adj[node]) {
if(x.first != prv) {
dfs2(x.first, node);
}
}
}
int st[19][MAXN];
int query_min(int l, int r ) {
int k = 32 - __builtin_clz(r-l+1) - 1;
return min(st[k][l], st[k][r - (1<<k) + 1]);
}
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=1; i<=n; i++) sort(adj[i].begin(), adj[i].end(), cmp);
dfs2(e, -1);
for(int i=0; i<euler2.size(); i++) st[0][i] = oh[euler2[i]] - 2 * dep[euler2[i]];
for(int i=1; i<19; i++) {
for(int j=0; j<euler2.size(); j++) {
if(j + (1<<i) - 1 < euler2.size()) {
st[i][j] = min(st[i-1][j], st[i-1][j + (1<<(i-1))]);
}
}
}
int pos[n + 1], dage[n + 1]; // position in euler2 -> position of dage
for(int i=0; i<n; i++) {
pos[euler2[i]] = i;
}
for(int i=0; i<n; i++) {
if(i == 0 || !(tin[euler2[i-1]] < tin[euler2[i]] && tout[euler2[i]] < tout[euler2[i-1]])) {
dage[i] = i;
}
else {
dage[i] = dage[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 og = r;
int ans = 1e18;
while(1) {
int p = pos[r], en = dage[pos[r]];
if(tin[euler2[en]] < tin[y]) en = pos[y];
ans = min(ans, dep[og] + query_min(en, p));
if(euler2[en] != y) r = par[euler2[en]];
else break;
}
cout << (ans >= 1e14 ? "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);
}
Compilation message (stderr)
valley.cpp: In function 'void solve(long long int)':
valley.cpp:97: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]
97 | for(int i=0; i<euler2.size(); i++) st[0][i] = oh[euler2[i]] - 2 * dep[euler2[i]];
| ~^~~~~~~~~~~~~~
valley.cpp:99: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]
99 | for(int j=0; j<euler2.size(); j++) {
| ~^~~~~~~~~~~~~~
valley.cpp:100: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]
100 | if(j + (1<<i) - 1 < euler2.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... |