이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define _pbrngw_
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, s, q, e, in[N], ou[N], timer, marked[N], cost[N], mi[N][20], P[N][20], dis[N], h[N];
vector<ii> G[N];
void dfs(int u, int par){
in[u] = ++timer;
if (marked[u]) cost[u] = 0;
else cost[u] = 1e18;
for (auto v : G[u]){
if (v.fi == par) continue;
dis[v.fi] = dis[u] + v.se;
h[v.fi] = h[u] + 1;
P[v.fi][0] = u;
dfs(v.fi, u);
cost[u] = min(cost[u], cost[v.fi] + v.se);
}
ou[u] = timer;
}
int inside(int u, int v){
return in[u] <= in[v] && in[v] <= ou[u];
}
void dfs2(int u, int par){
for (auto v : G[u]){
if (v.fi == par) continue;
mi[v.fi][0] = cost[u] - dis[u];
// cout << mi[u][0] << "\n";
dfs2(v.fi, u);
}
}
int get(int u, int v){
int k = h[u] - h[v], ans = 1e18;
FORD(i, 17, 0){
if ((k>>i)&1) {
ans = min(ans, mi[u][i]);
u = P[u][i];
}
}
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef _pbrngw_
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#endif
cin >> n >> s >> q >> e;
vector<pii> edges;
FOR (i, 2, n){
int u, v, w; cin >> u >> v >> w;
G[u].pb({v, w});
G[v].pb({u, w});
edges.pb({w, {u, v}});
}
FOR (i, 1, s){
int x; cin >> x;
marked[x] = 1;
}
memset(mi, 0x3f, sizeof mi);
dfs(e, 0);
dfs2(e, 0);
FOR (j, 1, 17){
FOR (i, 1, n) {
P[i][j] = P[P[i][j-1]][j-1];
mi[i][j] = min(mi[i][j-1], mi[P[i][j-1]][j-1]);
}
}
while (q--){
int p, u; cin >> p >> u;
--p;
if (in[edges[p].se.fi] < in[edges[p].se.se]) swap(edges[p].se.fi, edges[p].se.se);
int v = edges[p].se.fi;
if (!inside(v, u)){
cout << "escaped" << "\n";
continue;
}
int ans = min(cost[u], dis[u] + get(u, v));
if (ans >= 1e18) 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... |