이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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, tin[mxN], tout[mxN], t, anc[mxN][17];
ar<int, 3> edge[mxN];
vector<ar<int, 2>> adj[mxN];
ll d[mxN], dp[mxN], lft[mxN][17];
bool shop[mxN];
void dfs1(int u=ed) {
tin[u]=t++;
dp[u]=shop[u]?0:INF;
for (int i=1; i<17; ++i)
anc[u][i]=anc[u][i-1]^-1?anc[anc[u][i-1]][i-1]:-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]}));
d[v[0]]=d[u]+v[1];
dfs1(v[0]);
dp[u]=min(dp[u], dp[v[0]]+v[1]);
}
tout[u]=t++;
lft[u][0]=dp[u]<INF?dp[u]-d[u]:INF;
}
bool ia(int u, int v) {
return tin[u]<=tin[v]&&tout[v]<=tout[u];
}
void dfs2(int u=ed) {
for (int i=1; i<17; ++i)
lft[u][i]=min(lft[u][i-1], anc[u][i-1]^-1?lft[anc[u][i-1]][i-1]:INF);
for (ar<int, 2> v : adj[u])
dfs2(v[0]);
}
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;
}
anc[ed][0]=-1;
dfs1();
dfs2();
while(q--) {
int I, u;
cin >> I >> u, --u;
int a=edge[I][0], b=edge[I][1];
if (d[a]>d[b])
swap(a, b);
if (!ia(b, u)) {
cout << "escaped\n";
} else {
int v=u;
ll bst=INF;
for (int i=16; ~i; --i)
if (anc[v][i]^-1&&d[anc[v][i]]>=d[b]) {
bst=min(bst, lft[v][i]);
v=anc[v][i];
}
assert(v==b);
ll ans=min(dp[u], d[u]+min(bst, lft[b][0]));
if (ans==INF)
cout << "oo\n";
else
cout << ans << "\n";
}
}
return 0;
}
# | 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... |