This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#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;
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);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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';
}
}
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void precalc(int, int)':
valley.cpp:38:25: warning: unused variable 'w' [-Wunused-variable]
38 | ll v = x.first, w = x.second;
| ^
# | 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... |