이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//bit chass 1
#include <bits/stdc++.h>
#define x first
#define y second
#define el "\n"
#define ll long long
#define pb push_back
#define pll pair <ll, ll>
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
#define lcm(x,y) x * y / __gcd(x, y)
#define ibase ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;
const int N = 1e5 + 5, inf = 1e9 + 7, M = 2e6, mm = 1e15 + 5, K = 300;
const ll MI = 2e18;
const double P = 3.14;
int n, s, q, e, p, tim = 1, tin[N], tout[N], up[N][20];
ll dist[N], dp[N], mn[N][20];
bool ok, c[N];
vector <pair <int, int>> g[N];
pair <int, int> ed[N];
void dfs(int v, int pr = 0) {
	tin[v] = tim++;
	if (c[v])
		dp[v] = 0;
	up[v][0] = pr;
	for (int i = 1; i < 20; i++) 
		up[v][i] = up[up[v][i - 1]][i - 1];
	for (auto to : g[v]) {
		if (to.x != pr) {
			dist[to.x] = dist[v] + to.y;
			dfs(to.x, v);
			dp[v] = min(dp[to.x] + to.y, dp[v]);
		}
	}
	tout[tim]++;
}
void dfs2(int v, int pr) {
	mn[v][0] = dp[pr] - dist[pr];
	for (int i = 1; i < 20; i++)
		mn[v][i] = min(mn[up[v][i - 1]][i - 1], mn[v][i - 1]);
	for (pii to : g[v])
		if (to.x != pr)
			dfs2(to.x, v);
}
bool upper(int x, int y) {
	return (tin[x] < tin[y] && tout[y] < tout[x]);
}
void solve() {
	cin >> n >> s >> q >> e;
	for (int i = 1, x, y, w; i < n; i++) {
		cin >> x >> y >> w;
		g[x].pb({y, w});
		g[y].pb({x, w});
		ed[i] = {x, y};
		dp[i] = dp[i + 1] = mm;
	}
	for (int i = 1, x; i <= s; i++) {
		cin >> x;
		c[x] = 1;
	}
	dfs(e);
	dfs2(e, 0);
	while (q--) {
		int i, r, v;
		cin >> i >> r;
		pii x = ed[i];
		if (tin[x.x] <= tin[x.y])
			v = x.y;
		else 
			v = x.x;
		if (tin[v] <= tin[r] && tout[r] <= tout[v]) {
			if (v == r) {
				if (dp[v] == mm)
					cout << "oo" << el;
				else
					cout << dp[v] << el;
				continue;
			}
			ll ans = mm, rr = r;
			for (int i = 19; i >= 0; i--) {
				if (up[r][i] != 0 && !upper(up[r][i], v))
					ans = min(mn[r][i], ans), r = up[r][i];
			}
			ans = min(ans, mn[r][0]);
			if (ans == mm && dp[rr] == mm)
				cout << "oo" << el;
			else 	
				cout << min(ans + dist[rr], dp[rr]) << el;
		}
		else {
			cout << "escaped" << el;
		}
		
	}
}
int main() {
	ibase;
	int T = 1;
//	cin >> T;
	for (int i = 1; i <= T; i++) {
//		cout << "Case " << i << ": ";
		solve();
		cout << el;
	}
}
컴파일 시 표준 에러 (stderr) 메시지
valley.cpp:17:58: warning: overflow in conversion from 'double' to 'int' changes value from '1.000000000000005e+15' to '2147483647' [-Woverflow]
   17 | const int N = 1e5 + 5, inf = 1e9 + 7, M = 2e6, mm = 1e15 + 5, K = 300;
      |                                                     ~~~~~^~~| # | 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... |