This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
#define int ll
const int N = 2e5 + 13, INF = 1e18 + 14;
int level[N], up[20][N], dist[N], dp[N], mn[20][N];
set<int> shop;
vector<vector<pair<int, int>>> g(N);
void dfs(int v, int pr, int d = 0) {
up[0][v] = pr;
level[v] = level[pr] + 1;
dist[v] = dist[pr] + d;
for (int i = 1; i <= 18; i++)
up[i][v] = up[i - 1][up[i - 1][v]];
dp[v] = INF + INF;
if (shop.count(v))
dp[v] = 0;
for (auto [to, len]: g[v]) {
if (to == pr)
continue;
dfs(to, v, len);
dp[v] = min(dp[v], dp[to] + len);
}
}
int lca(int x, int y) {
if (level[x] > level[y])
swap(x, y);
for (int i = 18; i >= 0; i--) {
if (level[up[i][y]] >= level[x])
y = up[i][y];
}
if (x == y)
return x;
for (int i = 18; i >= 0; i--) {
if (up[i][x] != up[i][y]) {
x = up[i][x];
y = up[i][y];
}
}
return up[0][x];
}
void solve() {
int n, s, q, e;
cin >> n >> s >> q >> e;
vector<pair<int, int>> edges(n);
for (int i = 1; i < n; i++) {
int x, y, z;
cin >> x >> y >> z;
g[x].push_back({y, z});
g[y].push_back({x, z});
edges[i] = {x, y};
}
vector<int> c(s + 1);
for (int i = 1; i <= s; i++) {
cin >> c[i];
shop.insert(c[i]);
}
dfs(e, 0);
for (int j = 0; j <= 18; j++) {
for (int v = 1; v <= n; v++) {
if (!j)
mn[j][v] = dp[v] - dist[v];
else
mn[j][v] = min(mn[j - 1][v], mn[j - 1][up[j - 1][v]]);
}
}
while (q--) {
int edge, v;
cin >> edge >> v;
int x = edges[edge].first, y = edges[edge].second;
if (level[x] > level[y])
swap(x, y);
if (lca(y, v) == y) {
int res = INF;
int _v = v;
for (int i = 18; i >= 0; i--) {
if (level[up[i][v]] >= level[y]) {
res = min(res, mn[i][v]);
v = up[i][v];
}
}
res = min(res, dp[y] - dist[y]);
if (res >= INF)
cout << "oo\n";
else
cout << dist[_v] + res << '\n';
} else {
cout << "escaped\n";
}
}
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message (stderr)
valley.cpp: In function 'void setIO(const string&)':
valley.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:176:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
176 | freopen((f + ".out").c_str(), "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... |