This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// absolutely incredible
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define left _________left
#define right _________right
#define NAME ""
const int mod = 1e9 + 7;
bool maximize(int &u, int v){
if(v > u){
u = v;
return true;
}
return false;
}
bool minimize(int &u, int v){
if(v < u){
u = v;
return true;
}
return false;
}
bool maximizell(long long &u, long long v){
if(v > u){
u = v;
return true;
}
return false;
}
bool minimizell(long long &u, long long v){
if(v < u){
u = v;
return true;
}
return false;
}
int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 1e5 + 5;
int n, q, s, e, cnt, in[maxN], out[maxN], par[17][maxN];
vector<pair<int, int>> adj[maxN];
pair<int, int> edge[maxN];
const long long INFLL = 1e18;
long long Min[17][maxN], f[maxN], d[maxN];
void dfs(int u = e){
in[u] = ++cnt;
for(auto [v, w] : adj[u]){
if(in[v])continue;
d[v] = d[u] + w;
par[0][v] = u;
dfs(v);
minimizell(f[u], f[v] + w);
}
long long rem = f[u] != INFLL ? f[u] - d[u] : INFLL;
for(auto [v, w] : adj[u]){
if(in[v] > in[u]){
Min[0][v] = rem;
}
}
out[u] = cnt;
}
bool inside(int u, int v){
return in[u] <= in[v] && out[u] >= out[v];
}
long long get(int c, int v){
long long res = f[c] != INFLL ? f[c] - d[c] : INFLL;
REP(i, 16, 0){
if(inside(v, par[i][c])){
minimizell(res, Min[i][c]);
c = par[i][c];
}
}
return res;
}
void solve(){
cin >> n >> s >> q >> e;
FOR(i, 1, n - 1){
int u, v, w;
cin >> u >> v >> w;
edge[i] = make_pair(u, v);
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
FOR(i, 1, n)f[i] = INFLL;
FOR(i, 1, s){
int u;
cin >> u;
f[u] = 0;
}
dfs();
FOR(j, 1, 16){
FOR(i, 1, n){
par[j][i] = par[j - 1][par[j - 1][i]];
Min[j][i] = min(Min[j - 1][i], Min[j - 1][par[j - 1][i]]);
}
}
while(q--){
int id, c;
cin >> id >> c;
auto [u, v] = edge[id];
if(in[u] > in[v])swap(u, v);
if(!inside(v, c)){
cout << "escaped\n";
continue;
}
long long rem = get(c, v);
if(rem == INFLL){
cout << "oo\n";
}else cout << rem + d[c] << "\n";
}
}
int main(){
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'int main()':
valley.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | freopen(NAME".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... |