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 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);
for(int q = 1; q <= Q; q++)
{
int I, R;
cin >> I >> R;
bool escape;
if(R == E)
escape = 1;
else
{
int 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
cout << 0 << '\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... |