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 pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 1e5 + 10;
int root, tmr;
int tin[MAXN], tout[MAXN];
set <pii> g[MAXN];
pii EDGE[MAXN];
int shop[MAXN], is_shop[MAXN];
pii query[MAXN];
int n, s, q;
void dfs_calc(int v = root, int p = 0) {
tin[v] = ++tmr;
for(auto to : g[v])
if(to.f != p)
dfs_calc(to.f, v);
tout[v] = ++tmr;
}
bool upper(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
ll res_sub12;
void dfs_sub12(int v, int p, int blu, int blv, ll sum) {
if(is_shop[v]) res_sub12 = min(res_sub12, sum);
for(auto to : g[v]) {
if(to.f != p)
if(v == blu && to.f == blv) continue;
else dfs_sub12(to.f, v, blu, blv, sum + to.s);
}
}
void sub12() {
for(int i = 1; i <= q; ++i) {
int blv = EDGE[query[i].f].f, blu = EDGE[query[i].f].s, v = query[i].s;
if(!upper(blv, blu)) swap(blv, blu);
if(upper(blu, v)) {
res_sub12 = 1e18;
dfs_sub12(v, 0, blu, blv, 0);
if(res_sub12 == 1e18) cout << "oo\n";
else cout << res_sub12 << '\n';
} else cout << "escaped\n";
}
exit(0);
}
void sub3() {
for(int i = 1; i <= q; ++i) {
int blv = EDGE[query[i].f].f, blu = EDGE[query[i].f].s, v = query[i].s;
if(!upper(blv, blu)) swap(blv, blu);
if(upper(blu, v)) cout << "0\n";
else cout << "escaped\n";
}
exit(0);
}
void solve() {
cin >> n >> s >> q >> root;
for(int i = 1; i < n; ++i) {
int u, v, cost;
cin >> u >> v >> cost;
g[u].insert(mp(v, cost));
g[v].insert(mp(u, cost));
EDGE[i] = mp(u, v);
}
for(int i = 1; i <= s; ++i)
cin >> shop[i], is_shop[shop[i]] = 1;
for(int i = 1; i <= q; ++i)
cin >> query[i].f >> query[i].s;
dfs_calc();
if((n <= 100 && q <= 10000) || (n <= 1000 && q <= 1000)) sub12();
if(s == n) sub3();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
///cin >> T;
while(T--) solve(), cout << '\n';
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void dfs_sub12(int, int, int, int, long long int)':
valley.cpp:50:11: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
50 | if(to.f != p)
| ^
# | 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... |