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>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Edge{
int u, w, i;
};
const int MAXN = 2e5+100;
int N, S, Q, E, shop[MAXN];
pii edge[MAXN];
vector<Edge> g[MAXN];
int timer, tin[MAXN], tou[MAXN];
const int MAXK = 20+5;
int depth[MAXN], pa[MAXK][MAXN];
bool is_shop[MAXN];
ll dist[MAXN], min_dist[MAXN], value[MAXN], answer[MAXK][MAXN];
void dfs(int v, int p){
tin[v] = timer++;
pa[0][v] = p;
min_dist[v] = LLONG_MAX;
if(is_shop[v]) min_dist[v] = dist[v];
for(auto [u, w, i] : g[v]){
if(u != p){
depth[u] = depth[v] + 1;
dist[u] = dist[v] + w;
dfs(u, v);
if(min_dist[u] != LLONG_MAX){
min_dist[v] = min(min_dist[v], min_dist[u]);
}
}
}
tou[v] = timer - 1;
value[v] = LLONG_MAX;
if(min_dist[v] != LLONG_MAX){
value[v] = min_dist[v] - 2 * dist[v];
}
}
bool is_ancestor(int u, int v){
return (tin[u] <= tin[v] && tou[v] <= tou[u]);
}
void solve(){
cin >> N >> S >> Q >> E;
--E;
rep(i, 0, N - 1){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
g[u].pb({v, w, i});
g[v].pb({u, w, i});
edge[i] = {u, v};
}
rep(i, 0, S){
cin >> shop[i];
--shop[i];
is_shop[shop[i]] = true;
}
dfs(E, -1);
rep(v, 0, N){
if(pa[0][v] == -1){
answer[0][v] = value[v];
} else{
answer[0][v] = min(value[v], value[pa[0][v]]);
}
}
rep(k, 0, MAXK - 1){
rep(i, 0, N){
if(pa[k][i] == -1){
pa[k + 1][i] = -1;
answer[k + 1][i] = answer[k][i];
} else{
pa[k + 1][i] = pa[k][pa[k][i]];
answer[k + 1][i] = min(answer[k][i], answer[k][pa[k][i]]);
}
}
}
rep(q, 0, Q){
int i, x;
cin >> i >> x;
--i, --x;
auto [u, v] = edge[i];
if(is_ancestor(u, x) && is_ancestor(v, x)){
int lca = is_ancestor(u, v) ? v : u;
int diff = depth[x] - depth[lca];
ll cur = dist[x];
ll ans = LLONG_MAX;
if(value[x] != LLONG_MAX) ans = cur + value[x];
per(k, MAXK - 1, 0){
if(diff >> k & 1){
if(answer[k][x] != LLONG_MAX)
ans = min(ans, cur + answer[k][x]);
x = pa[k][x];
}
}
if(ans == LLONG_MAX){
cout << "oo\n";
} else{
cout << ans << "\n";
}
} else{
cout << "escaped\n";
}
}
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void dfs(int, int)':
valley.cpp:37:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto [u, w, i] : g[v]){
| ^
valley.cpp: In function 'void solve()':
valley.cpp:97:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
97 | auto [u, v] = edge[i];
| ^
# | 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... |