| Task: | Alpine valley |
| Sender: | MojoLake |
| Submission time: | 2023-04-18 17:37:35 +0300 |
| Language: | C++ (C++17) |
| Status: | READY |
| Result: | 59 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 9 |
| #2 | ACCEPTED | 27 |
| #3 | ACCEPTED | 23 |
| #4 | WRONG ANSWER | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.01 s | 2, 4 | details |
| #2 | ACCEPTED | 0.01 s | 2, 4 | details |
| #3 | ACCEPTED | 0.03 s | 1, 4 | details |
| #4 | ACCEPTED | 0.03 s | 1, 4 | details |
| #5 | ACCEPTED | 0.03 s | 1, 4 | details |
| #6 | ACCEPTED | 0.03 s | 1, 3, 4 | details |
| #7 | ACCEPTED | 0.01 s | 2, 4 | details |
| #8 | ACCEPTED | 0.01 s | 2, 4 | details |
| #9 | ACCEPTED | 0.01 s | 2, 4 | details |
| #10 | ACCEPTED | 0.01 s | 2, 4 | details |
| #11 | ACCEPTED | 0.01 s | 2, 4 | details |
| #12 | ACCEPTED | 0.01 s | 2, 4 | details |
| #13 | ACCEPTED | 0.01 s | 2, 3, 4 | details |
| #14 | ACCEPTED | 0.01 s | 2, 3, 4 | details |
| #15 | ACCEPTED | 0.01 s | 2, 3, 4 | details |
| #16 | ACCEPTED | 0.01 s | 2, 4 | details |
| #17 | ACCEPTED | 0.48 s | 3, 4 | details |
| #18 | ACCEPTED | 0.50 s | 3, 4 | details |
| #19 | ACCEPTED | 0.52 s | 3, 4 | details |
| #20 | ACCEPTED | 0.55 s | 3, 4 | details |
| #21 | ACCEPTED | 0.53 s | 3, 4 | details |
| #22 | ACCEPTED | 0.61 s | 3, 4 | details |
| #23 | ACCEPTED | 0.45 s | 4 | details |
| #24 | ACCEPTED | 0.47 s | 4 | details |
| #25 | ACCEPTED | 0.50 s | 4 | details |
| #26 | ACCEPTED | 0.52 s | 4 | details |
| #27 | ACCEPTED | 0.55 s | 4 | details |
| #28 | ACCEPTED | 0.45 s | 4 | details |
| #29 | WRONG ANSWER | 0.47 s | 4 | details |
| #30 | WRONG ANSWER | 0.49 s | 4 | details |
| #31 | WRONG ANSWER | 0.53 s | 4 | details |
| #32 | ACCEPTED | 0.57 s | 4 | details |
| #33 | WRONG ANSWER | 0.45 s | 4 | details |
| #34 | WRONG ANSWER | 0.47 s | 4 | details |
| #35 | WRONG ANSWER | 0.49 s | 4 | details |
| #36 | WRONG ANSWER | 0.53 s | 4 | details |
| #37 | ACCEPTED | 0.56 s | 4 | details |
| #38 | ACCEPTED | 0.49 s | 4 | details |
Code
#include <bits/stdc++.h>
#define debug(x) cout << #x << ": " << x << "\n"
#define all(x) x.begin(), x.end()
using namespace std;
using std::sort;
using ll = long long;
const int N = 1e5 + 1;
const int LOG = 20;
const int M = 101;
const int inf = 1e9;
const ll LLinf = 1e16;
vector<pair<int, ll>> g[N];
vector<pair<int, int>> edges;
int par[N];
bool shop[N];
int jump[N][LOG], depth[N];
ll weight_d[N];
ll up[N];
vector<pair<ll, int>> down[N];
int n, s, q, e;
void first_dfs(int cur, int pre){
par[cur] = pre;
jump[cur][0] = pre;
depth[cur] = depth[pre] + 1;
for(auto [nex, w] : g[cur]){
if(nex == pre)continue;
// cost_jump[nex][0] = w;
weight_d[nex] = weight_d[cur] + w;
first_dfs(nex, cur);
}
}
void create_jump(){
for(int j = 1; j < LOG; ++j){
for(int i = 1; i <= n; ++i){
jump[i][j] = jump[jump[i][j-1]][j-1];
// cost_jump[i][j] = cost_jump[i][j-1] + cost_jump[jump[i][j-1]][j-1];
}
}
}
bool solve(int ind, int node){
auto [p, c] = edges[ind];
if(par[c] != p)swap(p, c);
assert(par[c] == p);
// cout << p << " " << c << "\n";
// cout << node << "\n";
if(c == node)return 0;
if(depth[c] >= depth[node])return 1;
for(int j = LOG - 1; j >= 0; --j){
int nex = jump[node][j];
if(depth[nex] >= depth[c]){
node = nex;
}
}
// cqout << node << " " << c << "\n\n";
return node != c;
}
ll calc_down(int cur, int pre){
if(shop[cur])down[cur].push_back({0, -1});
else down[cur].push_back({LLinf, -1});
for(int i = 0; i < (int)g[cur].size(); ++i){
auto [nex, w] = g[cur][i];
if(nex == pre)continue;
down[cur].push_back({calc_down(nex, cur) + w, nex});
}
sort(all(down[cur]));
return down[cur].begin()->first;
}
void calc_up(int cur, int pre, ll mn_dis){
if(!shop[cur])up[cur] = mn_dis;
for(int i = 0; i < (int)g[cur].size(); ++i){
auto [nex, w] = g[cur][i];
if(nex == pre)continue;
ll add = down[cur].begin()->first ;
if(down[cur].begin()->second == nex)add = down[cur][1].first;
ll cur_mn = min(mn_dis, add) + w;
calc_up(nex, cur, cur_mn);
}
}
int lca(int a, int b){
if(depth[a] < depth[b])swap(a, b);
for(int j = LOG - 1; j >= 0; --j){
int nex = jump[a][j];
if(depth[nex] >= depth[b]){
a = nex;
}
}
assert(depth[a] == depth[b]);
for(int j = LOG - 1; j >= 0; --j){
while(jump[a][j] != jump[b][j]){
a = jump[a][j];
b = jump[b][j];
}
}
if(a == b)return a;
return jump[a][0];
}
ll dist(int a, int b){
int c = lca(a, b);
return weight_d[a] + weight_d[b] - 2ll * weight_d[c];
}
ll calc_min_dis(int ind, int node){
auto [p, c] = edges[ind];
if(par[c] != p)swap(p, c);
assert(par[c] == p);
ll dis = -1;
assert(lca(node, c) == c);
// cout << node << " " << p << " " << c << "\n";
// cout << down[node].begin()->first << "\n";
// cout << dist(node, c ) << "\n";
// cout << c << "\n";
if(node == c){
dis = down[node].begin()->first;
}else if(lca(node, c) == c){
dis = down[node].begin()->first;
if(up[node] < up[c] + dist(node, c))dis = min(dis, up[node]);
if(shop[c])dis = min(dis, dist(node, c));
int node_up = node;
for(int j = LOG - 1; j >= 0; --j){
int nex = jump[node_up][j];
if(depth[nex] > depth[c]){
node_up = nex;
}
}
// cout << node << " " << node_up << " " << p << " " << c << "\n";
ll dis_down = down[c].begin()->first;
if(down[c].begin()->second == node_up){
dis_down = down[c][1].first;
}
dis = min(dis, dist(node, c) + dis_down);
}
if(dis > (ll)1e15)return -1;
return dis;
}
int main(){
// cin.tie(0)->sync_with_stdio(0);
cin >> n >> s >> q >> e;
for(int _ = 0; _ < n - 1; ++_){
int a, b; ll w;
cin >> a >> b >> w;
g[a].push_back({b, w});
g[b].push_back({a, w});
edges.push_back({min(a, b), max(a, b)});
}
for(int _ = 0; _ < s; ++_){
int c; cin >> c;
shop[c] = 1;
}
first_dfs(e, 0);
create_jump();
calc_down(e, 0);
calc_up(e, 0, shop[e] ? 0 : LLinf);
// for(int i = 1; i <= n; ++i){
// cout << i << " " << up[i] << " " << down[i].begin()->first << "\n";
// }
while(q--){
int I, R;
cin >> I >> R;
if(solve(I-1, R)){
cout << "escaped\n";
}else{
ll res = calc_min_dis(I-1, R);
if(res == -1)cout << "oo\n";
else cout << res << "\n";
}
}
// how to check if we can reach the root?
// The road is cuts our way iff the endpoint
// (p, c) has depth smaller AND lca(node, c) == p with
// (note the special case, then we have to check)
// How to find closest shop? First we can precompute
// with tree dp to each direction for each node.
// But how to handle the removal of an edge?
//
// Let the edge removed be (p, c), that is, (parent, child)
// and the node be a.
//
// if c is an ancestor of a (lca(a, c) == c):
// answer is min(down[a], dis(a, c) + down[c])
// else if p is an ancestor of a (lca(a, p) == p):
// answer is min(down[a], dis(a, p) + min(up[p], down[p] (not including the direction to c)))
// else if p is in the subtree of a:
// answer is min(up[a], down[a] (not including direction of p), dis(a, p) - up[p] (if positive), dis(a, p) + down[p] (not including the direction of c))
// else they just have a lowest common ancestor lca(a, p)
// answer is min(down[a], dis(a, lca) + up[lca], dis(a, lca) + down[lca] (not including the direction of p), dis(a, lca) + dis(lca, p) - up[p] (if this is positive))
// think about the case when a is either p or a!!
}Test details
Test 1
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 5 2 3 1 1 2 3 1 3 2 3 4 1 3 5 2 ... |
| correct output |
|---|
| escaped 3 oo |
| user output |
|---|
| escaped 3 oo |
Test 2
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 10 2 5 4 7 2 3 4 8 3 9 10 1 6 7 3 ... |
| correct output |
|---|
| 8 escaped escaped escaped 0 |
| user output |
|---|
| 8 escaped escaped escaped 0 |
Test 3
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 100 1 10000 16 55 54 51 52 51 42 61 60 65 8 7 2 ... |
| correct output |
|---|
| escaped 556 102 escaped escaped ... |
| user output |
|---|
| escaped 556 102 escaped escaped ... Truncated |
Test 4
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 100 10 10000 33 9 8 121427413 62 61 566068708 54 53 965740432 15 14 900558622 ... |
| correct output |
|---|
| escaped escaped escaped escaped escaped ... |
| user output |
|---|
| escaped escaped escaped escaped escaped ... Truncated |
Test 5
Group: 1, 4
Verdict: ACCEPTED
| input |
|---|
| 100 50 10000 41 50 49 169637902 97 96 415399500 21 20 685901242 80 79 491260472 ... |
| correct output |
|---|
| escaped escaped escaped 0 236683917 ... |
| user output |
|---|
| escaped escaped escaped 0 236683917 ... Truncated |
Test 6
Group: 1, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100 100 10000 72 72 71 184167801 54 53 889251906 84 83 405385709 60 59 13704743 ... |
| correct output |
|---|
| 0 0 escaped escaped escaped ... |
| user output |
|---|
| 0 0 escaped escaped escaped ... Truncated |
Test 7
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 10 1000 775 118 928 460113608 474 70 44866463 733 393 759680660 710 206 648722901 ... |
| correct output |
|---|
| oo escaped escaped escaped escaped ... |
| user output |
|---|
| oo escaped escaped escaped escaped ... Truncated |
Test 8
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 10 1000 327 723 162 446180859 68 802 80310794 102 267 312376651 107 624 882387970 ... |
| correct output |
|---|
| oo escaped oo escaped escaped ... |
| user output |
|---|
| oo escaped oo escaped escaped ... Truncated |
Test 9
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 10 1000 650 119 161 836736621 838 410 854228864 199 424 454768400 60 883 388702647 ... |
| correct output |
|---|
| escaped escaped escaped escaped escaped ... |
| user output |
|---|
| escaped escaped escaped escaped escaped ... Truncated |
Test 10
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 100 1000 904 68 170 85699385 237 651 740139463 43 176 201438090 106 79 819260668 ... |
| correct output |
|---|
| escaped oo escaped 1775619050 escaped ... |
| user output |
|---|
| escaped oo escaped 1775619050 escaped ... Truncated |
Test 11
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 100 1000 529 463 789 845004864 358 945 244505855 758 337 184497598 576 316 809439021 ... |
| correct output |
|---|
| escaped 0 696530048 oo escaped ... |
| user output |
|---|
| escaped 0 696530048 oo escaped ... Truncated |
Test 12
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 100 1000 399 347 194 896931732 85 77 502917238 383 704 495548440 249 529 89377715 ... |
| correct output |
|---|
| escaped 1803283908 escaped escaped 725025384 ... |
| user output |
|---|
| escaped 1803283908 escaped escaped 725025384 ... Truncated |
Test 13
Group: 2, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 1000 1000 417 955 976 76162239 18 206 157439239 568 995 870854038 144 131 177990967 ... |
| correct output |
|---|
| escaped 0 0 0 0 ... |
| user output |
|---|
| escaped 0 0 0 0 ... Truncated |
Test 14
Group: 2, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 1000 1000 5 499 981 674767192 867 11 231983677 289 607 850347433 90 435 109149869 ... |
| correct output |
|---|
| escaped 0 escaped escaped 0 ... |
| user output |
|---|
| escaped 0 escaped escaped 0 ... Truncated |
Test 15
Group: 2, 3, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 1000 1000 402 827 566 831751863 177 338 737463577 130 290 477228687 381 865 208330027 ... |
| correct output |
|---|
| escaped escaped escaped escaped escaped ... |
| user output |
|---|
| escaped escaped escaped escaped escaped ... Truncated |
Test 16
Group: 2, 4
Verdict: ACCEPTED
| input |
|---|
| 1000 250 1000 719 955 478 999987900 118 129 1 693 642 1 89 10 1 ... |
| correct output |
|---|
| escaped escaped escaped 999977335 999996043 ... |
| user output |
|---|
| escaped escaped escaped 999977335 999996043 ... Truncated |
Test 17
Group: 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 12006 92983 79841 908116525 62826 38940 643240290 40394 33248 544303480 39790 52014 996656288 ... |
| correct output |
|---|
| 0 escaped 0 escaped escaped ... |
| user output |
|---|
| 0 escaped 0 escaped escaped ... Truncated |
Test 18
Group: 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 43094 5443 54581 582924938 89476 92046 904127360 75423 44348 666119170 81404 62453 705128933 ... |
| correct output |
|---|
| escaped escaped escaped 0 0 ... |
| user output |
|---|
| escaped escaped escaped 0 0 ... Truncated |
Test 19
Group: 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 9488 85370 19201 696298836 18577 46570 748157745 93114 16265 496529753 72078 35717 412237533 ... |
| correct output |
|---|
| escaped escaped 0 escaped 0 ... |
| user output |
|---|
| escaped escaped 0 escaped 0 ... Truncated |
Test 20
Group: 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 92001 67551 36343 183011569 68768 16547 690994742 13471 67372 20695710 98903 83909 529919884 ... |
| correct output |
|---|
| escaped 0 0 0 0 ... |
| user output |
|---|
| escaped 0 0 0 0 ... Truncated |
Test 21
Group: 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 63926 18158 65925 685699704 99966 63209 722194632 56258 49447 38979748 29360 52524 25423329 ... |
| correct output |
|---|
| escaped escaped escaped escaped escaped ... |
| user output |
|---|
| escaped escaped escaped escaped escaped ... Truncated |
Test 22
Group: 3, 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 85193 49261 94283 81875369 4550 87086 277612782 72700 32371 96444536 35214 5979 709167976 ... |
| correct output |
|---|
| 0 escaped escaped escaped 0 ... |
| user output |
|---|
| 0 escaped escaped escaped 0 ... Truncated |
Test 23
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 1 100000 62049 75022 73793 917598653 23223 74953 213701558 70480 94902 210482731 33079 67728 721615672 ... |
| correct output |
|---|
| escaped oo oo oo oo ... |
| user output |
|---|
| escaped oo oo oo oo ... Truncated |
Test 24
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 1 100000 60683 9762 13929 907620072 89241 80927 134733122 82965 83054 398493573 4190 85710 220472721 ... |
| correct output |
|---|
| escaped escaped escaped oo oo ... |
| user output |
|---|
| escaped escaped escaped oo oo ... Truncated |
Test 25
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 1 100000 93416 42120 30969 334590602 10610 91725 333854906 42083 81502 240679835 46376 56414 480503067 ... |
| correct output |
|---|
| oo escaped escaped oo escaped ... |
| user output |
|---|
| oo escaped escaped oo escaped ... Truncated |
Test 26
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 1 100000 49260 46043 27573 782901085 51894 53168 613136964 51695 80449 302924017 93824 72798 926998901 ... |
| correct output |
|---|
| escaped oo oo escaped oo ... |
| user output |
|---|
| escaped oo oo escaped oo ... Truncated |
Test 27
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 1 100000 48778 3031 66280 306595565 79785 6125 348382536 94956 48606 123996452 55800 22463 47341587 ... |
| correct output |
|---|
| 20156757703441 escaped 20262827699543 escaped escaped ... |
| user output |
|---|
| 20156757703441 escaped 20262827699543 escaped escaped ... Truncated |
Test 28
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100 100000 45904 91930 987 534423336 39029 44466 55162879 2591 7401 202591700 39136 56566 892409583 ... |
| correct output |
|---|
| escaped escaped oo escaped escaped ... |
| user output |
|---|
| escaped escaped oo escaped escaped ... Truncated |
Test 29
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 100 100000 52351 4136 69049 98258152 1792 36401 226432996 75376 71266 993314697 69397 76013 629006800 ... |
| correct output |
|---|
| oo escaped escaped escaped oo ... |
| user output |
|---|
| oo escaped escaped escaped oo ... Truncated |
Test 30
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 100 100000 53445 34402 59905 406487655 56698 75242 807833720 81339 68024 45910238 65317 68290 1780385 ... |
| correct output |
|---|
| oo escaped escaped escaped escaped ... |
| user output |
|---|
| oo escaped escaped escaped escaped ... Truncated |
Test 31
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 100 100000 47731 54332 9781 136759305 68411 656 232175662 94782 84387 638037402 32454 10432 915608818 ... |
| correct output |
|---|
| escaped escaped oo escaped oo ... |
| user output |
|---|
| escaped escaped oo escaped oo ... Truncated |
Test 32
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 100 100000 93400 41569 42438 321398411 49592 36193 851003327 97175 8604 619586051 49225 51930 774715298 ... |
| correct output |
|---|
| escaped escaped escaped escaped 169890887130 ... |
| user output |
|---|
| escaped escaped escaped escaped 169890887130 ... Truncated |
Test 33
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 10000 100000 93612 38831 95327 244435916 89221 84478 457809450 19641 22100 136197388 30274 55704 225978648 ... |
| correct output |
|---|
| escaped oo oo escaped escaped ... |
| user output |
|---|
| escaped oo oo escaped escaped ... Truncated |
Test 34
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 10000 100000 87842 98378 17648 944109570 15907 38157 120262987 22016 83274 39186354 60388 31304 479667455 ... |
| correct output |
|---|
| escaped 1139182192 1232429781 0 oo ... |
| user output |
|---|
| escaped 1139182192 1232429781 0 oo ... Truncated |
Test 35
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 10000 100000 67190 83252 69978 629862602 59544 15851 9101373 65114 24734 594009368 80279 35421 30446439 ... |
| correct output |
|---|
| escaped escaped oo escaped oo ... |
| user output |
|---|
| escaped escaped oo escaped oo ... Truncated |
Test 36
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 10000 100000 99161 72942 44990 542188499 43085 52327 474981003 35672 65958 618901863 47062 48069 864393688 ... |
| correct output |
|---|
| escaped 653059479 escaped escaped 0 ... |
| user output |
|---|
| escaped 653059479 escaped escaped 0 ... Truncated |
Test 37
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 10000 100000 10932 61909 89572 45368675 5240 30566 274286750 59949 57340 817361702 76233 22765 595831327 ... |
| correct output |
|---|
| escaped escaped escaped 832606240 530619380 ... |
| user output |
|---|
| escaped escaped escaped 832606240 530619380 ... Truncated |
Test 38
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 100000 25000 100000 61734 16733 18530 1 47096 33881 1 95659 47 1 90943 69095 1 ... |
| correct output |
|---|
| escaped escaped escaped escaped escaped ... |
| user output |
|---|
| escaped escaped escaped escaped escaped ... Truncated |
