#include <bits/stdc++.h>
using namespace std;
#define int long long
int inf = 1e18;
int n, s, q, e;
vector<pair<int, int>> g[100005], f[100005];
pair<int, int> t[100005];
int h[100005], d[100005], v[100005], h_real[100005];
int x[100005], y[100005], w[100005];
bool yup[100005], viz[100005];
int lg[100005];
int bl[20][100005], vl[20][100005];
void dfs(int nod)
{
viz[nod] = true;
d[nod] = inf;
if (yup[nod])
d[nod] = 0;
for (auto vecin : g[nod])
{
if (!viz[vecin.first])
{
h[vecin.first] = h[nod] + vecin.second;
h_real[vecin.first] = 1 + h_real[nod];
t[vecin.first] = {nod, vecin.second};
f[nod].push_back(vecin);
dfs(vecin.first);
d[nod] = min(d[nod], d[vecin.first] + vecin.second);
}
}
v[nod] = d[nod] - h[nod];
}
int anc(int nod, int dh)
{
for (int pas = lg[n]; pas >= 0; pas--)
{
if (dh - (1 << pas) >= 0)
{
dh -= (1 << pas);
nod = bl[pas][nod];
}
}
return nod;
}
int vv(int nod, int dh)
{
int nod1 = nod;
dh++;
int mnm = inf;
for (int pas = lg[n]; pas >= 0; pas--)
{
if (dh - (1 << pas) >= 0)
{
mnm = min(mnm, vl[pas][nod]);
nod = bl[pas][nod];
dh -= (1 << pas);
}
}
return mnm + h[nod1];
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> s >> q >> e;
for (int i = 1; i < n; i++)
{
cin >> x[i] >> y[i] >> w[i];
g[x[i]].push_back({y[i], w[i]});
g[y[i]].push_back({x[i], w[i]});
}
for (int i = 1; i <= s; i++)
{
int oras;
cin >> oras;
yup[oras] = true;
}
dfs(e);
for (int i = 1; i < n; i++)
{
if (h[x[i]] > h[y[i]])
swap(x[i], y[i]);
}
for (int i = 2; i <= n; i++)
lg[i] = 1 + lg[i / 2];
for (int i = 1; i <= n; i++)
bl[0][i] = t[i].first;
for (int j = 1; j <= lg[n]; j++)
for (int i = 1; i <= n; i++)
bl[j][i] = bl[j - 1][bl[j - 1][i]];
for (int i = 1; i <= n; i++)
vl[0][i] = v[i];
//for (int i = 1; i <= n; i++)
// cout << v[i] << '\n';
for (int j = 1; j <= lg[n]; j++)
for (int i = 1; i <= n; i++)
vl[j][i] = min(vl[j - 1][i], vl[j - 1][bl[j - 1][i]]);
for (int i = 1; i <= q; i++)
{
int idx, r;
cin >> idx >> r;
if (h_real[r] < h_real[y[idx]] or anc(r, h_real[r] - h_real[y[idx]]) != y[idx])
cout << "escaped\n";
else
{
int dh = h_real[r] - h_real[y[idx]];
int ans = vv(r, dh);
if (ans >= inf / 10)
cout << "oo\n";
else
cout << ans << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5464 KB |
Output is correct |
2 |
Correct |
4 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5464 KB |
Output is correct |
2 |
Correct |
4 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5616 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5464 KB |
Output is correct |
13 |
Correct |
3 ms |
5468 KB |
Output is correct |
14 |
Correct |
3 ms |
5616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
51576 KB |
Output is correct |
2 |
Correct |
94 ms |
51616 KB |
Output is correct |
3 |
Correct |
103 ms |
51752 KB |
Output is correct |
4 |
Correct |
113 ms |
54096 KB |
Output is correct |
5 |
Correct |
108 ms |
54096 KB |
Output is correct |
6 |
Correct |
139 ms |
56844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5464 KB |
Output is correct |
2 |
Correct |
4 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5616 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5464 KB |
Output is correct |
13 |
Correct |
3 ms |
5468 KB |
Output is correct |
14 |
Correct |
3 ms |
5616 KB |
Output is correct |
15 |
Correct |
90 ms |
51576 KB |
Output is correct |
16 |
Correct |
94 ms |
51616 KB |
Output is correct |
17 |
Correct |
103 ms |
51752 KB |
Output is correct |
18 |
Correct |
113 ms |
54096 KB |
Output is correct |
19 |
Correct |
108 ms |
54096 KB |
Output is correct |
20 |
Correct |
139 ms |
56844 KB |
Output is correct |
21 |
Correct |
88 ms |
51024 KB |
Output is correct |
22 |
Correct |
94 ms |
51156 KB |
Output is correct |
23 |
Correct |
99 ms |
51280 KB |
Output is correct |
24 |
Correct |
103 ms |
53664 KB |
Output is correct |
25 |
Correct |
137 ms |
57428 KB |
Output is correct |
26 |
Correct |
99 ms |
51028 KB |
Output is correct |
27 |
Correct |
123 ms |
51252 KB |
Output is correct |
28 |
Correct |
102 ms |
51280 KB |
Output is correct |
29 |
Correct |
104 ms |
53072 KB |
Output is correct |
30 |
Correct |
132 ms |
54984 KB |
Output is correct |
31 |
Correct |
91 ms |
51208 KB |
Output is correct |
32 |
Correct |
91 ms |
51280 KB |
Output is correct |
33 |
Correct |
104 ms |
51368 KB |
Output is correct |
34 |
Correct |
114 ms |
53584 KB |
Output is correct |
35 |
Correct |
132 ms |
57428 KB |
Output is correct |
36 |
Correct |
108 ms |
53584 KB |
Output is correct |