This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int n, s, q, e;
VVPI gp;
VI tin, tout, depth;
int tc;
VVI par;
VVL parv, parw;
VL dp;
VI isshop;
void dfs1(int u, int p, int pw = -1) {
tin[u] = tc++;
if (isshop[u]) dp[u] = 0;
else dp[u] = 1e18;
for (auto[v, w] : gp[u]) if (v != p) {
depth[v] = depth[u] + 1;
dfs1(v, u, w);
setmin(dp[u], dp[v]+w);
}
par[u][0] = p;
parv[u][0] = dp[u];
parw[u][0] = pw;
tout[u] = tc++;
}
bool isanc(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
ll binlift(int u, int k) {
ll ans = 1e18;
ll lift = 0;
rep(i, 20) if (k & (1 << i)) {
/* cout << (1<<i) << " hat verev elav " << endl; */
setmin(ans, parv[u][i] + lift);
lift += parw[u][i];
u = par[u][i];
}
return ans;
}
void solve() {
cin >> n >> s >> q >> e;
e--;
gp = VVPI(n);
VPI edges;
rep(i, n-1) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
gp[u].pb({v, w});
gp[v].pb({u, w});
edges.pb({u, v});
}
isshop = VI(n);
rep(i, s) {
int x;
cin >> x;
x--;
isshop[x] = true;
}
tin = tout = depth = VI(n);
tc = 0;
dp = VL(n);
par = VVI(n, VI(20));
parv = VVL(n, VL(20));
parw = VVL(n, VL(20));
dfs1(e, e);
/* rep(u, n) cin >> parv[u][0]; */
replr(i, 1, 19) rep(u, n) {
int m = par[u][i-1];
par[u][i] = par[m][i-1];
parw[u][i] = parw[u][i-1] + parw[m][i-1];
parv[u][i] = min(parv[u][i-1], parw[u][i-1] + parv[m][i-1]);
}
for (auto&[u, v] : edges) if (!isanc(u, v)) swap(u, v);
/* rep(u, n) cout << "dp[" << u+1 << "] = " << dp[u] << endl; */
/* rep(u, n) { */
/* cout << u+1 << ": " << parv[u][0] << " " << parv[u][1] << " " << par[u][2] << endl; */
/* } */
while (q--) {
int edgei, u;
cin >> edgei >> u;
edgei--, u--;
int s = edges[edgei].ss;
if (!isanc(s, u)) {
cout << "escaped" << endl;
} else {
ll ans = binlift(u, depth[u] - depth[s] + 1);
if (ans == 1e18) cout << "oo" << endl;
else cout << ans << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1;
/* cin >> t; */
while (t--) solve();
}
Compilation message (stderr)
valley.cpp: In function 'void dfs1(int, int, int)':
valley.cpp:46:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for (auto[v, w] : gp[u]) if (v != p) {
| ^
valley.cpp: In function 'void solve()':
valley.cpp:106:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
106 | for (auto&[u, v] : edges) if (!isanc(u, v)) swap(u, v);
| ^
# | 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... |