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>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e5 + 7;
const int Mod = 1e6 + 3;///lon
const ll INF = 1e18 + 7;
const ll BASE = 137;
const int szBL = 250;
int n, S, Q, root;
int dd[N];
int high[N], par[N][25], ein[N], eout[N];
ll dp[N], spt[N][25], Dis[N];
vector<pii> ke[N];
void solution () {
cin >> n >> S >> Q >> root;
vector<pii> edges = {MP(0, 0)};
rep (i, 1, n - 1) {
int u, v, W;
cin >> u >> v >> W;
edges.pb({u, v});
ke[u].pb({v, W});
ke[v].pb({u, W});
}
rep (i, 1, S) {
int X;
cin >> X;
dd[X] = 1;
}
rep (i, 1, n) dp[i] = INF;
function<void(int, int)> pdfs = [&] (int u, int p) {
static int time_dfs = 0;
ein[u] = ++time_dfs;
high[u] = high[p] + 1;
par[u][0] = p;
rep (i, 1, 20)
par[u][i] = par[par[u][i - 1]][i - 1];
if (dd[u]) dp[u] = Dis[u];
iter (&id, ke[u]) {
int v, w;
tie(v, w) = id;
if (v != p) {
Dis[v] = Dis[u] + w;
pdfs (v, u);
dp[u] = min(dp[u], dp[v]);
}
}
eout[u] = time_dfs;
};
pdfs(root, 0);
function<void(int, int)> dfs = [&] (int u, int p) {
spt[u][0] = dp[u] - 2 * Dis[u];
rep (i, 1, 20) spt[u][i] = min(spt[u][i - 1], spt[par[u][i - 1]][i - 1]);
iter (&id, ke[u]) {
int v, w;
tie(v, w) = id;
if (v != p) {
dfs (v, u);
}
}
};
dfs(root, 0);
rep (i, 1, Q) {
int I, R;
cin >> I >> R;
int u, v;
tie(u, v) = edges[I];
if (high[u] < high[v]) swap(u, v);
if (ein[u] <= ein[R] && ein[R] <= eout[u]) {
ll res = INF;
int delta = high[R] - high[u] + 1;
int cur = R;
rep (i, 0, 20) if (bit(delta, i)) {
res = min(res, spt[cur][i]);
cur = par[cur][i];
}
if (res >= 1e14) cout << "oo\n";
else cout << res + Dis[R] <<"\n";
}
else cout << "escaped\n";
}
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +12
*/
Compilation message (stderr)
valley.cpp:106:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
106 | main () {
| ^~~~
# | 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... |