#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 |
2 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20964 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20920 KB |
Output is correct |
9 |
Correct |
42 ms |
41968 KB |
Output is correct |
10 |
Correct |
52 ms |
47548 KB |
Output is correct |
11 |
Correct |
53 ms |
47380 KB |
Output is correct |
12 |
Correct |
50 ms |
47952 KB |
Output is correct |
13 |
Correct |
50 ms |
51044 KB |
Output is correct |
14 |
Correct |
43 ms |
42348 KB |
Output is correct |
15 |
Correct |
99 ms |
51676 KB |
Output is correct |
16 |
Correct |
93 ms |
51460 KB |
Output is correct |
17 |
Correct |
96 ms |
52112 KB |
Output is correct |
18 |
Correct |
118 ms |
55236 KB |
Output is correct |
19 |
Correct |
44 ms |
31624 KB |
Output is correct |
20 |
Correct |
97 ms |
52688 KB |
Output is correct |
21 |
Correct |
92 ms |
52844 KB |
Output is correct |
22 |
Correct |
99 ms |
53428 KB |
Output is correct |
23 |
Correct |
117 ms |
56556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
102 ms |
58252 KB |
Output is correct |
4 |
Correct |
99 ms |
58808 KB |
Output is correct |
5 |
Correct |
113 ms |
58852 KB |
Output is correct |
6 |
Correct |
100 ms |
58556 KB |
Output is correct |
7 |
Correct |
113 ms |
58828 KB |
Output is correct |
8 |
Correct |
103 ms |
58368 KB |
Output is correct |
9 |
Correct |
117 ms |
58576 KB |
Output is correct |
10 |
Correct |
101 ms |
58120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20964 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20920 KB |
Output is correct |
9 |
Correct |
3 ms |
20828 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
3 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
2 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 |
3 ms |
20828 KB |
Output is correct |
17 |
Correct |
2 ms |
20968 KB |
Output is correct |
18 |
Correct |
3 ms |
20828 KB |
Output is correct |
19 |
Correct |
3 ms |
20824 KB |
Output is correct |
20 |
Correct |
3 ms |
20828 KB |
Output is correct |
21 |
Correct |
3 ms |
21084 KB |
Output is correct |
22 |
Correct |
3 ms |
21084 KB |
Output is correct |
23 |
Correct |
3 ms |
20828 KB |
Output is correct |
24 |
Correct |
3 ms |
21084 KB |
Output is correct |
25 |
Correct |
3 ms |
21080 KB |
Output is correct |
26 |
Correct |
3 ms |
21008 KB |
Output is correct |
27 |
Correct |
3 ms |
21084 KB |
Output is correct |
28 |
Correct |
3 ms |
21084 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 |
3 ms |
20964 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
3 ms |
20920 KB |
Output is correct |
10 |
Correct |
42 ms |
41968 KB |
Output is correct |
11 |
Correct |
52 ms |
47548 KB |
Output is correct |
12 |
Correct |
53 ms |
47380 KB |
Output is correct |
13 |
Correct |
50 ms |
47952 KB |
Output is correct |
14 |
Correct |
50 ms |
51044 KB |
Output is correct |
15 |
Correct |
2 ms |
20828 KB |
Output is correct |
16 |
Correct |
3 ms |
20828 KB |
Output is correct |
17 |
Correct |
3 ms |
20828 KB |
Output is correct |
18 |
Correct |
2 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 |
3 ms |
20828 KB |
Output is correct |
22 |
Correct |
2 ms |
20968 KB |
Output is correct |
23 |
Correct |
3 ms |
20828 KB |
Output is correct |
24 |
Correct |
3 ms |
20824 KB |
Output is correct |
25 |
Correct |
3 ms |
20828 KB |
Output is correct |
26 |
Correct |
3 ms |
21084 KB |
Output is correct |
27 |
Correct |
3 ms |
21084 KB |
Output is correct |
28 |
Correct |
3 ms |
20828 KB |
Output is correct |
29 |
Correct |
3 ms |
21084 KB |
Output is correct |
30 |
Correct |
3 ms |
21080 KB |
Output is correct |
31 |
Correct |
3 ms |
21008 KB |
Output is correct |
32 |
Correct |
3 ms |
21084 KB |
Output is correct |
33 |
Correct |
3 ms |
21084 KB |
Output is correct |
34 |
Correct |
8 ms |
24152 KB |
Output is correct |
35 |
Correct |
55 ms |
47720 KB |
Output is correct |
36 |
Correct |
49 ms |
47708 KB |
Output is correct |
37 |
Correct |
55 ms |
47696 KB |
Output is correct |
38 |
Correct |
55 ms |
47444 KB |
Output is correct |
39 |
Correct |
51 ms |
47448 KB |
Output is correct |
40 |
Correct |
45 ms |
44884 KB |
Output is correct |
41 |
Correct |
53 ms |
48208 KB |
Output is correct |
42 |
Correct |
60 ms |
47584 KB |
Output is correct |
43 |
Correct |
48 ms |
51768 KB |
Output is correct |
44 |
Correct |
58 ms |
47956 KB |
Output is correct |
45 |
Correct |
90 ms |
59672 KB |
Output is correct |
46 |
Correct |
53 ms |
47700 KB |
Output is correct |
47 |
Correct |
50 ms |
47904 KB |
Output is correct |
48 |
Correct |
65 ms |
53076 KB |
Output is correct |
49 |
Correct |
62 ms |
59312 KB |
Output is correct |
50 |
Correct |
50 ms |
51536 KB |
Output is correct |
51 |
Correct |
65 ms |
55376 KB |
Output is correct |
52 |
Correct |
82 ms |
63060 KB |
Output is correct |
53 |
Correct |
105 ms |
64336 KB |
Output is correct |
54 |
Correct |
100 ms |
72700 KB |
Output is correct |
55 |
Correct |
48 ms |
51348 KB |
Output is correct |
56 |
Correct |
107 ms |
66480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20828 KB |
Output is correct |
2 |
Correct |
2 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20964 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20920 KB |
Output is correct |
9 |
Correct |
42 ms |
41968 KB |
Output is correct |
10 |
Correct |
52 ms |
47548 KB |
Output is correct |
11 |
Correct |
53 ms |
47380 KB |
Output is correct |
12 |
Correct |
50 ms |
47952 KB |
Output is correct |
13 |
Correct |
50 ms |
51044 KB |
Output is correct |
14 |
Correct |
43 ms |
42348 KB |
Output is correct |
15 |
Correct |
99 ms |
51676 KB |
Output is correct |
16 |
Correct |
93 ms |
51460 KB |
Output is correct |
17 |
Correct |
96 ms |
52112 KB |
Output is correct |
18 |
Correct |
118 ms |
55236 KB |
Output is correct |
19 |
Correct |
102 ms |
58252 KB |
Output is correct |
20 |
Correct |
99 ms |
58808 KB |
Output is correct |
21 |
Correct |
113 ms |
58852 KB |
Output is correct |
22 |
Correct |
100 ms |
58556 KB |
Output is correct |
23 |
Correct |
113 ms |
58828 KB |
Output is correct |
24 |
Correct |
103 ms |
58368 KB |
Output is correct |
25 |
Correct |
117 ms |
58576 KB |
Output is correct |
26 |
Correct |
101 ms |
58120 KB |
Output is correct |
27 |
Correct |
2 ms |
20828 KB |
Output is correct |
28 |
Correct |
3 ms |
20828 KB |
Output is correct |
29 |
Correct |
3 ms |
20828 KB |
Output is correct |
30 |
Correct |
2 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 |
3 ms |
20828 KB |
Output is correct |
34 |
Correct |
2 ms |
20968 KB |
Output is correct |
35 |
Correct |
3 ms |
20828 KB |
Output is correct |
36 |
Correct |
8 ms |
24152 KB |
Output is correct |
37 |
Correct |
55 ms |
47720 KB |
Output is correct |
38 |
Correct |
49 ms |
47708 KB |
Output is correct |
39 |
Correct |
55 ms |
47696 KB |
Output is correct |
40 |
Correct |
55 ms |
47444 KB |
Output is correct |
41 |
Correct |
51 ms |
47448 KB |
Output is correct |
42 |
Correct |
45 ms |
44884 KB |
Output is correct |
43 |
Correct |
53 ms |
48208 KB |
Output is correct |
44 |
Correct |
60 ms |
47584 KB |
Output is correct |
45 |
Correct |
48 ms |
51768 KB |
Output is correct |
46 |
Correct |
58 ms |
47956 KB |
Output is correct |
47 |
Correct |
13 ms |
24668 KB |
Output is correct |
48 |
Correct |
104 ms |
52804 KB |
Output is correct |
49 |
Correct |
100 ms |
52664 KB |
Output is correct |
50 |
Correct |
96 ms |
52664 KB |
Output is correct |
51 |
Correct |
100 ms |
52416 KB |
Output is correct |
52 |
Correct |
101 ms |
52220 KB |
Output is correct |
53 |
Correct |
86 ms |
46404 KB |
Output is correct |
54 |
Correct |
105 ms |
53492 KB |
Output is correct |
55 |
Correct |
101 ms |
52596 KB |
Output is correct |
56 |
Correct |
117 ms |
55736 KB |
Output is correct |
57 |
Correct |
114 ms |
53428 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 |
3 ms |
20964 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
3 ms |
20920 KB |
Output is correct |
10 |
Correct |
42 ms |
41968 KB |
Output is correct |
11 |
Correct |
52 ms |
47548 KB |
Output is correct |
12 |
Correct |
53 ms |
47380 KB |
Output is correct |
13 |
Correct |
50 ms |
47952 KB |
Output is correct |
14 |
Correct |
50 ms |
51044 KB |
Output is correct |
15 |
Correct |
43 ms |
42348 KB |
Output is correct |
16 |
Correct |
99 ms |
51676 KB |
Output is correct |
17 |
Correct |
93 ms |
51460 KB |
Output is correct |
18 |
Correct |
96 ms |
52112 KB |
Output is correct |
19 |
Correct |
118 ms |
55236 KB |
Output is correct |
20 |
Correct |
102 ms |
58252 KB |
Output is correct |
21 |
Correct |
99 ms |
58808 KB |
Output is correct |
22 |
Correct |
113 ms |
58852 KB |
Output is correct |
23 |
Correct |
100 ms |
58556 KB |
Output is correct |
24 |
Correct |
113 ms |
58828 KB |
Output is correct |
25 |
Correct |
103 ms |
58368 KB |
Output is correct |
26 |
Correct |
117 ms |
58576 KB |
Output is correct |
27 |
Correct |
101 ms |
58120 KB |
Output is correct |
28 |
Correct |
2 ms |
20828 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 |
2 ms |
20828 KB |
Output is correct |
33 |
Correct |
3 ms |
20828 KB |
Output is correct |
34 |
Correct |
3 ms |
20828 KB |
Output is correct |
35 |
Correct |
2 ms |
20968 KB |
Output is correct |
36 |
Correct |
3 ms |
20828 KB |
Output is correct |
37 |
Correct |
8 ms |
24152 KB |
Output is correct |
38 |
Correct |
55 ms |
47720 KB |
Output is correct |
39 |
Correct |
49 ms |
47708 KB |
Output is correct |
40 |
Correct |
55 ms |
47696 KB |
Output is correct |
41 |
Correct |
55 ms |
47444 KB |
Output is correct |
42 |
Correct |
51 ms |
47448 KB |
Output is correct |
43 |
Correct |
45 ms |
44884 KB |
Output is correct |
44 |
Correct |
53 ms |
48208 KB |
Output is correct |
45 |
Correct |
60 ms |
47584 KB |
Output is correct |
46 |
Correct |
48 ms |
51768 KB |
Output is correct |
47 |
Correct |
58 ms |
47956 KB |
Output is correct |
48 |
Correct |
13 ms |
24668 KB |
Output is correct |
49 |
Correct |
104 ms |
52804 KB |
Output is correct |
50 |
Correct |
100 ms |
52664 KB |
Output is correct |
51 |
Correct |
96 ms |
52664 KB |
Output is correct |
52 |
Correct |
100 ms |
52416 KB |
Output is correct |
53 |
Correct |
101 ms |
52220 KB |
Output is correct |
54 |
Correct |
86 ms |
46404 KB |
Output is correct |
55 |
Correct |
105 ms |
53492 KB |
Output is correct |
56 |
Correct |
101 ms |
52596 KB |
Output is correct |
57 |
Correct |
117 ms |
55736 KB |
Output is correct |
58 |
Correct |
114 ms |
53428 KB |
Output is correct |
59 |
Correct |
44 ms |
31624 KB |
Output is correct |
60 |
Correct |
97 ms |
52688 KB |
Output is correct |
61 |
Correct |
92 ms |
52844 KB |
Output is correct |
62 |
Correct |
99 ms |
53428 KB |
Output is correct |
63 |
Correct |
117 ms |
56556 KB |
Output is correct |
64 |
Correct |
3 ms |
20824 KB |
Output is correct |
65 |
Correct |
3 ms |
20828 KB |
Output is correct |
66 |
Correct |
3 ms |
21084 KB |
Output is correct |
67 |
Correct |
3 ms |
21084 KB |
Output is correct |
68 |
Correct |
3 ms |
20828 KB |
Output is correct |
69 |
Correct |
3 ms |
21084 KB |
Output is correct |
70 |
Correct |
3 ms |
21080 KB |
Output is correct |
71 |
Correct |
3 ms |
21008 KB |
Output is correct |
72 |
Correct |
3 ms |
21084 KB |
Output is correct |
73 |
Correct |
3 ms |
21084 KB |
Output is correct |
74 |
Correct |
90 ms |
59672 KB |
Output is correct |
75 |
Correct |
53 ms |
47700 KB |
Output is correct |
76 |
Correct |
50 ms |
47904 KB |
Output is correct |
77 |
Correct |
65 ms |
53076 KB |
Output is correct |
78 |
Correct |
62 ms |
59312 KB |
Output is correct |
79 |
Correct |
50 ms |
51536 KB |
Output is correct |
80 |
Correct |
65 ms |
55376 KB |
Output is correct |
81 |
Correct |
82 ms |
63060 KB |
Output is correct |
82 |
Correct |
105 ms |
64336 KB |
Output is correct |
83 |
Correct |
100 ms |
72700 KB |
Output is correct |
84 |
Correct |
48 ms |
51348 KB |
Output is correct |
85 |
Correct |
107 ms |
66480 KB |
Output is correct |
86 |
Correct |
40 ms |
36120 KB |
Output is correct |
87 |
Correct |
99 ms |
52552 KB |
Output is correct |
88 |
Correct |
98 ms |
52668 KB |
Output is correct |
89 |
Correct |
122 ms |
55404 KB |
Output is correct |
90 |
Correct |
110 ms |
64588 KB |
Output is correct |
91 |
Correct |
122 ms |
62588 KB |
Output is correct |
92 |
Correct |
118 ms |
59572 KB |
Output is correct |
93 |
Correct |
133 ms |
67596 KB |
Output is correct |
94 |
Correct |
153 ms |
69068 KB |
Output is correct |
95 |
Correct |
142 ms |
76372 KB |
Output is correct |
96 |
Correct |
120 ms |
56024 KB |
Output is correct |
97 |
Correct |
136 ms |
64020 KB |
Output is correct |