#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 20;
const int LOG = 21;
#define ar array
int p[N];
int nn;
vector<int> g[N];
bool ok[N];
int deg[N];
int find(int x){
if(p[x] == x)return x;
return p[x] = find(p[x]);
}
void join(int a,int b){
deg[a]++, deg[b]++;
if(deg[a] >= 3 || deg[b] >= 3)ok[nn] = 1;
a = find(a), b = find(b);
if(a == b)ok[nn] = 1;
if(ok[a] || ok[b])ok[nn] = 1;
p[a] = p[b] = p[nn] = nn;
g[nn].push_back(a);
if(a != b)g[nn].push_back(b);
++nn;
}
int up[N][LOG];
int dp[N];
int dep[N];
void dfs(int x, int p,int lst = -1){
//cout<<"->"<<x<<": "<<ok[x]<<endl;
up[x][0] = p;
for(int i = 1;i < LOG;i++)up[x][i] = up[up[x][i - 1]][i - 1];
if(ok[x])lst = x;
dp[x] = lst;
for(auto u: g[x]){
dep[u] = dep[x] + 1;
dfs(u, x, lst);
}
//cout<<"<-"<<endl;
}
int lca(int a,int b){
if(dep[a] < dep[b])swap(a, b);
int k = dep[a] - dep[b];
for(int i = 0;i < LOG;i++){
if((1 << i) & k)a = up[a][i];
}
if(a == b)return a;
for(int i = LOG - 1;i >= 0;i--){
if(up[a][i] != up[b][i]){
a = up[a][i];
b = up[b][i];
}
}
return up[a][0];
}
ar<int, 3> ed[N];
int n;
void init(int _n, int m, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n = _n;
for(int i = 0;i < m;i++)ed[i] = {W[i], U[i], V[i]};
sort(ed, ed + m);
for(int i = 0;i < n;i++)p[i] = i;
nn = n;
for(int i = 0;i < m;i++)join(ed[i][1], ed[i][2]);
dfs(nn-1, nn-1, -1);
}
int getMinimumFuelCapacity(int x, int y) {
int l = lca(x, y);
//cout<<x<<" "<<y<<": "<<l<<endl;
if(dp[l] - n < 0)return -1;
return ed[dp[l] - n][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
2 ms |
20828 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
21084 KB |
Output is correct |
9 |
Correct |
50 ms |
39020 KB |
Output is correct |
10 |
Correct |
63 ms |
46004 KB |
Output is correct |
11 |
Correct |
55 ms |
45904 KB |
Output is correct |
12 |
Correct |
60 ms |
47956 KB |
Output is correct |
13 |
Correct |
57 ms |
51028 KB |
Output is correct |
14 |
Correct |
52 ms |
42644 KB |
Output is correct |
15 |
Correct |
107 ms |
50396 KB |
Output is correct |
16 |
Correct |
100 ms |
50084 KB |
Output is correct |
17 |
Correct |
106 ms |
50356 KB |
Output is correct |
18 |
Correct |
131 ms |
55128 KB |
Output is correct |
19 |
Correct |
44 ms |
31572 KB |
Output is correct |
20 |
Correct |
100 ms |
52692 KB |
Output is correct |
21 |
Correct |
101 ms |
52892 KB |
Output is correct |
22 |
Correct |
100 ms |
53432 KB |
Output is correct |
23 |
Correct |
127 ms |
56592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
107 ms |
58352 KB |
Output is correct |
4 |
Correct |
111 ms |
57312 KB |
Output is correct |
5 |
Correct |
114 ms |
58856 KB |
Output is correct |
6 |
Correct |
103 ms |
58704 KB |
Output is correct |
7 |
Correct |
109 ms |
57296 KB |
Output is correct |
8 |
Correct |
106 ms |
58148 KB |
Output is correct |
9 |
Correct |
104 ms |
57024 KB |
Output is correct |
10 |
Correct |
104 ms |
58180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
2 ms |
20828 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
21084 KB |
Output is correct |
9 |
Correct |
3 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
3 ms |
20824 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
3 ms |
20828 KB |
Output is correct |
14 |
Correct |
2 ms |
20828 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
2 ms |
20828 KB |
Output is correct |
17 |
Correct |
2 ms |
21084 KB |
Output is correct |
18 |
Correct |
3 ms |
20828 KB |
Output is correct |
19 |
Correct |
3 ms |
20988 KB |
Output is correct |
20 |
Correct |
3 ms |
20828 KB |
Output is correct |
21 |
Correct |
3 ms |
20964 KB |
Output is correct |
22 |
Correct |
3 ms |
21084 KB |
Output is correct |
23 |
Correct |
2 ms |
20828 KB |
Output is correct |
24 |
Correct |
3 ms |
21084 KB |
Output is correct |
25 |
Correct |
4 ms |
21084 KB |
Output is correct |
26 |
Correct |
3 ms |
20968 KB |
Output is correct |
27 |
Correct |
2 ms |
21084 KB |
Output is correct |
28 |
Correct |
5 ms |
21124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
2 ms |
20828 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
2 ms |
20828 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
2 ms |
21084 KB |
Output is correct |
10 |
Correct |
50 ms |
39020 KB |
Output is correct |
11 |
Correct |
63 ms |
46004 KB |
Output is correct |
12 |
Correct |
55 ms |
45904 KB |
Output is correct |
13 |
Correct |
60 ms |
47956 KB |
Output is correct |
14 |
Correct |
57 ms |
51028 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
3 ms |
20824 KB |
Output is correct |
17 |
Correct |
3 ms |
20828 KB |
Output is correct |
18 |
Correct |
3 ms |
20828 KB |
Output is correct |
19 |
Correct |
2 ms |
20828 KB |
Output is correct |
20 |
Correct |
3 ms |
20828 KB |
Output is correct |
21 |
Correct |
2 ms |
20828 KB |
Output is correct |
22 |
Correct |
2 ms |
21084 KB |
Output is correct |
23 |
Correct |
3 ms |
20828 KB |
Output is correct |
24 |
Correct |
3 ms |
20988 KB |
Output is correct |
25 |
Correct |
3 ms |
20828 KB |
Output is correct |
26 |
Correct |
3 ms |
20964 KB |
Output is correct |
27 |
Correct |
3 ms |
21084 KB |
Output is correct |
28 |
Correct |
2 ms |
20828 KB |
Output is correct |
29 |
Correct |
3 ms |
21084 KB |
Output is correct |
30 |
Correct |
4 ms |
21084 KB |
Output is correct |
31 |
Correct |
3 ms |
20968 KB |
Output is correct |
32 |
Correct |
2 ms |
21084 KB |
Output is correct |
33 |
Correct |
5 ms |
21124 KB |
Output is correct |
34 |
Correct |
9 ms |
24156 KB |
Output is correct |
35 |
Correct |
57 ms |
47796 KB |
Output is correct |
36 |
Correct |
54 ms |
47700 KB |
Output is correct |
37 |
Correct |
49 ms |
47700 KB |
Output is correct |
38 |
Correct |
49 ms |
47692 KB |
Output is correct |
39 |
Correct |
56 ms |
47444 KB |
Output is correct |
40 |
Correct |
50 ms |
44772 KB |
Output is correct |
41 |
Correct |
58 ms |
47956 KB |
Output is correct |
42 |
Correct |
48 ms |
47696 KB |
Output is correct |
43 |
Correct |
48 ms |
52048 KB |
Output is correct |
44 |
Correct |
58 ms |
47956 KB |
Output is correct |
45 |
Correct |
78 ms |
59648 KB |
Output is correct |
46 |
Correct |
58 ms |
47576 KB |
Output is correct |
47 |
Correct |
53 ms |
47964 KB |
Output is correct |
48 |
Correct |
63 ms |
53168 KB |
Output is correct |
49 |
Correct |
61 ms |
59412 KB |
Output is correct |
50 |
Correct |
49 ms |
51536 KB |
Output is correct |
51 |
Correct |
67 ms |
55440 KB |
Output is correct |
52 |
Correct |
81 ms |
63020 KB |
Output is correct |
53 |
Correct |
96 ms |
64336 KB |
Output is correct |
54 |
Correct |
109 ms |
72528 KB |
Output is correct |
55 |
Correct |
49 ms |
51560 KB |
Output is correct |
56 |
Correct |
97 ms |
66644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
2 ms |
20828 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
21084 KB |
Output is correct |
9 |
Correct |
50 ms |
39020 KB |
Output is correct |
10 |
Correct |
63 ms |
46004 KB |
Output is correct |
11 |
Correct |
55 ms |
45904 KB |
Output is correct |
12 |
Correct |
60 ms |
47956 KB |
Output is correct |
13 |
Correct |
57 ms |
51028 KB |
Output is correct |
14 |
Correct |
52 ms |
42644 KB |
Output is correct |
15 |
Correct |
107 ms |
50396 KB |
Output is correct |
16 |
Correct |
100 ms |
50084 KB |
Output is correct |
17 |
Correct |
106 ms |
50356 KB |
Output is correct |
18 |
Correct |
131 ms |
55128 KB |
Output is correct |
19 |
Correct |
107 ms |
58352 KB |
Output is correct |
20 |
Correct |
111 ms |
57312 KB |
Output is correct |
21 |
Correct |
114 ms |
58856 KB |
Output is correct |
22 |
Correct |
103 ms |
58704 KB |
Output is correct |
23 |
Correct |
109 ms |
57296 KB |
Output is correct |
24 |
Correct |
106 ms |
58148 KB |
Output is correct |
25 |
Correct |
104 ms |
57024 KB |
Output is correct |
26 |
Correct |
104 ms |
58180 KB |
Output is correct |
27 |
Correct |
3 ms |
20828 KB |
Output is correct |
28 |
Correct |
3 ms |
20824 KB |
Output is correct |
29 |
Correct |
3 ms |
20828 KB |
Output is correct |
30 |
Correct |
3 ms |
20828 KB |
Output is correct |
31 |
Correct |
2 ms |
20828 KB |
Output is correct |
32 |
Correct |
3 ms |
20828 KB |
Output is correct |
33 |
Correct |
2 ms |
20828 KB |
Output is correct |
34 |
Correct |
2 ms |
21084 KB |
Output is correct |
35 |
Correct |
3 ms |
20828 KB |
Output is correct |
36 |
Correct |
9 ms |
24156 KB |
Output is correct |
37 |
Correct |
57 ms |
47796 KB |
Output is correct |
38 |
Correct |
54 ms |
47700 KB |
Output is correct |
39 |
Correct |
49 ms |
47700 KB |
Output is correct |
40 |
Correct |
49 ms |
47692 KB |
Output is correct |
41 |
Correct |
56 ms |
47444 KB |
Output is correct |
42 |
Correct |
50 ms |
44772 KB |
Output is correct |
43 |
Correct |
58 ms |
47956 KB |
Output is correct |
44 |
Correct |
48 ms |
47696 KB |
Output is correct |
45 |
Correct |
48 ms |
52048 KB |
Output is correct |
46 |
Correct |
58 ms |
47956 KB |
Output is correct |
47 |
Correct |
12 ms |
24668 KB |
Output is correct |
48 |
Correct |
103 ms |
52664 KB |
Output is correct |
49 |
Correct |
117 ms |
52664 KB |
Output is correct |
50 |
Correct |
101 ms |
52556 KB |
Output is correct |
51 |
Correct |
99 ms |
52420 KB |
Output is correct |
52 |
Correct |
99 ms |
52224 KB |
Output is correct |
53 |
Correct |
96 ms |
46400 KB |
Output is correct |
54 |
Correct |
99 ms |
53360 KB |
Output is correct |
55 |
Correct |
96 ms |
52448 KB |
Output is correct |
56 |
Correct |
118 ms |
55736 KB |
Output is correct |
57 |
Correct |
111 ms |
53432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
2 ms |
20828 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
2 ms |
20828 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
2 ms |
21084 KB |
Output is correct |
10 |
Correct |
50 ms |
39020 KB |
Output is correct |
11 |
Correct |
63 ms |
46004 KB |
Output is correct |
12 |
Correct |
55 ms |
45904 KB |
Output is correct |
13 |
Correct |
60 ms |
47956 KB |
Output is correct |
14 |
Correct |
57 ms |
51028 KB |
Output is correct |
15 |
Correct |
52 ms |
42644 KB |
Output is correct |
16 |
Correct |
107 ms |
50396 KB |
Output is correct |
17 |
Correct |
100 ms |
50084 KB |
Output is correct |
18 |
Correct |
106 ms |
50356 KB |
Output is correct |
19 |
Correct |
131 ms |
55128 KB |
Output is correct |
20 |
Correct |
107 ms |
58352 KB |
Output is correct |
21 |
Correct |
111 ms |
57312 KB |
Output is correct |
22 |
Correct |
114 ms |
58856 KB |
Output is correct |
23 |
Correct |
103 ms |
58704 KB |
Output is correct |
24 |
Correct |
109 ms |
57296 KB |
Output is correct |
25 |
Correct |
106 ms |
58148 KB |
Output is correct |
26 |
Correct |
104 ms |
57024 KB |
Output is correct |
27 |
Correct |
104 ms |
58180 KB |
Output is correct |
28 |
Correct |
3 ms |
20828 KB |
Output is correct |
29 |
Correct |
3 ms |
20824 KB |
Output is correct |
30 |
Correct |
3 ms |
20828 KB |
Output is correct |
31 |
Correct |
3 ms |
20828 KB |
Output is correct |
32 |
Correct |
2 ms |
20828 KB |
Output is correct |
33 |
Correct |
3 ms |
20828 KB |
Output is correct |
34 |
Correct |
2 ms |
20828 KB |
Output is correct |
35 |
Correct |
2 ms |
21084 KB |
Output is correct |
36 |
Correct |
3 ms |
20828 KB |
Output is correct |
37 |
Correct |
9 ms |
24156 KB |
Output is correct |
38 |
Correct |
57 ms |
47796 KB |
Output is correct |
39 |
Correct |
54 ms |
47700 KB |
Output is correct |
40 |
Correct |
49 ms |
47700 KB |
Output is correct |
41 |
Correct |
49 ms |
47692 KB |
Output is correct |
42 |
Correct |
56 ms |
47444 KB |
Output is correct |
43 |
Correct |
50 ms |
44772 KB |
Output is correct |
44 |
Correct |
58 ms |
47956 KB |
Output is correct |
45 |
Correct |
48 ms |
47696 KB |
Output is correct |
46 |
Correct |
48 ms |
52048 KB |
Output is correct |
47 |
Correct |
58 ms |
47956 KB |
Output is correct |
48 |
Correct |
12 ms |
24668 KB |
Output is correct |
49 |
Correct |
103 ms |
52664 KB |
Output is correct |
50 |
Correct |
117 ms |
52664 KB |
Output is correct |
51 |
Correct |
101 ms |
52556 KB |
Output is correct |
52 |
Correct |
99 ms |
52420 KB |
Output is correct |
53 |
Correct |
99 ms |
52224 KB |
Output is correct |
54 |
Correct |
96 ms |
46400 KB |
Output is correct |
55 |
Correct |
99 ms |
53360 KB |
Output is correct |
56 |
Correct |
96 ms |
52448 KB |
Output is correct |
57 |
Correct |
118 ms |
55736 KB |
Output is correct |
58 |
Correct |
111 ms |
53432 KB |
Output is correct |
59 |
Correct |
44 ms |
31572 KB |
Output is correct |
60 |
Correct |
100 ms |
52692 KB |
Output is correct |
61 |
Correct |
101 ms |
52892 KB |
Output is correct |
62 |
Correct |
100 ms |
53432 KB |
Output is correct |
63 |
Correct |
127 ms |
56592 KB |
Output is correct |
64 |
Correct |
3 ms |
20988 KB |
Output is correct |
65 |
Correct |
3 ms |
20828 KB |
Output is correct |
66 |
Correct |
3 ms |
20964 KB |
Output is correct |
67 |
Correct |
3 ms |
21084 KB |
Output is correct |
68 |
Correct |
2 ms |
20828 KB |
Output is correct |
69 |
Correct |
3 ms |
21084 KB |
Output is correct |
70 |
Correct |
4 ms |
21084 KB |
Output is correct |
71 |
Correct |
3 ms |
20968 KB |
Output is correct |
72 |
Correct |
2 ms |
21084 KB |
Output is correct |
73 |
Correct |
5 ms |
21124 KB |
Output is correct |
74 |
Correct |
78 ms |
59648 KB |
Output is correct |
75 |
Correct |
58 ms |
47576 KB |
Output is correct |
76 |
Correct |
53 ms |
47964 KB |
Output is correct |
77 |
Correct |
63 ms |
53168 KB |
Output is correct |
78 |
Correct |
61 ms |
59412 KB |
Output is correct |
79 |
Correct |
49 ms |
51536 KB |
Output is correct |
80 |
Correct |
67 ms |
55440 KB |
Output is correct |
81 |
Correct |
81 ms |
63020 KB |
Output is correct |
82 |
Correct |
96 ms |
64336 KB |
Output is correct |
83 |
Correct |
109 ms |
72528 KB |
Output is correct |
84 |
Correct |
49 ms |
51560 KB |
Output is correct |
85 |
Correct |
97 ms |
66644 KB |
Output is correct |
86 |
Correct |
45 ms |
36164 KB |
Output is correct |
87 |
Correct |
99 ms |
52564 KB |
Output is correct |
88 |
Correct |
108 ms |
52588 KB |
Output is correct |
89 |
Correct |
121 ms |
55408 KB |
Output is correct |
90 |
Correct |
113 ms |
64680 KB |
Output is correct |
91 |
Correct |
126 ms |
62324 KB |
Output is correct |
92 |
Correct |
134 ms |
59684 KB |
Output is correct |
93 |
Correct |
140 ms |
67424 KB |
Output is correct |
94 |
Correct |
169 ms |
68912 KB |
Output is correct |
95 |
Correct |
162 ms |
74832 KB |
Output is correct |
96 |
Correct |
124 ms |
56004 KB |
Output is correct |
97 |
Correct |
157 ms |
64020 KB |
Output is correct |