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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
#define sz(x) int(x.size())
const int mx = 100'001;
const int lg = 17;
const ll inf = 1'000'000'000'000'000'000LL;
int N, S, Q, E;
vi A(1+mx), B(1+mx);
vll W(1+mx);
vi edgeind[1+mx];
vll nearshop(1+mx, inf); //distance to closest shop within subtree
vi parent(1+mx, 0);
vi paredge(1+mx, 0);
vll distE(1+mx, 0);
vi depth(1+mx, 0);
vi edgelower(1+mx, 0);
int dfsct = 0;
vi dfsind(1+mx, 0);
vi subtree(1+mx, 1);
void dfs(int u, int p)
{
dfsct++;
dfsind[u] = dfsct;
for(int e : edgeind[u])
{
int v = A[e] + B[e] - u;
ll w = W[e];
if(v == p)
continue;
parent[v] = u;
paredge[v] = e;
edgelower[e] = v;
distE[v] = distE[u] + w;
depth[v] = depth[u] + 1;
dfs(v, u);
nearshop[u] = min(nearshop[u], nearshop[v] + w);
subtree[u] += subtree[v];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> S >> Q >> E;
for(int e = 1; e <= N-1; e++)
{
cin >> A[e] >> B[e] >> W[e];
edgeind[A[e]].push_back(e);
edgeind[B[e]].push_back(e);
}
for(int x = 1; x <= S; x++)
{
int u;
cin >> u;
nearshop[u] = 0;
}
dfs(E, 0);
parent[E] = E;
ll lft[1+N][1+lg];
int anc[1+N][1+lg];
for(int i = 1; i <= N; i++)
{
lft[i][0] = nearshop[i] - distE[i];
anc[i][0] = parent[i];
}
for(int e = 1; e <= lg; e++)
{
for(int i = 1; i <= N; i++)
{
anc[i][e] = anc[ anc[i][e-1] ][e-1];
lft[i][e] = min(lft[i][e-1], lft[ anc[i][e-1] ][e-1]);
}
}
// cerr << "nearshop = ";
// for(int i = 1; i <= N; i++)
// cerr << nearshop[i] << ' ';
// cerr << '\n';
for(int q = 1; q <= Q; q++)
{
int I, R;
cin >> I >> R;
bool escape;
int A;
if(R == E)
escape = 1;
else
{
A = edgelower[I];
if(dfsind[A] <= dfsind[R] && dfsind[R] < dfsind[A] + subtree[A])
escape = 0;
else
escape = 1;
}
if(escape)
cout << "escaped\n";
else
{
if(nearshop[A] >= inf)
cout << "oo\n";
else
{
// cerr << "A = " << A << '\n';
ll res = inf;
int u = R;
for(int e = lg; e >= 0; e--)
{
if((1 << e) & (depth[u] - depth[A] + 1))
{
// cerr << "jump by " << (1<<e) << '\n';
res = min(res, distE[R] + lft[u][e]);
u = anc[u][e];
}
}
cout << res << '\n';
}
}
}
}
# | 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... |