#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
int a,b;
int operator()(const int x)const{
return a*x+b;
}
};
bool check(line l1,line l2,line l3){
return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=1e5+5;
vector<pii>adj[mxn];
bool isshop[mxn];
int dep[mxn],d[mxn];
int in[mxn],out[mxn];
int timer;
vector<tuple<int,int,int>>edge;
int jump[20][mxn];
int ord[mxn];
int dp[mxn];
int pre[mxn];
void dfs(int v,int pa){
in[v]=++timer;
ord[timer]=v;
jump[0][v]=pa;
pre[timer]=pre[timer-1]+isshop[v];
if(isshop[v])dp[v]=0;
else dp[v]=1e18;
for(auto [u,pp]:adj[v]){
if(u==pa)continue;
dep[u]=dep[v]+pp;
d[u]=d[v]+1;
dfs(u,v);
dp[v]=min(dp[v],dp[u]+pp);
}
out[v]=timer;
}
int lca(int v,int u){
if(dep[v]<dep[u])swap(v,u);
int dif=d[v]-d[u];
for(int i=0;i<20;i++){
if(dif>>i&1){
v=jump[i][v];
}
}
if(v==u)return v;
for(int i=19;i>=0;i--){
if(jump[i][v]!=jump[i][u]){
v=jump[i][v];
u=jump[i][u];
}
}
return jump[0][v];
}
int ans[mxn];
void calc(int v,int pa){
ans[v]=dp[v];
for(auto [u,dd]:adj[v]){
if(u==pa)continue;
dp[u]=min(dp[u],dp[v]+dd);
calc(u,v);
}
}
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,s,q,e;
cin>>n>>s>>q>>e;
for(int i=1;i<n;i++){
int a,b,c;
cin>>a>>b>>c;
adj[a].pb({b,c});
adj[b].pb({a,c});
edge.pb({a,b,c});
}
for(int i=1;i<=s;i++){
int x;
cin>>x;
isshop[x]=1;
}
dfs(e,0);
calc(e,0);
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++){
jump[i][j]=jump[i-1][jump[i-1][j]];
}
}
while(q--){
int id,v;
cin>>id>>v;
id--;
auto [x,y,l]=edge[id];
if(d[x]<d[y])swap(x,y);
debug(x);
if(in[x]<=in[v] and in[v]<=out[x]){
if(pre[out[x]]-pre[in[x]-1]==0){
cout<<"oo\n";
}
else{
cout<<ans[v]<<'\n';
}
}
else{
cout<<"escaped\n";
}
}
}
/*
input:
*/
Compilation message
valley.cpp: In function 'void dfs(long long int, long long int)':
valley.cpp:72:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for(auto [u,pp]:adj[v]){
| ^
valley.cpp: In function 'void calc(long long int, long long int)':
valley.cpp:101:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
101 | for(auto [u,dd]:adj[v]){
| ^
valley.cpp: In function 'int main()':
valley.cpp:136:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
136 | auto [x,y,l]=edge[id];
| ^
valley.cpp: In function 'void setIO(std::string)':
valley.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22616 KB |
Output is correct |
2 |
Incorrect |
5 ms |
22620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22616 KB |
Output is correct |
2 |
Incorrect |
5 ms |
22620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
33280 KB |
Output is correct |
2 |
Correct |
83 ms |
37236 KB |
Output is correct |
3 |
Correct |
85 ms |
36604 KB |
Output is correct |
4 |
Correct |
127 ms |
38652 KB |
Output is correct |
5 |
Correct |
84 ms |
38652 KB |
Output is correct |
6 |
Correct |
97 ms |
40788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22616 KB |
Output is correct |
2 |
Incorrect |
5 ms |
22620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |