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;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 1e5 + 3;
const int LG = 17;
int n, s, q, e;
vector<pair<int, ll>> g[N];
int u[N], v[N];
ll w[N];
bool sh[N];
int tin[N], tout[N], timer;
ll f[N], dep[N], mn[N];
int h[N];
ll st[LG + 1][N], up[LG + 1][N];
void dfs(int u, int pre = -1){
f[u] = (sh[u] ? 0 : 1e16);
tin[u] = ++timer;
for(auto [v, w] : g[u]){
if(v == pre)
continue;
dep[v] = dep[u] + w;
h[v] = h[u] + 1;
dfs(v, u);
f[u] = min(f[u], f[v] + w);
}
tout[u] = timer;
mn[u] = f[u] - dep[u];
}
void dfs_st(int u, int pre = -1){
for(auto [v, w] : g[u]){
if(v == pre)
continue;
st[0][v] = mn[v];
up[0][v] = u;
for(int k = 1; k <= LG; ++k){
up[k][v] = up[k - 1][up[k - 1][v]];
st[k][v] = min(st[k - 1][v], st[k - 1][up[k - 1][v]]);
}
dfs_st(v, u);
}
}
void solve(){
cin >> n >> s >> q >> e;
for(int i = 1; i < n; ++i){
cin >> u[i] >> v[i] >> w[i];
g[u[i]].push_back({v[i], w[i]});
g[v[i]].push_back({u[i], w[i]});
}
for(int i = 1; i <= s; ++i){
int x;
cin >> x;
sh[x] = 1;
}
dfs(e);
dfs_st(e);
while(q--){
int i, r;
cin >> i >> r;
int x = u[i], y = v[i];
if(h[x] < h[y])
swap(x, y);
if(tin[x] <= tin[r] and tin[r] <= tout[x]){
int k = h[r] - h[x] + 1;
ll dep_r = dep[r];
ll mn_d = 1e16;
for(int i = 0; (1 << i) <= k; ++i)
if(k & (1 << i))
mn_d = min(mn_d, st[i][r]), r = up[i][r];
if(mn_d > 1e15)
cout << "oo" << '\n';
else
cout << dep_r + mn_d << '\n';
}
else
cout << "escaped" << '\n';
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'int32_t main()':
valley.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |