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 ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 2e18;
const ll B = 320;
const ll N = 6e5+8;
int n,s,q,e;
vector<pair<int,ll>> g[N];
vector<pair<int,int>> edge = {{0,0}};
bool mark[N];
int tin[N],tout[N],tdfs,d[N],h[N],arr[N];
void dfs_shop(int u, int p){
tin[u] = ++tdfs;
arr[tdfs] = u;
for(pair<int,ll> ed:g[u]){
int v = ed.fi;
ll w = ed.se;
if(v == p) continue;
d[v] = d[u]+w;
h[v] = h[u]+1;
dfs_shop(v,u);
}
tout[u] = tdfs;
}
ll st[20][N],a[N];
void build(){
for(int i = 1; i <= n; i++) st[0][i] = (mark[arr[i]]?d[arr[i]]:inf);
for(int j = 1; j < 20; j++){
for(int i = 1; i + (1<<j) - 1 <= n; i++){
st[j][i] = min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
}
ll rmq(int l, int r){
int k = __lg(r-l+1);
return min(st[k][l],st[k][r-(1<<k)+1]);
}
struct node{
int pr;
ll mn = inf;
} up[N][20];
void dfs_pre(int u, int p){
for(pair<int,ll> ed:g[u]){
int v = ed.fi;
if(v == p) continue;
up[v][0].pr = u;
up[v][0].mn = min(a[u]-2*d[u],a[v]-2*d[v]);
for(int i = 1; i <= 19; i++){
up[v][i].pr = up[up[v][i-1].pr][i-1].pr;
up[v][i].mn = min(up[v][i-1].mn,up[up[v][i-1].pr][i-1].mn);
}
dfs_pre(v,u);
}
}
bool is_anc(int u, int v){
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
ll query(int u, int p){
int k = h[u]-h[p];
ll ans = inf;
for(int i = 19; i >= 0; i--){
if(k>>i&1){
ans = min(ans,up[u][i].mn);
u = up[u][i].pr;
}
}
ans = min(ans,a[u]);
return ans;
}
void solve(){
cin >> n >> s >> q >> e;
for(int i = 1; i < n; i++){
int u,v;
ll w;cin >> u >> v >> w;
g[u].pb({v,w});
g[v].pb({u,w});
edge.pb({u,v});
}
for(int i = 1; i <= s; i++){
int x;cin >> x;
mark[x] = 1;
}
dfs_shop(e,e);
build();
for(int i = 1; i <= n ;i++) a[i] = rmq(tin[i],tout[i]);
dfs_pre(e,e);
while(q--){
int id,r;cin >> id >> r;
int u;
if(h[edge[id].fi] > h[edge[id].se]) u = edge[id].fi;
else u = edge[id].se;
if(!is_anc(u,r)) cout << "escaped\n";
else{
ll ans = query(r,u)+d[r];
if(ans >= 1e18) cout << "oo\n";
else cout << ans << '\n';
}
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if (fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll T = 1;
// cin >> T;
for (ll i = 1; i <= T; i++) {
solve();
// cout << '\n';
}
}
Compilation message (stderr)
valley.cpp: In function 'int main()':
valley.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |