//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 = 2e5, K = 300;
const ll MI = 2e18, mm = 1e15 + 5;
const double P = 3.14;
int n, s, q, e, p, tim = 1, tin[2 * N], tout[2 * N], up[N][20];
ll dist[N], dp[N], mn[N][20];
bool 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[v] = 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; i <= n; i++)
for (int j = 0; j < 20; j++)
mn[i][j] = 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;
// cout << v << el;
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 + dist[rr] >= 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;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
33836 KB |
Output is correct |
2 |
Correct |
154 ms |
33500 KB |
Output is correct |
3 |
Correct |
204 ms |
33556 KB |
Output is correct |
4 |
Correct |
197 ms |
35268 KB |
Output is correct |
5 |
Correct |
185 ms |
35396 KB |
Output is correct |
6 |
Correct |
234 ms |
37212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |