이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#ifdef LOCAL
#include "../src/debug.hpp"
#else
#define debug(...) 420
#endif
// g++ -DLOCAL -Wall Practice.cpp -o bin
template<class T> bool smax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template<class T> bool smin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
void solve() {
int n, s, q, e;
cin >> n >> s >> q >> e; --e;
vector<vector<pii>> adj(n);
vector<array<int, 3>> edges(n - 1);
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
edges[i - 1] = {u, v, w};
}
const ll INF = 1e18;
vector<ll> dp(n, INF), dist(n);
for (int i = 0; i < s; i++) {
int c; cin >> c;
dp[--c] = 0;
}
const int LOG = 32 - __builtin_clz(n);
vector lift(LOG, vector(n, 0));
vector data(LOG, vector(n, 0ll));
vector<int> dep(n);
auto dfs = [&](int u, int p, auto&& dfs) -> void {
if (p != -1) lift[0][u] = p;
for (int i = 1; i < LOG; i++) {
lift[i][u] = lift[i - 1][lift[i - 1][u]];
}
for (auto [v, w] : adj[u]) if (v != p) {
dist[v] = dist[u] + w;
dep[v] = dep[u] + 1;
dfs(v, u, dfs);
smin(dp[u], dp[v] + w);
}
}; dfs(0, -1, dfs);
auto dfs2 = [&](int u, int p, auto&& dfs2) -> void {
for (auto [v, w] : adj[u]) if (v != p) {
data[0][v] = dp[u] - dist[u];
for (int i = 1; i < LOG; i++) {
data[i][v] = min(data[i - 1][v], data[i - 1][lift[i - 1][v]]);
}
dfs2(v, u, dfs2);
}
}; dfs2(0, -1, dfs2);
for (auto &[u, v, w] : edges) {
if (dep[u] < dep[v]) swap(u, v);
}
auto qry = [&](int idx, int r) -> void {
int dx = dep[r] - dep[edges[idx][0]], ptr = r;
for (int i = LOG - 1; i >= 0; i--) {
if (dx >> i & 1) ptr = lift[i][ptr];
}
if (ptr == edges[idx][0]) {
ll res = INF;
ptr = r;
for (int i = LOG - 1; i >= 0; i--) {
if (dx >> i & 1) {
smin(res, data[i][ptr]);
ptr = lift[i][ptr];
}
}
res = min(res + dist[r], dp[r]);
cout << (res < INF ? to_string(res) : "oo") << "\n";
} else {
cout << "escaped" << "\n";
}
};
while (q--) {
int i, r;
cin >> i >> r;
qry(i - 1, r - 1);
}
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
int t = 1; // cin >> t;
while (t--) solve();
}
# | 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... |