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 int long long
using namespace std;
const int N = 1e5 + 1 , lg = 21;
int n , s , q , root , T = 0;
vector<int> U(N) , V(N) , W(N) , in(N) , out(N) , d(N) , D(N) , closest(N , 1e18);
vector<vector<int>> up(N , vector<int>(lg)) , mn(N , vector<int>(lg));
vector<vector<pair<int , int>>> g(N);
void dfs(int u , int p = 0 , int ds = 0 , int Ds = 0){
in[u] = ++T;
d[u] = ds;
D[u] = Ds++;
up[u][0] = p;
for(int x = 0;x < g[u].size();x ++){
if(g[u][x].first != p){
dfs(g[u][x].first , u , ds + g[u][x].second , Ds);
closest[u] = min(closest[u] , closest[g[u][x].first] + g[u][x].second);
}
}
mn[u][0] = closest[u] - d[u];
out[u] = T;
}
main() {
cin >> n >> s >> q >> root;
for(int i = 1;i < n;i ++){
cin >> U[i] >> V[i] >> W[i];
g[U[i]].push_back({V[i] , W[i]});
g[V[i]].push_back({U[i] , W[i]});
}
for(int i = 1;i <= s;i ++){
int u;
cin >> u;
closest[u] = 0;
}
dfs(root);
for(int x = 1;x < lg;x ++){
for(int u = 1;u <= n;u ++){
up[u][x] = up[up[u][x - 1]][x - 1];
mn[u][x] = min(mn[u][x - 1] , mn[up[u][x - 1]][x - 1]);
}
}
while(q--){
int I , u;
cin >> I >> u;
if(d[U[I]] < d[V[I]]) swap(U[I] , V[I]);
if(!(in[U[I]] <= in[u] && out[u] <= out[U[I]])){
cout << "escaped" << endl;
continue;
}
int ans = 1e18 , e = D[u] - D[U[I]] , X = d[u];
for(int x = lg - 1;x >= 0;x --){
if((e + 1) & (1 << x)){
ans = min(ans , mn[u][x]);
u = up[u][x];
}
}
if(ans + X > (n - 1) * (int)1e9){
cout << "oo" << endl;
}else{
cout << ans + X << endl;
}
}
}
Compilation message (stderr)
valley.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
valley.cpp:14:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int x = 0;x < g[u].size();x ++){
| ~~^~~~~~~~~~~~~
valley.cpp: At global scope:
valley.cpp:23:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
23 | main() {
| ^~~~
# | 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... |