CSES - Leirikisa 2 - Results
Submission details
Task:Alpine valley
Sender:a256
Submission time:2023-04-18 16:08:19 +0300
Language:C++ (C++11)
Status:READY
Result:9
Feedback
groupverdictscore
#1ACCEPTED9
#20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.01 s2, 4details
#2ACCEPTED0.01 s2, 4details
#3ACCEPTED0.01 s1, 4details
#4ACCEPTED0.01 s1, 4details
#5ACCEPTED0.01 s1, 4details
#6ACCEPTED0.01 s1, 3, 4details
#70.01 s2, 4details
#80.01 s2, 4details
#90.01 s2, 4details
#100.01 s2, 4details
#110.01 s2, 4details
#120.01 s2, 4details
#130.01 s2, 3, 4details
#140.01 s2, 3, 4details
#150.01 s2, 3, 4details
#160.01 s2, 4details
#170.08 s3, 4details
#180.09 s3, 4details
#190.10 s3, 4details
#200.10 s3, 4details
#210.10 s3, 4details
#220.10 s3, 4details
#230.08 s4details
#240.10 s4details
#250.10 s4details
#260.10 s4details
#270.10 s4details
#280.08 s4details
#290.09 s4details
#300.10 s4details
#310.10 s4details
#320.10 s4details
#330.08 s4details
#340.09 s4details
#350.10 s4details
#360.10 s4details
#370.11 s4details
#380.10 s4details

Code

#include <bits/stdc++.h>
using namespace std;
using ll=long long;

int N,S,Q,E,shp[(int)2e5],I,D[(int)2e5];
vector<pair<int,pair<ll,int>>> v[(int)2e5];
vector<pair<int,int>> tiet;
int f[(int)2e5][32];

int ylös(int s,int k){
	for(;k;k^=(k&-k))
		s=f[s][__builtin_ctz(k)+1];
	return s;
}

int ylös_huono(int s,int k){
	for(;k;--k){
		s=f[s][1];
	}
	return s;
}

int aye(int a,int b){
	if(D[b]>D[a]) swap(a,b);
	a=ylös_huono(a,D[a]-D[b]);
	if(a==b) return a;
	for(int i=31;i;--i){
		if(f[a][i]!=f[b][i]){
			a=f[a][i];
			b=f[b][i];
		}
	}
	return f[a][1];
}

void haku(int s,int p,int d){
	f[s][0]=s;
	D[s]=d;
	for(auto u:v[s]){
		if(u.first==p) continue;
		f[u.first][1]=s;
		haku(u.first,s,d+1);
	}
}

int haye(int a,int b){
	if(D[b]>D[a]) swap(a,b);
	a=ylös_huono(a,D[a]-D[b]);
	while(a!=b){
		a=f[a][1];
		b=f[b][1];
	}
	return a;
}

bool reitillä(int a,int b,int s){
	if(s==a||s==b) return true;
	int ay=haye(a,b);
	if(D[s]<D[ay]) return false;
	if(D[a]>D[s]&&ylös_huono(a,D[a]-D[s])==s) return true;
	if(D[b]>D[s]&&ylös_huono(b,D[b]-D[s])==s) return true;
	return false;
}

set<int> res;

bool reitti(int a,int b,int p=-1){
	res.insert(a);
	if(a==b) return true;
	for(auto u:v[a]){
		if(u.first==p) continue;
		if(reitti(u.first,b,a)) return true;
	}
	res.erase(a);
	return false;
}

bool reitil2(int a,int b,int s){
	for(auto it=res.begin();it!=res.end();it=res.erase(it));
	reitti(a,b);
	if(res.count(s)) return true;
	return false;
}

bool ppako(int s){
	int a=tiet[I-1].first,b=tiet[I-1].second;
	return !(reitillä(s,E,a)&&reitillä(s,E,b));
}

bool pako(int s,int p){
	if(s==E) return true;
	for(auto u:v[s]){
		if(u.first==p||u.second.second==I) continue;
		if(pako(u.first,s)) return true;
	}
	return false;
}

ll etäisyys(int s){
	priority_queue<pair<ll,pair<int,int>>> q;
	q.push({0,{s,-1}});
	while(q.size()){
		auto p=q.top();q.pop();
		ll et=-p.first;
		int u=p.second.first;
		if(shp[u]) return et;

		for(auto B:v[u]){
			if(B.first==p.second.second||B.second.second==I) continue;
			q.push({-et-B.second.first,{B.first,u}});
		}
	}
	return -1;
}

void kysmys(){
	int R;
	cin>>I>>R;
	ll d;
	bool p=ppako(R);
	if(p){
		cout<<"escaped\n";
	} else if((d=etäisyys(R))==-1){
		cout<<"oo\n";
	} else {
		cout<<d<<'\n';
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>N>>S>>Q>>E;
	for(int i=0;i<N-1;++i){
		int A,B,W;
		cin>>A>>B>>W;
		v[A].push_back({B,{W,i+1}});
		v[B].push_back({A,{W,i+1}});
		tiet.push_back({A,B});
	}

	haku(1,-1,0);
	for(int i=1;i<=N;++i){
		for(int k=2;k<32&&f[i][k-1];++k){
			f[i][k]=f[f[i][k-1]][k-1];
		}
	}

	for(int i=1;i<N;++i){
		for(int j=0;j<D[i];++j){
			if(ylös(i,j)!=ylös_huono(i,j)){
				cerr<<"ei toimi: "<<i<<j<<'\n';;
				return 6;
			}
		}
	}

	for(int i=0;i<S;++i){
		int s; cin>>s;
		shp[s]=1;
	}

	for(int i=0;i<Q;++i){
		kysmys();
	}
}

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:

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
(empty)

Error:
ei toimi: 34

Test 8

Group: 2, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 9

Group: 2, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 10

Group: 2, 4

Verdict:

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
(empty)

Error:
ei toimi: 54

Test 11

Group: 2, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 12

Group: 2, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 13

Group: 2, 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 14

Group: 2, 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 15

Group: 2, 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 16

Group: 2, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 17

Group: 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 18

Group: 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 19

Group: 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 20

Group: 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 21

Group: 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 22

Group: 3, 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 23

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 24

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 25

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 26

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 27

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 28

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 29

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 30

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 31

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 32

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 33

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 34

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 35

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 36

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 37

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24

Test 38

Group: 4

Verdict:

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
(empty)

Error:
ei toimi: 24