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;
int level[N], up[20][N], sum[20][N];
vector<vector<pair<int, int>>> g(N);
void dfs(int v, int pr, int d = 0) {
up[0][v] = pr;
sum[0][v] = d;
level[v] = level[pr] + 1;
for (int i = 1; i <= 18; i++) {
up[i][v] = up[i - 1][up[i - 1][v]];
sum[i][v] = sum[i - 1][v] + sum[i - 1][up[i - 1][v]];
}
for (auto [to, len]: g[v]) {
if (to == pr)
continue;
dfs(to, v, 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];
}
int sumlca(int x, int y) {
if (level[x] > level[y])
swap(x, y);
int res = 0;
for (int i = 18; i >= 0; i--) {
if (level[up[i][y]] >= level[x]) {
res += sum[i][y];
y = up[i][y];
}
}
if (x == y)
return res;
for (int i = 18; i >= 0; i--) {
if (up[i][x] != up[i][y]) {
res += sum[i][x];
res += sum[i][y];
x = up[i][x];
y = up[i][y];
}
}
res += sum[0][x] + sum[0][y];
return res;
}
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};
}
dfs(1, 0);
vector<int> c(s + 1);
for (int i = 1; i <= s; i++)
cin >> c[i];
while (q--) {
int b, v;
cin >> b >> v;
ll x = edges[b].first, y = edges[b].second;
if (level[x] > level[y])
swap(x, y);
if (lca(v, y) == y) {
if (lca(y, e) == y) {
cout << "escaped\n";
continue;
}
int ans = 100000000000;
for (int i = 1; i <= s; i++) {
if (lca(c[i], y) == y)
ans = min(ans, sumlca(c[i], v));
}
if (ans == 100000000000)
cout << "oo\n";
else
cout << ans << '\n';
} else {
if (lca(y, e) != y) {
cout << "escaped\n";
continue;
}
int ans = 100000000000;
for (int i = 1; i <= s; i++) {
if (lca(c[i], y) != y)
ans = min(ans, sumlca(c[i], v));
}
if (ans == 100000000000)
cout << "oo\n";
else
cout << ans << '\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:195:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
195 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:196:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
196 | 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... |