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 range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))
template<class T> bool minimize(T& a, T b){
if(a > b) return a = b, true;
return false;
}
template<class T> bool maximize(T& a, T b){
if(a < b) return a = b, true;
return false;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int N = 1e5 + 5;
const ll inf = 1e16;
int n, q, s, root, h[N], lift[20][N], a[N], b[N], tin[N], tout[N], sz[N], timerDFS;
vector<pair<int, int>> g[N];
ll d[N], dp[N], rmq[20][N];
void dfsLCA(int u, int e){
tin[u] = ++timerDFS;
for(auto [v, w] : g[u]) if(v != e){
h[v] = h[u] + 1;
d[v] = d[u] + w;
lift[0][v] = u;
for(int i = 1; i <= 17; ++i){
lift[i][v] = lift[i - 1][lift[i - 1][v]];
}
dfsLCA(v, u);
minimize(dp[u], dp[v] + w);
sz[u] += sz[v];
}
tout[u] = timerDFS;
}
void dfsRMQ(int u, int e){
for(auto [v, w] : g[u]) if(v != e){
rmq[0][v] = dp[u] - d[u];
for(int i = 1; i <= 17; ++i){
rmq[i][v] = min(rmq[i - 1][v], rmq[i - 1][lift[i - 1][v]]);
}
dfsRMQ(v, u);
}
}
bool inSubtree(int u, int v){
return tin[u] <= tin[v] and tout[v] <= tout[u];
}
ll getLift(int u, int d){
ll ans = inf;
for(int i = 17; i >= 0; --i){
if(d >> i & 1){
minimize(ans, rmq[i][u]);
u = lift[i][u];
}
}
return ans;
}
void Zero_OP(){
cin >> n >> s >> q >> root;
for(int i = 1; i < n; ++i){
int u, v, c;
cin >> u >> v >> c;
g[u].push_back({v, c});
g[v].push_back({u, c});
a[i] = u, b[i] = v;
}
for(int i = 1; i <= n; ++i) dp[i] = inf;
for(int i = 1; i <= s; ++i){
int x; cin >> x;
dp[x] = 0; sz[x] = 1;
}
dfsLCA(root, -1);
dfsRMQ(root, -1);
while(q--){
int i, v;
cin >> i >> v;
int u = (h[a[i]] < h[b[i]] ? b[i] : a[i]);
if(!inSubtree(u, v)) cout << "escaped\n";
else {
ll ans = min(dp[v], d[v] + getLift(v, h[v] - h[u]));
if(ans == inf){
cout << "oo\n";
}
else cout << ans << '\n';
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("antuvu.inp", "r")){
freopen("antuvu.inp", "r", stdin);
freopen("antuvu.out", "w", stdout);
}
Zero_OP();
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void dfsLCA(int, int)':
valley.cpp:31:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for(auto [v, w] : g[u]) if(v != e){
| ^
valley.cpp: In function 'void dfsRMQ(int, int)':
valley.cpp:46:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for(auto [v, w] : g[u]) if(v != e){
| ^
valley.cpp: In function 'int main()':
valley.cpp:110:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen("antuvu.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:111:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen("antuvu.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... |