#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define ld long double
#define int long long
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 3e6 + 10 , inf= 1e17 ;
vector <pii> G[maxn] ;int n , m ,id[maxn] , cnt , ans[maxn] , d1[maxn] , d2[maxn] , mark[maxn] ;
void add(int v ,int u , int w){
G[u].pb({v,w});
}
void upd(int x ,int w , int le , int ri , int p = 1, int l = 1 , int r= n){
int pl = p<<1 , pr = p<<1 | 1 , mid = (l+r)/2 ;
if(le > r || l > ri){
return ;
}
if(le <= l&& r <= ri){
add(x , id[p] , w) ;
return ;
}
upd(x,w,le,ri,pl,l,mid);
upd(x,w,le,ri,pr,mid+1,r);
}
void bui(int p = 1 , int l = 1 , int r=n){
int pl = p<<1 , pr = p<<1 | 1 , mid = (l+r)/2 ;
id[p] = cnt ;cnt++;
if(l ==r){
add(id[p] , l , 0);
return ;
}
bui(pl,l,mid);
bui(pr,mid+1,r);
add(id[p] , id[pl] , 0) ;
add(id[p] , id[pr] , 0) ;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
cin >> n ;
cnt = n+1;
bui() ;
rep(i , 1 ,n){
int c , w , v , u ;
cin >> v >> u ;
c= i ;
w = 1;
if(v > u)swap(v,u) ;
add(c , cnt , w);
upd(cnt , 0 , v , u) ;
cnt++;
}
rep(i , 1, cnt){
d1[i] = d2[i] = inf ;
}
priority_queue <pii> pq ;
d1[1] = 0 ;pq.push({0,1});
while(sz(pq)){
int v = pq.top().S ; pq.pop();
if(mark[v] == 1)continue ;
mark[v] = 1;
for(pii u : G[v]){
if(d1[u.F] > d1[v] + u.S){
d1[u.F] =d1[v] + u.S;
pq.push({-d1[u.F] , u.F}) ;
}
}
}
rep(i , 1, cnt)mark[i] = 0 ;
d2[n] = 0;pq.push({0,n});
while(sz(pq)){
int v = pq.top().S ; pq.pop();
if(mark[v] == 1)continue ;
mark[v] = 1;
for(pii u : G[v]){
if(d2[u.F] > d2[v] + u.S){
d2[u.F] =d2[v] + u.S;
pq.push({-d2[u.F] , u.F}) ;
}
}
}
rep(i , 1, cnt){
ans[i] = d1[i] + d2[i] ;
pq.push({-ans[i] , i}) ;
}
rep(i , 1 ,cnt)mark[i] =0 ;
while(sz(pq)){
int v = pq.top().S ; pq.pop();
if(mark[v] == 1)continue ;
mark[v] = 1;
for(pii u : G[v]){
if(ans[u.F] > ans[v] + u.S){
ans[u.F] =ans[v] + u.S;
pq.push({-ans[u.F] , u.F}) ;
}
}
}
int q;
cin >> q;
rep(i , 1, q){
int x;
cin >> x ;
if(ans[x] >= inf){
cout << -1 << "\n";
}else{
cout << ans[x] << "\n";
}
}
}
/*
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
79196 KB |
Output is correct |
2 |
Correct |
22 ms |
79196 KB |
Output is correct |
3 |
Correct |
22 ms |
79192 KB |
Output is correct |
4 |
Correct |
1303 ms |
206860 KB |
Output is correct |
5 |
Correct |
659 ms |
170876 KB |
Output is correct |
6 |
Correct |
399 ms |
161840 KB |
Output is correct |
7 |
Correct |
405 ms |
153416 KB |
Output is correct |
8 |
Correct |
264 ms |
164196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
79196 KB |
Output is correct |
2 |
Correct |
24 ms |
79388 KB |
Output is correct |
3 |
Correct |
23 ms |
79196 KB |
Output is correct |
4 |
Correct |
23 ms |
79196 KB |
Output is correct |
5 |
Correct |
25 ms |
79196 KB |
Output is correct |
6 |
Correct |
22 ms |
79400 KB |
Output is correct |
7 |
Correct |
23 ms |
79196 KB |
Output is correct |
8 |
Correct |
23 ms |
79196 KB |
Output is correct |
9 |
Correct |
23 ms |
79348 KB |
Output is correct |
10 |
Correct |
23 ms |
79188 KB |
Output is correct |
11 |
Correct |
24 ms |
79452 KB |
Output is correct |
12 |
Correct |
24 ms |
79448 KB |
Output is correct |
13 |
Correct |
23 ms |
79360 KB |
Output is correct |
14 |
Correct |
23 ms |
79452 KB |
Output is correct |
15 |
Correct |
24 ms |
79452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
79196 KB |
Output is correct |
2 |
Correct |
24 ms |
79388 KB |
Output is correct |
3 |
Correct |
23 ms |
79196 KB |
Output is correct |
4 |
Correct |
23 ms |
79196 KB |
Output is correct |
5 |
Correct |
25 ms |
79196 KB |
Output is correct |
6 |
Correct |
22 ms |
79400 KB |
Output is correct |
7 |
Correct |
23 ms |
79196 KB |
Output is correct |
8 |
Correct |
23 ms |
79196 KB |
Output is correct |
9 |
Correct |
23 ms |
79348 KB |
Output is correct |
10 |
Correct |
23 ms |
79188 KB |
Output is correct |
11 |
Correct |
24 ms |
79452 KB |
Output is correct |
12 |
Correct |
24 ms |
79448 KB |
Output is correct |
13 |
Correct |
23 ms |
79360 KB |
Output is correct |
14 |
Correct |
23 ms |
79452 KB |
Output is correct |
15 |
Correct |
24 ms |
79452 KB |
Output is correct |
16 |
Correct |
28 ms |
80476 KB |
Output is correct |
17 |
Correct |
29 ms |
80212 KB |
Output is correct |
18 |
Correct |
30 ms |
80728 KB |
Output is correct |
19 |
Correct |
29 ms |
80732 KB |
Output is correct |
20 |
Correct |
28 ms |
80472 KB |
Output is correct |
21 |
Correct |
28 ms |
80220 KB |
Output is correct |
22 |
Correct |
25 ms |
80220 KB |
Output is correct |
23 |
Correct |
28 ms |
80476 KB |
Output is correct |
24 |
Correct |
27 ms |
80468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
79196 KB |
Output is correct |
2 |
Correct |
24 ms |
79388 KB |
Output is correct |
3 |
Correct |
23 ms |
79196 KB |
Output is correct |
4 |
Correct |
23 ms |
79196 KB |
Output is correct |
5 |
Correct |
25 ms |
79196 KB |
Output is correct |
6 |
Correct |
22 ms |
79400 KB |
Output is correct |
7 |
Correct |
23 ms |
79196 KB |
Output is correct |
8 |
Correct |
23 ms |
79196 KB |
Output is correct |
9 |
Correct |
23 ms |
79348 KB |
Output is correct |
10 |
Correct |
23 ms |
79188 KB |
Output is correct |
11 |
Correct |
24 ms |
79452 KB |
Output is correct |
12 |
Correct |
24 ms |
79448 KB |
Output is correct |
13 |
Correct |
23 ms |
79360 KB |
Output is correct |
14 |
Correct |
23 ms |
79452 KB |
Output is correct |
15 |
Correct |
24 ms |
79452 KB |
Output is correct |
16 |
Correct |
28 ms |
80476 KB |
Output is correct |
17 |
Correct |
29 ms |
80212 KB |
Output is correct |
18 |
Correct |
30 ms |
80728 KB |
Output is correct |
19 |
Correct |
29 ms |
80732 KB |
Output is correct |
20 |
Correct |
28 ms |
80472 KB |
Output is correct |
21 |
Correct |
28 ms |
80220 KB |
Output is correct |
22 |
Correct |
25 ms |
80220 KB |
Output is correct |
23 |
Correct |
28 ms |
80476 KB |
Output is correct |
24 |
Correct |
27 ms |
80468 KB |
Output is correct |
25 |
Correct |
24 ms |
79448 KB |
Output is correct |
26 |
Correct |
22 ms |
79196 KB |
Output is correct |
27 |
Correct |
29 ms |
80604 KB |
Output is correct |
28 |
Correct |
28 ms |
80212 KB |
Output is correct |
29 |
Correct |
27 ms |
80212 KB |
Output is correct |
30 |
Correct |
27 ms |
80668 KB |
Output is correct |
31 |
Correct |
27 ms |
80220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
79196 KB |
Output is correct |
2 |
Correct |
22 ms |
79196 KB |
Output is correct |
3 |
Correct |
22 ms |
79192 KB |
Output is correct |
4 |
Correct |
1303 ms |
206860 KB |
Output is correct |
5 |
Correct |
659 ms |
170876 KB |
Output is correct |
6 |
Correct |
399 ms |
161840 KB |
Output is correct |
7 |
Correct |
405 ms |
153416 KB |
Output is correct |
8 |
Correct |
264 ms |
164196 KB |
Output is correct |
9 |
Correct |
22 ms |
79196 KB |
Output is correct |
10 |
Correct |
24 ms |
79388 KB |
Output is correct |
11 |
Correct |
23 ms |
79196 KB |
Output is correct |
12 |
Correct |
23 ms |
79196 KB |
Output is correct |
13 |
Correct |
25 ms |
79196 KB |
Output is correct |
14 |
Correct |
22 ms |
79400 KB |
Output is correct |
15 |
Correct |
23 ms |
79196 KB |
Output is correct |
16 |
Correct |
23 ms |
79196 KB |
Output is correct |
17 |
Correct |
23 ms |
79348 KB |
Output is correct |
18 |
Correct |
23 ms |
79188 KB |
Output is correct |
19 |
Correct |
24 ms |
79452 KB |
Output is correct |
20 |
Correct |
24 ms |
79448 KB |
Output is correct |
21 |
Correct |
23 ms |
79360 KB |
Output is correct |
22 |
Correct |
23 ms |
79452 KB |
Output is correct |
23 |
Correct |
24 ms |
79452 KB |
Output is correct |
24 |
Correct |
28 ms |
80476 KB |
Output is correct |
25 |
Correct |
29 ms |
80212 KB |
Output is correct |
26 |
Correct |
30 ms |
80728 KB |
Output is correct |
27 |
Correct |
29 ms |
80732 KB |
Output is correct |
28 |
Correct |
28 ms |
80472 KB |
Output is correct |
29 |
Correct |
28 ms |
80220 KB |
Output is correct |
30 |
Correct |
25 ms |
80220 KB |
Output is correct |
31 |
Correct |
28 ms |
80476 KB |
Output is correct |
32 |
Correct |
27 ms |
80468 KB |
Output is correct |
33 |
Correct |
24 ms |
79448 KB |
Output is correct |
34 |
Correct |
22 ms |
79196 KB |
Output is correct |
35 |
Correct |
29 ms |
80604 KB |
Output is correct |
36 |
Correct |
28 ms |
80212 KB |
Output is correct |
37 |
Correct |
27 ms |
80212 KB |
Output is correct |
38 |
Correct |
27 ms |
80668 KB |
Output is correct |
39 |
Correct |
27 ms |
80220 KB |
Output is correct |
40 |
Correct |
1474 ms |
208276 KB |
Output is correct |
41 |
Correct |
708 ms |
171436 KB |
Output is correct |
42 |
Correct |
863 ms |
214908 KB |
Output is correct |
43 |
Correct |
856 ms |
215720 KB |
Output is correct |
44 |
Correct |
421 ms |
162728 KB |
Output is correct |
45 |
Correct |
523 ms |
173832 KB |
Output is correct |
46 |
Correct |
311 ms |
116928 KB |
Output is correct |
47 |
Correct |
882 ms |
176536 KB |
Output is correct |
48 |
Correct |
616 ms |
187548 KB |
Output is correct |