이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1e5 + 3, S = 20;
const ll INF = 1e18;
int tin[N], tout[N], up[N][S];
bool sh[N];
ll mag[N], len[N], up_mag[N][S];
int t = 1;
struct edge{
int v, u;
};
edge ed[N];
vector<vector<pair<int, ll>>> g;
void dfs0(int v, int p, ll l){
if(p != -1){
up[v][0] = p;
}
tin[v] = t++;
len[v] = l;
for(auto i : g[v]){
if(i.first == p) continue;
dfs0(i.first, v, l + i.second);
}
tout[v] = t++;
}
void cal_mag(int v, int p){
ll mi = (sh[v] ? len[v] : INF);
if(mi != INF) mi -= 2 * len[v];
for(auto i : g[v]){
if(i.first == p) continue;
cal_mag(i.first, v);
if(mag[i.first] != INF)
mi = min(mi, mag[i.first] + 2 * i.second);
}
mag[v] = mi;
up_mag[v][0] = mi;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, s, q, e;
cin >> n >> s >> q >> e;
g.resize(n + 1);
for(int i = 1; i <= n - 1; ++i){
int x, y;
ll w;
cin >> x >> y >> w;
ed[i] = {x, y};
g[x].push_back({y, w});
g[y].push_back({x, w});
}
for(int i = 0; i < s; ++i){
int a;
cin >> a;
sh[a] = true;
}
dfs0(e, -1, 0);
cal_mag(e, -1);
for(int i = 1; i <= n; ++i){
for(int j = 1; j < S; ++j){
up[i][j] = up[up[i][j - 1]][j - 1];
up_mag[i][j] = min(up_mag[i][j - 1], up_mag[up[i][j - 1]][j - 1]);
}
}
// for(int i = 1; i <= n; ++i){
// cout << len[i] << " " << mag[i] << " " << i << "\n";
// }
for(int i = 0; i < q; ++i){
int ro, v;
cin >> ro >> v;
int p, ch;
p = ed[ro].u;
ch = ed[ro].v;
int l = len[v];
if(tin[ed[i].u] > tin[ed[i].v]) swap(p, ch);
if(tin[p] < tin[v] && tout[v] < tout[p]){
ll ans = INF;
// cout << v << " " << up[v][0] << "!\n";
for(int k = S - 1; k >= 0; --k){
int cur = up[v][k];
if(tin[p] < tin[cur] && tout[cur] < tout[p]){
// cout << k << " " << v << "*\n";
ans = min(ans, up_mag[v][k + 1]);
v = cur;
}
}
// cout << v << " " << mag[v] << " " << up_mag[v][0] << "\n";
if(ans == INF){
cout << "oo\n";
}else{
cout << ans + l << "\n";
}
}else{
cout << "escaped\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... |