이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e17, sINF = 1e16;
vector <ll> h, f, open, closed, dist;
vector <vector<ll>> bin, anc;
vector <vector<pair<ll, ll>>> g;
vector <bool> is_shop;
ll timer = 0;
void dfs(ll c, vector<bool> b){
open[c] = timer++;
b[c] = 1;
if (is_shop[c])f[c] = 0;
for (auto k : g[c]){
if (b[k.first])continue;
h[k.first] = h[c]+1;
dist[k.first] = dist[c]+k.second;
anc[k.first][0] = c;
dfs(k.first, b);
f[c] = min(f[c], f[k.first]+k.second);
}
closed[c] = timer++;
}
void build(ll n){
//resize anc ecc.
for (int i = 1; i <= n; i++)bin[i][0] = f[anc[i][0]]-dist[anc[i][0]];
for (int j = 1; j <= 18; j++){
for (int i = 1; i <= n; i++){
anc[i][j] = anc[anc[i][j-1]][j-1];
bin[i][j] = min(bin[i][j-1], bin[anc[i][j-1]][j-1]);
}
}
}
ll query(ll a, ll b){
if (a == b)return f[a];
ll x = h[a]-h[b], len = dist[a];
ll m = f[a]-len;
for (int i = 0; i <= 18; i++){
if (x & (1<<i)){
m = min(m, bin[a][i]);
a = anc[a][i];
}
}
return m+len;
}
int main(){
ll n, s, q, e; cin >> n >> s >> q >> e;
vector <pair<ll, ll>> edge(1);
h.resize(n+1); dist.resize(n+1); f.resize(n+1, INF); open.resize(n+1); closed.resize(n+1); g.resize(n+1); bin.resize(n+1); anc.resize(n+1); is_shop.resize(n+1, 0);
for (int i = 1; i <= n; i++){anc[i].resize(19); bin[i].resize(19);}
for (int i = 0; i < n-1; i++){
ll a, b, w; cin >> a >> b >> w;
g[a].push_back({b, w});
g[b].push_back({a, w});
edge.push_back({a, b});
}
while (s--){
ll c; cin >> c;
is_shop[c] = 1;
}
h[e] = 0; dist[e] = 0; anc[e][0] = e;
vector<bool> boh(n+1, 0);
dfs(e, boh);
build(n);
while (q--){
ll ind, r; cin >> ind >> r;
ll x = edge[ind].first;
if (h[edge[ind].first] < h[edge[ind].second])x = edge[ind].second;
if (!(open[r] >= open[x] && closed[r] <= closed[x]))cout << "escaped" << "\n";
else {
ll k = query(r, x);
if (k >= sINF)cout << "oo" << "\n";
else cout << k << "\n";
}
}
}
# | 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... |