#P6227. Alpine Valley Escape
Alpine Valley Escape
Alpine Valley Escape
In the Alpine valley, there are \(N\) villages numbered \(1, 2, \ldots, N\) connected by \(N-1\) edges forming a tree. Only \(S\) of the villages house stores, and there is a unique exit at village \(E\). This winter a heavy snowfall renders one of the roads impassable. Although you know one road is blocked, you do not know which one in advance. Moreover, your friends live in various villages.
You need to answer \(Q\) queries. For each query, you are given a blocked road (specified by two village numbers) and the village where a friend is located. Determine if the friend can still leave the valley, that is, if there is a route from their village to the exit village \(E\) that does not use the blocked road. If it is possible, output escaped
. Otherwise, compute and output the minimum travel time required to reach any store in the connected component containing the friend. If there is no store reachable, output \(-1\).
The tree edges have an associated travel time. All routes are undirected and the blocked road is removed from the tree for that query only.
inputFormat
The first line contains four integers: \(N\) \(S\) \(Q\) \(E\) — the number of villages, the number of villages with a store, the number of queries, and the exit village respectively.
The second line contains \(S\) distinct integers indicating the villages with a store.
The next \(N-1\) lines each contain three integers \(u\), \(v\), \(w\) representing a bidirectional road connecting villages \(u\) and \(v\) with travel time \(w\) (\(w > 0\)).
The following \(Q\) lines each contain three integers: \(u\) \(v\) \(x\), where the road between villages \(u\) and \(v\) is blocked and \(x\) is the village where a friend is located.
outputFormat
For each query, output a single line. If the friend can reach village \(E\) (the exit) in the modified tree (with the blocked road removed), output escaped
(without quotes). Otherwise, output a single integer representing the minimum travel time from village \(x\) to any store in the same connected component. If no store is reachable, output \(-1\).
sample
5 2 3 5
2 3
1 2 3
2 3 4
2 4 2
4 5 5
2 3 1
2 4 3
4 5 1
escaped
0
3
</p>