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>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 42
#endif
using namespace std;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 1e5 + 18;
const ll mod = 1e9+7;
const int LOG = 23;
const ll inf = 1e18;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define F first
#define S second
#define pb push_back
#define ms(x,y) memset((x) , (y) , sizeof (x))
#define done return cout<<endl , 0;
#define kill(x) cout<<x<<endl, exit(0);
#define isIn(x,s,e) ((x) >= (s) && (x) <= e)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pc(x) __builtin_popcount(x)
#define ctz(x) __builtin_ctz(x)
#define MinHeap(x) priority_queue<x, vector<x> , greater<x> >
#define MaxHeap(x) priority_queue<x, vector<x>>
#define int ll
ll pw(ll a, ll b, ll md = mod){ll res = 1; while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n,s,q,ex;
vector<pair<pll,int>> G[N];
int dp[N],up[N][LOG],dpup[N][LOG],dist[N];
int st[N],ft[N];
bool isShop[N];
pll edges[N];
int timer = 1;
void dfs(int v,int p = -1,int edge = -1) {
st[v] = timer ++;
if(isShop[v]) {
dp[v] = dist[v];
} else {
dp[v] = inf;
}
if(edge != -1) {
edges[edge] = make_pair(v,p);
}
for(auto [e,id] : G[v]) {
auto [u,w] = e;
if(u == p) continue;
dist[u] = dist[v] + w;
up[u][0] = v;
dfs(u,v,id);
dp[v] = min(dp[v],dp[u]);
}
ft[v] = timer++;
}
int query(int v,int par) {
int ans = inf;
for(int i = LOG -1; i >= 0 ; i-- ) {
int u = up[v][i];
if(st[par] <= st[u] && ft[par] >= ft[u]) {
debug(u,dpup[v][i]);
ans= min(ans,dpup[v][i]);
v = u;
}
}
return ans;
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>s>>q>>ex;
for(int i = 1; i <= n-1; i ++) {
int v,u,w; cin>>v>>u>>w;
G[v].pb({{u,w},i}); G[u].pb({{v,w},i});
}
for(int i = 1; i <= s; i ++) {int v; cin>>v; isShop[v] = true;}
dfs(ex);
for(int v = 1; v<= n; v ++) {
if(dp[v] != inf) {
dp[v] -= 2 * dist[v];
}
dpup[v][0] = dp[v];
}
for(int i = 1; i < LOG; i ++) {
for(int v = 1 ; v <= n; v++) {
up[v][i] = up[up[v][i-1]][i-1];
dpup[v][i] = min(dpup[v][i-1] , dpup[up[v][i-1]][i-1]);
}
}
while(q--) {
int I,r; cin>>I>>r;
auto [p,pp] = edges[I];
if(!(st[p] <= st[r] && ft[p] >= ft[r])) {
cout<<"escaped\n";
} else {
int ans = query(r,pp);
if(ans == inf)
cout<<"oo\n";
else
cout<<ans + dist[r]<<'\n';
}
}
done;
}
Compilation message (stderr)
valley.cpp: In function 'll query(ll, ll)':
valley.cpp:5:20: warning: statement has no effect [-Wunused-value]
5 | #define debug(...) 42
| ^~
valley.cpp:72:13: note: in expansion of macro 'debug'
72 | debug(u,dpup[v][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... |