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>
#define name "cownav"
#define fi(i,a,b) for(int i = a; i <= b; i++)
#define fid(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define f first
#define se second
#define pii pair<int, int>
#define getbit(i, j) ((i >> j) & 1)
#define pb push_back
#define all(v) v.begin(), v.end()
#define maxn 100005
const int M = 1e9 + 7;
using namespace std;
int n, s, q, root, num[maxn], en[maxn], h[maxn], cha[maxn][20], cnt;
vector <pii> g[maxn];
pii a[maxn];
ll down[maxn], d[maxn], mi[maxn][20];
void dfs(int u, int fa){
num[u] = cnt++;
for(auto [v, w]: g[u]) if(v != fa){
h[v] = h[u] + 1;
d[v] = d[u] + w;
dfs(v, u);
down[u] = min(down[u], down[v] + w);
}
en[u] = cnt;
}
void build(int u, int fa){
for(auto [v, w]: g[u]) if(v != fa){
cha[v][0] = u;
mi[v][0] = down[v] - d[v];
fi(i, 1, 17){
cha[v][i] = cha[cha[v][i - 1]][i - 1];
mi[v][i] = min(mi[v][i - 1], mi[cha[v][i - 1]][i - 1]);
}
build(v, u);
}
}
ll get_mi(int u, int v){
ll Mi = 1e18;
int kc = h[u] - h[v];
fi(i, 0, 17) if(getbit(kc, i)){
Mi = min(Mi, mi[u][i]);
u = cha[u][i];
}
Mi = min(Mi, mi[v][0]);
return Mi;
}
bool in(int u, int v){
return (num[u] <= num[v] && num[v] <= en[u]);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
//freopen(name".in","r",stdin);
//freopen(name".out","w",stdout);
cin >> n >> s >> q >> root;
fi(i, 1, n - 1){
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
a[i] = {u, v};
}
fi(i, 1, n) down[i] = 1e18;
fi(i, 1, s){
int x; cin >> x;
down[x] = 0;
}
dfs(root, 0);
build(root, 0);
while(q--){
int id, r; cin >> id >> r;
auto [u, v] = a[id];
if(h[u] > h[v]) swap(u, v); //assume that cha[v][0] = u;
if(!in(v, r)){cout << "escaped" << '\n'; continue;}
if(down[v] == 1e18) {cout << "oo" << '\n'; continue;}
cout << d[r] + get_mi(r, v) << '\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... |