이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
return l + abs((ll) rng()) % (r - l + 1);
}
const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 123456789;
const int N = 1e5 + 5, BASE = 307, LOG = 20;
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
int n, S, Q, H;
struct Edge {
int u, v, w;
void input() {
cin >> u >> v >> w;
}
} e[N];
vector<pair<int, int>> g[N];
bool rescue[N];
int haveIn[N];
int timer = 0;
int in[N], out[N];
int h[N];
int par[N][LOG + 5];
void calc(int u, int p) {
in[u] = ++timer;
haveIn[u] = rescue[u];
forn(i, 1, LOG) par[u][i] = par[par[u][i-1]][i-1];
for (pair<int, int> i : g[u]) {
int v = i.first;
if (v == p) continue;
par[v][0] = u;
h[v] = h[u] + 1;
calc(v, u);
haveIn[u] += haveIn[v];
}
out[u] = timer;
}
ll dist[N];
ll dp[N], value[N][LOG + 5];
void dfs(int u, int p) {
dp[u] = rescue[u] ? 0 : oo;
for (pair<int, int> i : g[u]) {
int v = i.first;
if (v == p) continue;
dist[v] = dist[u] + e[i.second].w;
dfs(v, u);
minimize(dp[u], dp[v] + e[i.second].w);
}
}
void build_stb(int u, int p) {
value[u][0] = min(-dist[u] + dp[u], -dist[p] + dp[p]);
forn(i, 1, LOG) {
value[u][i] = min(value[u][i-1], value[par[u][i-1]][i-1]);
}
for (pair<int, int> i : g[u]) if (i.first != p) {
build_stb(i.first, u);
}
return;
}
bool isIn(int root, int u) {
return in[root] <= in[u] && in[u] <= out[root];
}
int lift(int u, int k) {
for (; k > 0; k -= k & -k)
u = par[u][ctz(k)];
return u;
}
ll getMin(int u, int k) {
ll ans = oo;
for (; k > 0; k -= k & -k) {
minimize(ans, value[u][ctz(k)]);
u = par[u][ctz(k)];
}
return ans;
}
signed main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> S >> Q >> H;
rep(i, n - 1) {
e[i].input();
g[e[i].u].pb(mp(e[i].v, i));
g[e[i].v].pb(mp(e[i].u, i));
}
rep(i, S) {
int u;
cin >> u;
rescue[u] = true;
}
calc(H, 0);
dfs(H, 0);
memset(value, 0x3f, sizeof value);
build_stb(H, 0);
#define ins(x) { cout << x << '\n'; }
rep(_, Q) {
int ban, node;
cin >> ban >> node;
Edge cur = e[ban];
if (h[cur.u] > h[cur.v])
swap(cur.u, cur.v);
if (!isIn(cur.v, node)) {
ins("escaped");
continue;
}
if (!haveIn[cur.v]) {
ins("oo");
continue;
}
ll ans = oo;
if (haveIn[node]) minimize(ans, dp[node]);
minimize(ans, getMin(node, h[node] - h[cur.v]) + dist[node]);
ins(ans);
}
return 0;
}
# | 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... |