이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1e5 + 5, lg = 20;
const int inf = 1e18 + 7;
int n, s, q, r, timer, h[maxn], val[maxn], dp[maxn][lg], par[maxn][lg];
int ras[maxn], in[maxn], out[maxn];
vector<pair<int, pii>> adj[maxn];
bool mark[maxn];
void dfs(int v, int p){
in[v] = timer++;
par[v][0] = p;
val[v] = inf;
if(mark[v]) val[v] = h[v];
for(auto [u, e]: adj[v]){
if(u != p){
h[u] = h[v] + e.F;
ras[e.S] = u;
dfs(u, v);
smin(val[v], val[u]);
}
}
dp[v][0] = val[v] - 2 * h[v];
out[v] = timer;
}
bool is_par(int u, int v){
return in[u] <= in[v] && out[u] >= out[v];
}
int lca(int u, int v){
if(is_par(u, v)) return u;
if(is_par(v, u)) return v;
for(int b = lg - 1; b >= 0; b--){
if(!is_par(par[v][b], u)) v = par[v][b];
}
return par[v][0];
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s >> q >> r;
for(int i = 1; i < n; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].pb(mp(v, mp(w, i)));
adj[v].pb(mp(u, mp(w, i)));
}
for(int i = 0; i < s; i++){
int u; cin >> u;
mark[u] = 1;
}
dfs(r, r);
for(int i = 0; i < lg; i++) dp[0][i] = inf;
for(int b = 1; b < lg; b++){
for(int i = 1; i <= n; i++){
par[i][b] = par[par[i][b - 1]][b - 1];
dp[i][b] = dp[i][b - 1];
smin(dp[i][b], dp[par[i][b - 1]][b - 1]);
}
}
while(q--){
int ind, v; cin >> ind >> v;
int u = ras[ind];
if(lca(u, v) != u) cout << "escaped\n";
else{
int x = v;
int ans = dp[u][0];
for(int b = 0; b < lg; b++){
if((h[v] - h[u]) & (1ll << b)){
smin(ans, dp[v][b]);
v = par[v][b];
}
}
ans += h[x];
if(ans <= n) cout << ans << '\n';
else cout << "oo\n";
}
}
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... |