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;
#define ll long long
#define ar array
const int mxN=1e5;
const ll INF=1e18;
int n, ns, q, ed, d1[mxN], tin[mxN], tout[mxN], t, anc[mxN][17];
ar<int, 3> edge[mxN];
vector<ar<int, 2>> adj[mxN];
ll d2[mxN], lft[mxN][17], up[mxN], lft2[mxN][17];
bool shop[mxN], has[mxN];
ar<ll, 2> dp[mxN][2];
void ins(int u, ar<ll, 2> x) {
if (x[0]<dp[u][0][0]) {
dp[u][1]=dp[u][0];
dp[u][0]=x;
} else if (x[0]<dp[u][1][0])
dp[u][1]=x;
}
void dfs1(int u=0) {
has[u]=u==ed;
tin[u]=t++;
dp[u][0]=dp[u][1]={INF, -1};
if (shop[u])
ins(u, {0, -1});
for (int i=1; i<17; ++i)
anc[u][i]=anc[anc[u][i-1]][i-1];
for (ar<int, 2> v : adj[u]) {
anc[v[0]][0]=u;
adj[v[0]].erase(find(adj[v[0]].begin(), adj[v[0]].end(), ar<int, 2>{u, v[1]}));
d1[v[0]]=d1[u]+1;
d2[v[0]]=d2[u]+v[1];
dfs1(v[0]);
has[u]|=has[v[0]];
ins(u, {dp[v[0]][0][0]+v[1], v[0]});
}
tout[u]=t++;
}
void dfs2(int u=0) {
lft[u][0]=dp[u][0][0]-d2[u];
lft2[u][0]=shop[u]?d2[u]:INF;
for (int i=1; i<17; ++i) {
lft[u][i]=min(lft[u][i-1], lft[anc[u][i-1]][i-1]);
lft2[u][i]=min(lft2[u][i-1], lft2[anc[u][i-1]][i-1]);
}
if (shop[u])
up[u]=0;
for (ar<int, 2> v : adj[u]) {
up[v[0]]=min(up[u]+v[1], (dp[u][0][1]==v[0]?dp[u][1][0]:dp[u][0][0])+v[1]);
dfs2(v[0]);
}
}
bool ia(int u, int v) {
return tin[u]<=tin[v]&&tout[v]<=tout[u];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> ns >> q >> ed, --ed;
for (int i=1; i<n; ++i) {
int u, v, w;
cin >> u >> v >> w, --u, --v;
edge[i]={u, v, w};
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
while(ns--) {
int u;
cin >> u, --u;
shop[u]=1;
}
dfs1();
up[0]=shop[0]?0:INF;
dfs2();
while(q--) {
int I, u;
cin >> I >> u, --u;
int a=edge[I][0], b=edge[I][1];
if (d1[a]>d1[b])
swap(a, b);
if (ia(b, u)&&has[b]||!ia(b, u)&&!has[b]) {
cout << "escaped\n";
continue;
}
if (ia(b, u)) {
int v=u;
ll bst=INF;
for (int i=16; ~i; --i)
if (d1[anc[v][i]]>=d1[b]) {
bst=min(bst, lft[v][i]);
v=anc[v][i];
}
assert(v==b);
ll ans=min(dp[u][0][0], d2[u]+min(bst, lft[b][0]));
if (ans>INF/2)
cout << "oo\n";
else
cout << ans << "\n";
} else {
int v=u;
ll ans=dp[u][0][0], bst=INF;
for (int i=16; ~i; --i)
if (!ia(anc[v][i], a)) {
bst=min(bst, lft[v][i]);
v=anc[v][i];
}
if (!ia(v, a)) {
bst=min(bst, lft[v][0]);
v=anc[v][0];
}
assert(ia(v, a));
ans=min(ans, d2[u]+bst);
ans=min(ans, d2[u]-d2[v]+up[v]);
//cout << ans << "\n";
bst=INF;
for (int i=16; ~i; --i)
if (d1[a]-(1<<i)>=d1[v]) {
bst=min(bst, lft2[a][i]);
a=anc[a][i];
}
ans=min(ans, d2[u]+min(bst, lft2[v][0])-2*d2[v]);
if (ans>INF/2)
cout << "oo\n";
else
cout << ans << "\n";
}
}
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'int main()':
valley.cpp:88:15: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
88 | if (ia(b, u)&&has[b]||!ia(b, u)&&!has[b]) {
| ~~~~~~~~^~~~~~~~
# | 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... |