# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
937055 | Neco_arc | Valley (BOI19_valley) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <bits/debug.hpp>
#endif // ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Alpine valley"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#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 maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, m, q, spe;
ll h[maxn], amin[maxn][23], Val[maxn];
int cl[maxn], In[maxn], Out[maxn], depth[maxn], cha[maxn][23];
pair<int, int> E[maxn];
vector<pair<int, ll>> edges[maxn];
void DFS(int u, int par)
{
static int Time = 0;
In[u] = ++Time;
Val[u] = cl[u] ? (-h[u]) : 1e15;
for(pair<int, ll> x : edges[u]) if(x.first != par)
{
ll v = x.first, w = x.second;
h[v] = h[u] + w, depth[v] = depth[u] + 1;
DFS(v, u);
Val[u] = min(Val[u], Val[v] - 2 * h[u] + 2 * h[v]);
}
Out[u] = Time;
}
void precalc(int u, int par)
{
for(pair<int, ll> x : edges[u]) if(x.first != par)
{
ll v = x.first, w = x.second;
amin[v][0] = Val[u], cha[v][0] = u;
fi(i, 1, 20) {
cha[v][i] = cha[cha[v][i - 1]][i - 1];
amin[v][i] = min(amin[v][i - 1], amin[cha[v][i - 1]][i - 1]);
}
precalc(v, u);
}
}
void solve()
{
cin >> n >> m >> q >> spe;
fi(i, 1, n - 1)
{
int x, y, w;
cin >> x >> y >> w;
edges[x].push_back({y, w}), edges[y].push_back({x, w});
E[i] = {x, y};
}
fi(i, 1, m) {
int x;
cin >> x, cl[x] = 1;
}
DFS(spe, 0), precalc(spe, 0);
fi(_, 1, q) {
int id, u, p, x;
cin >> id >> x;
tie(u, p) = E[id];
if(In[u] > In[p]) swap(u, p);
if(!(In[p] <= In[x] && In[x] <= Out[p])) cout << "escaped\n";
else
{
ll ans = Val[x], w = h[x];
fid(i, 20, 0) if(depth[x] - (1 << i) >= depth[p]) ans = min(ans, amin[x][i]), x = cha[x][i];
if(ans + w >= 1e15) cout << "oo\n";
else cout << ans + w << '\n';
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}