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>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 998244353;
const int mxn = 3e5 + 5, mxq = 2e5 + 5, sq = 400, mxv = 2e7 + 5;
//const int base = (1 << 18);
const ll inf = 1e15, neg = -69420;
int n, q, s, e;
vt<pll>adj[mxn + 1];
ll dep[mxn + 1], tin[mxn + 1], tout[mxn + 1], tt = 0, dp[mxn + 1];
bool ok[mxn + 1];
ll up[mxn + 1][18], mn[mxn + 1][18], h[mxn + 1];
vt<pii>edge;
void dfs(int u, int pre, ll d){
tin[u] = ++tt; dep[u] = d;
if(ok[u])dp[u] = d;
else dp[u] = inf;
for(auto [v, w]: adj[u]){
if(v != pre){
up[v][0] = u; h[v] = h[u] + 1;
dfs(v, u, d + w);
dp[u] = min(dp[u], dp[v]);
}
}
tout[u] = tt;
}
bool inside(int u, int r){
return(tin[u] <= tin[r] && tout[r] <= tout[u]);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s >> q >> e;
for(int i = 1; i <= n; i++){
for(int j = 0; j < 17; j++){
mn[i][j] = inf;
}
}
forr(i, 1, n){
int u, v, w; cin >> u >> v >> w;
adj[u].pb({v, w}); adj[v].pb({u, w});
edge.pb({u, v});
}
for(int i = 0; i < s; i++){
int x; cin >> x; ok[x] = 1;
}
dfs(e, -1, 0);
for(int i = 1; i <= n; i++){
if(dp[i] != inf)dp[i] -= 2 * dep[i];
mn[i][0] = dp[i];
}
for(int i = 1; i < 17; i++){
for(int j = 1; j <= n; j++){
up[j][i] = up[up[j][i - 1]][i - 1];
mn[j][i] = min(mn[j][i - 1], mn[up[j][i - 1]][i - 1]);
}
}
for(int i = 0; i < q; i++){
int id, r; cin >> id >> r; --id;
auto [u, v] = edge[id];
if(dep[u] < dep[v])swap(u, v);
// is r inside subtree of u
if(!inside(u, r)){
cout << "escaped" << "\n";
}else{
int dif = h[r] - h[u] + 1;
ll res = inf, val = dep[r];
for(int j = 0; j < 17; j++){
if(dif & (1 << j)){
res = min(res, mn[r][j]); r = up[r][j];
}
}
if(res >= inf)cout << "oo" << "\n";
else cout << res + val << "\n";
}
}
return(0);
}
Compilation message (stderr)
valley.cpp: In function 'void dfs(int, int, long long int)':
valley.cpp:37:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto [v, w]: adj[u]){
| ^
valley.cpp: In function 'int main()':
valley.cpp:80:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
80 | auto [u, v] = edge[id];
| ^
# | 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... |