이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define name "cownav"
#define fi(i,a,b) for(int i = a; i <= b; i++)
#define fid(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define f first
#define se second
#define pii pair<int, int>
#define getbit(i, j) ((i >> j) & 1)
#define pb push_back
#define all(v) v.begin(), v.end()
#define maxn 100005
const int M = 1e9 + 7;
using namespace std;
int n, s, q, root, num[maxn], en[maxn], h[maxn], cha[maxn][20], cnt;
vector <pii> g[maxn];
pii a[maxn];
ll down[maxn], d[maxn], mi[maxn][20];
void dfs(int u, int fa){
num[u] = ++cnt;
for(auto [v, w]: g[u]) if(v != fa){
h[v] = h[u] + 1;
d[v] = d[u] + w;
dfs(v, u);
down[u] = min(down[u], down[v] + w);
}
en[u] = cnt;
}
void build(int u, int fa){
for(auto [v, w]: g[u]) if(v != fa){
cha[v][0] = u;
mi[v][0] = down[v] - d[v];
fi(i, 1, 19){
cha[v][i] = cha[cha[v][i - 1]][i - 1];
mi[v][i] = min(mi[v][i - 1], mi[cha[v][i - 1]][i - 1]);
}
build(v, u);
}
}
ll get_mi(int r, int v){
ll Mi = 1e18;
int kc = h[r] - h[v];
fi(i, 0, 19) if(getbit(kc, i)){
Mi = min(Mi, mi[r][i]);
r = cha[r][i];
}
Mi = min(Mi, mi[v][0]);
return Mi;
}
bool in(int u, int v){
return (num[u] <= num[v] && num[v] <= en[u]);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
//freopen(name".in","r",stdin);
//freopen(name".out","w",stdout);
cin >> n >> s >> q >> root;
fi(i, 1, n - 1){
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
a[i] = {u, v};
}
fi(i, 1, n){
down[i] = 1e18;
fi(j, 0, 19) mi[i][j] = 1e18;
}
fi(i, 1, s){
int x; cin >> x;
down[x] = 0;
}
dfs(root, 0);
build(root, 0);
while(q--){
int id, r; cin >> id >> r;
auto [u, v] = a[id];
if(h[v] < h[u]) swap(u, v); //assume that cha[v][0] = u;
if(!in(v, r)){cout << "escaped" << '\n'; continue;}
if(down[v] == 1e18) {cout << "oo" << '\n'; continue;}
cout << d[r] + get_mi(r, v) << '\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... |