#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())
ll n, s, q, e;
ll all, bll, cll;
vector<pll> edg, vec[100010];
ll a[100010];
ll IN[100010], OUT[100010];
ll cc, P[100010];
ll dp[100010], dep[100010], DEP[100010];
ll spa[100010][21], minn[100010][21];
void dfs0(ll here, ll pa)
{
IN[here] = ++cc;
P[here] = pa;
spa[here][0] = pa;
for(auto &i : vec[here])
{
if(i.fi == pa)
continue;
dep[i.fi] = dep[here] + i.se;
DEP[i.fi] = DEP[here] + 1;
dfs0(i.fi, here);
}
OUT[here] = cc;
}
void dfs(ll here, ll pa)
{
for(auto &i : vec[here])
{
if(i.fi == pa)
continue;
dfs(i.fi, here);
dp[here] = min(dp[here], dp[i.fi]);
}
}
int main(void)
{
fastio
cin >> n >> s >> q >> e;
for(ll i = 1 ; i < n ; i++)
{
cin >> all >> bll >> cll;
edg.push_back({all, bll});
vec[all].push_back({bll, cll});
vec[bll].push_back({all, cll});
}
for(ll i = 1 ; i <= n ; i++)
dp[i] = INF;
for(ll i = 1 ; i <= s ; i++)
cin >> a[i];
dfs0(e, 0);
for(ll i = 1 ; i <= s ; i++)
dp[a[i]] = dep[a[i]];
dfs(e, 0);
for(ll i = 1 ; i <= n ; i++)
{
dp[i] -= 2 * dep[i];
minn[i][0] = dp[i];
}
for(ll i = 1 ; i <= 20 ; i++)
{
for(ll j = 1 ; j <= n ; j++)
{
spa[j][i] = spa[spa[j][i - 1]][i - 1];
minn[j][i] = min(minn[j][i - 1], minn[spa[j][i - 1]][i - 1]);
}
}
while(q--)
{
cin >> bll >> all;
pll E = edg[bll - 1];
if(dep[E.fi] < dep[E.se])
swap(E.fi, E.se);
if(!(IN[E.fi] <= IN[all] && OUT[all] <= OUT[E.fi]))
{
cout << "escaped\n";
continue;
}
ll gaet = DEP[all] - DEP[E.fi] + 1;
ll ans = INF;
ll now = all;
for(ll i = 20 ; i >= 0 ; i--)
{
if(gaet >= (1LL << i))
{
gaet -= (1LL << i);
ans = min(ans, minn[now][i] + dep[all]);
now = spa[now][i];
}
}
ans = min(ans, dp[E.fi] + dep[all]);
if(ans >= INF / 100)
cout << "oo\n";
else
cout << ans en;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8272 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8272 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8220 KB |
Output is correct |
5 |
Correct |
3 ms |
10488 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10332 KB |
Output is correct |
12 |
Correct |
4 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10584 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
48320 KB |
Output is correct |
2 |
Correct |
106 ms |
48320 KB |
Output is correct |
3 |
Correct |
127 ms |
48496 KB |
Output is correct |
4 |
Correct |
157 ms |
49856 KB |
Output is correct |
5 |
Correct |
112 ms |
50060 KB |
Output is correct |
6 |
Correct |
176 ms |
51868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8272 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8220 KB |
Output is correct |
5 |
Correct |
3 ms |
10488 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10332 KB |
Output is correct |
12 |
Correct |
4 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10584 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
112 ms |
48320 KB |
Output is correct |
16 |
Correct |
106 ms |
48320 KB |
Output is correct |
17 |
Correct |
127 ms |
48496 KB |
Output is correct |
18 |
Correct |
157 ms |
49856 KB |
Output is correct |
19 |
Correct |
112 ms |
50060 KB |
Output is correct |
20 |
Correct |
176 ms |
51868 KB |
Output is correct |
21 |
Correct |
113 ms |
51680 KB |
Output is correct |
22 |
Correct |
118 ms |
51648 KB |
Output is correct |
23 |
Correct |
109 ms |
51716 KB |
Output is correct |
24 |
Correct |
148 ms |
53540 KB |
Output is correct |
25 |
Correct |
140 ms |
56000 KB |
Output is correct |
26 |
Correct |
137 ms |
51644 KB |
Output is correct |
27 |
Correct |
117 ms |
51500 KB |
Output is correct |
28 |
Correct |
127 ms |
51680 KB |
Output is correct |
29 |
Correct |
135 ms |
52928 KB |
Output is correct |
30 |
Correct |
131 ms |
54376 KB |
Output is correct |
31 |
Correct |
125 ms |
51888 KB |
Output is correct |
32 |
Correct |
102 ms |
51608 KB |
Output is correct |
33 |
Correct |
110 ms |
51904 KB |
Output is correct |
34 |
Correct |
149 ms |
53440 KB |
Output is correct |
35 |
Correct |
138 ms |
55740 KB |
Output is correct |
36 |
Correct |
141 ms |
53440 KB |
Output is correct |