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;
vector<pii> ke[N];
bool dd[N];
int high[N], par[N][25], ein[N], eout[N];
ll dp[N], spt[N][25], Dis[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 (u, 1, n) dp[u] = 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] - Dis[u] * 2;
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 ver = high[edges[I].fs] < high[edges[I].se] ? edges[I].se : edges[I].fs;
if (ein[ver] <= ein[R] && ein[R] <= eout[ver]) {
int delta = high[R] - high[ver] + 1;
ll res = INF;
int u = R;
rep (i, 0, 20) if (bit(delta, i)) {
res = min (res, spt[u][i]);
u = par[u][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:102:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
102 | 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... |