#include "swap.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+12;
vector<pair<int,int>> g[N];
vector<int> gr[N],st[N];
int n,m,p[N],timer=0,t[N],e1[N],e2[N],first_time[N];
bool is[N];
int get(int v){
if(p[v] == v) return v;
return p[v] = get(p[v]);
}
int n1,tt[N],P[N];
void merge(int a,int b){
int vv,uu;
vv = a;
uu = b;
a = get(a);
b = get(b);
if(a == b){
if(!is[a]){
for(int j:st[a]){
first_time[j]=timer;
}
st[a].clear();
is[a]=1;
}
timer++;
return;
}
if((int)st[a].size() > (int)st[b].size()){
swap(vv,uu);
swap(a, b);
}
n1++;
tt[n1] = timer;
// cout << n1 << ' ' << P[a] << ' ' << P[b] << "x\n";
gr[n1].push_back(P[a]);
gr[n1].push_back(P[b]);
P[b] = n1;
p[a] = b;
for(int j:st[a]){
st[b].push_back(j);
}
st[a].clear();
if(is[b] || is[a] || (vv != e1[a] && vv != e2[a]) || (uu != e1[b] && uu != e2[b])) {
is[b]=1;
for(int j:st[b]){
first_time[j]=timer;
}
st[b].clear();
}else{
if(uu == e1[b]){
e1[b] = (vv == e1[a] ? e2[a] : e1[a]);
}else{
e2[b] = (vv == e1[a] ? e2[a] : e1[a]);
}
}
timer++;
}
int B = 20;
int up[N][20],tin[N],tout[N],tv;
void dfs(int v,int pr){
up[v][0] = pr;
tin[v] = tv++;
for(int i = 1;i < B;i++){
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(int to:gr[v]){
dfs(to,v);
}
tout[v] = tv++;
}
bool is_pr(int a,int b){
return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca(int v,int u){
if(is_pr(v,u)) return v;
if(is_pr(u,v)) return u;
for(int i = B - 1;i >= 0;i--){
if(!is_pr(up[v][i],u)){
v = up[v][i];
}
}
return up[v][0];
}
vector <array<int, 3>> e;
void init(int nn, int mm,std::vector<int> u, std::vector<int> v, std::vector<int> w) {
memset(first_time,-1,sizeof(first_time));
n1 = n = nn;
m = mm;
for (int i = 1; i <= m; i++) {
e.push_back({w[i - 1], u[i - 1] + 1, v[i - 1] + 1});
}
sort(e.begin(), e.end());
for(int i = 1;i <= n;i++) {
P[i] =e1[i] = e2[i] = p[i] = i;
st[i].push_back(i);
}
for(auto [w,a,b]:e){
merge(a,b);
}
dfs(n1,n1);
}
void merge1(int a,int b){
a = get(a);
b = get(b);
p[a] = b;
}
int getMinimumFuelCapacity(int X, int Y) {
X++;Y++;
int f = max(first_time[X],max(tt[lca(X,Y)],first_time[Y]));
if(first_time[X] == -1 || first_time[Y] == -1) return -1;
return e[f][0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22876 KB |
Output is correct |
2 |
Correct |
5 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22872 KB |
Output is correct |
5 |
Correct |
6 ms |
22876 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
6 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22872 KB |
Output is correct |
9 |
Correct |
90 ms |
46024 KB |
Output is correct |
10 |
Correct |
103 ms |
49812 KB |
Output is correct |
11 |
Correct |
115 ms |
49924 KB |
Output is correct |
12 |
Correct |
115 ms |
50772 KB |
Output is correct |
13 |
Correct |
85 ms |
50372 KB |
Output is correct |
14 |
Correct |
90 ms |
45908 KB |
Output is correct |
15 |
Correct |
175 ms |
52192 KB |
Output is correct |
16 |
Correct |
191 ms |
51676 KB |
Output is correct |
17 |
Correct |
206 ms |
52520 KB |
Output is correct |
18 |
Correct |
197 ms |
52096 KB |
Output is correct |
19 |
Correct |
74 ms |
30484 KB |
Output is correct |
20 |
Correct |
175 ms |
53060 KB |
Output is correct |
21 |
Correct |
188 ms |
52808 KB |
Output is correct |
22 |
Correct |
198 ms |
53952 KB |
Output is correct |
23 |
Correct |
201 ms |
53360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22876 KB |
Output is correct |
2 |
Correct |
5 ms |
22876 KB |
Output is correct |
3 |
Correct |
186 ms |
53912 KB |
Output is correct |
4 |
Correct |
194 ms |
54624 KB |
Output is correct |
5 |
Correct |
204 ms |
54532 KB |
Output is correct |
6 |
Correct |
193 ms |
54828 KB |
Output is correct |
7 |
Correct |
183 ms |
54604 KB |
Output is correct |
8 |
Correct |
178 ms |
53876 KB |
Output is correct |
9 |
Correct |
186 ms |
54664 KB |
Output is correct |
10 |
Correct |
176 ms |
53840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22876 KB |
Output is correct |
2 |
Correct |
5 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22872 KB |
Output is correct |
5 |
Correct |
6 ms |
22876 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
6 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22872 KB |
Output is correct |
9 |
Correct |
5 ms |
22872 KB |
Output is correct |
10 |
Correct |
6 ms |
22872 KB |
Output is correct |
11 |
Correct |
5 ms |
22876 KB |
Output is correct |
12 |
Correct |
5 ms |
22876 KB |
Output is correct |
13 |
Correct |
6 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22876 KB |
Output is correct |
15 |
Correct |
5 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22872 KB |
Output is correct |
17 |
Correct |
6 ms |
22872 KB |
Output is correct |
18 |
Correct |
6 ms |
22876 KB |
Output is correct |
19 |
Correct |
5 ms |
22876 KB |
Output is correct |
20 |
Correct |
5 ms |
22872 KB |
Output is correct |
21 |
Correct |
6 ms |
22876 KB |
Output is correct |
22 |
Correct |
6 ms |
22876 KB |
Output is correct |
23 |
Correct |
5 ms |
22876 KB |
Output is correct |
24 |
Correct |
5 ms |
22876 KB |
Output is correct |
25 |
Correct |
6 ms |
22972 KB |
Output is correct |
26 |
Correct |
5 ms |
22876 KB |
Output is correct |
27 |
Correct |
6 ms |
22876 KB |
Output is correct |
28 |
Correct |
6 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22872 KB |
Output is correct |
2 |
Correct |
5 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22876 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
6 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
6 ms |
22876 KB |
Output is correct |
9 |
Correct |
5 ms |
22872 KB |
Output is correct |
10 |
Correct |
90 ms |
46024 KB |
Output is correct |
11 |
Correct |
103 ms |
49812 KB |
Output is correct |
12 |
Correct |
115 ms |
49924 KB |
Output is correct |
13 |
Correct |
115 ms |
50772 KB |
Output is correct |
14 |
Correct |
85 ms |
50372 KB |
Output is correct |
15 |
Correct |
6 ms |
22872 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
5 ms |
22876 KB |
Output is correct |
18 |
Correct |
6 ms |
22876 KB |
Output is correct |
19 |
Correct |
5 ms |
22876 KB |
Output is correct |
20 |
Correct |
5 ms |
22876 KB |
Output is correct |
21 |
Correct |
5 ms |
22872 KB |
Output is correct |
22 |
Correct |
6 ms |
22872 KB |
Output is correct |
23 |
Correct |
6 ms |
22876 KB |
Output is correct |
24 |
Correct |
5 ms |
22876 KB |
Output is correct |
25 |
Correct |
5 ms |
22872 KB |
Output is correct |
26 |
Correct |
6 ms |
22876 KB |
Output is correct |
27 |
Correct |
6 ms |
22876 KB |
Output is correct |
28 |
Correct |
5 ms |
22876 KB |
Output is correct |
29 |
Correct |
5 ms |
22876 KB |
Output is correct |
30 |
Correct |
6 ms |
22972 KB |
Output is correct |
31 |
Correct |
5 ms |
22876 KB |
Output is correct |
32 |
Correct |
6 ms |
22876 KB |
Output is correct |
33 |
Correct |
6 ms |
22876 KB |
Output is correct |
34 |
Correct |
14 ms |
26396 KB |
Output is correct |
35 |
Correct |
112 ms |
49516 KB |
Output is correct |
36 |
Correct |
108 ms |
49096 KB |
Output is correct |
37 |
Correct |
110 ms |
48328 KB |
Output is correct |
38 |
Correct |
104 ms |
47812 KB |
Output is correct |
39 |
Correct |
105 ms |
47300 KB |
Output is correct |
40 |
Correct |
94 ms |
46280 KB |
Output is correct |
41 |
Correct |
119 ms |
50300 KB |
Output is correct |
42 |
Correct |
110 ms |
50632 KB |
Output is correct |
43 |
Correct |
98 ms |
50120 KB |
Output is correct |
44 |
Correct |
106 ms |
47216 KB |
Output is correct |
45 |
Correct |
109 ms |
48012 KB |
Output is correct |
46 |
Correct |
107 ms |
49260 KB |
Output is correct |
47 |
Correct |
108 ms |
47776 KB |
Output is correct |
48 |
Correct |
103 ms |
48584 KB |
Output is correct |
49 |
Correct |
54 ms |
29636 KB |
Output is correct |
50 |
Correct |
45 ms |
29640 KB |
Output is correct |
51 |
Correct |
85 ms |
42740 KB |
Output is correct |
52 |
Correct |
134 ms |
51644 KB |
Output is correct |
53 |
Correct |
131 ms |
50952 KB |
Output is correct |
54 |
Correct |
155 ms |
53672 KB |
Output is correct |
55 |
Correct |
82 ms |
49860 KB |
Output is correct |
56 |
Correct |
133 ms |
52024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22876 KB |
Output is correct |
2 |
Correct |
5 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22872 KB |
Output is correct |
5 |
Correct |
6 ms |
22876 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
6 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22872 KB |
Output is correct |
9 |
Correct |
90 ms |
46024 KB |
Output is correct |
10 |
Correct |
103 ms |
49812 KB |
Output is correct |
11 |
Correct |
115 ms |
49924 KB |
Output is correct |
12 |
Correct |
115 ms |
50772 KB |
Output is correct |
13 |
Correct |
85 ms |
50372 KB |
Output is correct |
14 |
Correct |
90 ms |
45908 KB |
Output is correct |
15 |
Correct |
175 ms |
52192 KB |
Output is correct |
16 |
Correct |
191 ms |
51676 KB |
Output is correct |
17 |
Correct |
206 ms |
52520 KB |
Output is correct |
18 |
Correct |
197 ms |
52096 KB |
Output is correct |
19 |
Correct |
186 ms |
53912 KB |
Output is correct |
20 |
Correct |
194 ms |
54624 KB |
Output is correct |
21 |
Correct |
204 ms |
54532 KB |
Output is correct |
22 |
Correct |
193 ms |
54828 KB |
Output is correct |
23 |
Correct |
183 ms |
54604 KB |
Output is correct |
24 |
Correct |
178 ms |
53876 KB |
Output is correct |
25 |
Correct |
186 ms |
54664 KB |
Output is correct |
26 |
Correct |
176 ms |
53840 KB |
Output is correct |
27 |
Correct |
6 ms |
22872 KB |
Output is correct |
28 |
Correct |
5 ms |
22876 KB |
Output is correct |
29 |
Correct |
5 ms |
22876 KB |
Output is correct |
30 |
Correct |
6 ms |
22876 KB |
Output is correct |
31 |
Correct |
5 ms |
22876 KB |
Output is correct |
32 |
Correct |
5 ms |
22876 KB |
Output is correct |
33 |
Correct |
5 ms |
22872 KB |
Output is correct |
34 |
Correct |
6 ms |
22872 KB |
Output is correct |
35 |
Correct |
6 ms |
22876 KB |
Output is correct |
36 |
Correct |
14 ms |
26396 KB |
Output is correct |
37 |
Correct |
112 ms |
49516 KB |
Output is correct |
38 |
Correct |
108 ms |
49096 KB |
Output is correct |
39 |
Correct |
110 ms |
48328 KB |
Output is correct |
40 |
Correct |
104 ms |
47812 KB |
Output is correct |
41 |
Correct |
105 ms |
47300 KB |
Output is correct |
42 |
Correct |
94 ms |
46280 KB |
Output is correct |
43 |
Correct |
119 ms |
50300 KB |
Output is correct |
44 |
Correct |
110 ms |
50632 KB |
Output is correct |
45 |
Correct |
98 ms |
50120 KB |
Output is correct |
46 |
Correct |
106 ms |
47216 KB |
Output is correct |
47 |
Correct |
22 ms |
26648 KB |
Output is correct |
48 |
Correct |
191 ms |
56000 KB |
Output is correct |
49 |
Correct |
203 ms |
55112 KB |
Output is correct |
50 |
Correct |
210 ms |
54572 KB |
Output is correct |
51 |
Correct |
183 ms |
54072 KB |
Output is correct |
52 |
Correct |
184 ms |
52948 KB |
Output is correct |
53 |
Correct |
143 ms |
46520 KB |
Output is correct |
54 |
Correct |
210 ms |
56920 KB |
Output is correct |
55 |
Correct |
204 ms |
57020 KB |
Output is correct |
56 |
Correct |
210 ms |
53056 KB |
Output is correct |
57 |
Correct |
204 ms |
51504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22872 KB |
Output is correct |
2 |
Correct |
5 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22876 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
6 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
6 ms |
22876 KB |
Output is correct |
9 |
Correct |
5 ms |
22872 KB |
Output is correct |
10 |
Correct |
90 ms |
46024 KB |
Output is correct |
11 |
Correct |
103 ms |
49812 KB |
Output is correct |
12 |
Correct |
115 ms |
49924 KB |
Output is correct |
13 |
Correct |
115 ms |
50772 KB |
Output is correct |
14 |
Correct |
85 ms |
50372 KB |
Output is correct |
15 |
Correct |
90 ms |
45908 KB |
Output is correct |
16 |
Correct |
175 ms |
52192 KB |
Output is correct |
17 |
Correct |
191 ms |
51676 KB |
Output is correct |
18 |
Correct |
206 ms |
52520 KB |
Output is correct |
19 |
Correct |
197 ms |
52096 KB |
Output is correct |
20 |
Correct |
186 ms |
53912 KB |
Output is correct |
21 |
Correct |
194 ms |
54624 KB |
Output is correct |
22 |
Correct |
204 ms |
54532 KB |
Output is correct |
23 |
Correct |
193 ms |
54828 KB |
Output is correct |
24 |
Correct |
183 ms |
54604 KB |
Output is correct |
25 |
Correct |
178 ms |
53876 KB |
Output is correct |
26 |
Correct |
186 ms |
54664 KB |
Output is correct |
27 |
Correct |
176 ms |
53840 KB |
Output is correct |
28 |
Correct |
6 ms |
22872 KB |
Output is correct |
29 |
Correct |
5 ms |
22876 KB |
Output is correct |
30 |
Correct |
5 ms |
22876 KB |
Output is correct |
31 |
Correct |
6 ms |
22876 KB |
Output is correct |
32 |
Correct |
5 ms |
22876 KB |
Output is correct |
33 |
Correct |
5 ms |
22876 KB |
Output is correct |
34 |
Correct |
5 ms |
22872 KB |
Output is correct |
35 |
Correct |
6 ms |
22872 KB |
Output is correct |
36 |
Correct |
6 ms |
22876 KB |
Output is correct |
37 |
Correct |
14 ms |
26396 KB |
Output is correct |
38 |
Correct |
112 ms |
49516 KB |
Output is correct |
39 |
Correct |
108 ms |
49096 KB |
Output is correct |
40 |
Correct |
110 ms |
48328 KB |
Output is correct |
41 |
Correct |
104 ms |
47812 KB |
Output is correct |
42 |
Correct |
105 ms |
47300 KB |
Output is correct |
43 |
Correct |
94 ms |
46280 KB |
Output is correct |
44 |
Correct |
119 ms |
50300 KB |
Output is correct |
45 |
Correct |
110 ms |
50632 KB |
Output is correct |
46 |
Correct |
98 ms |
50120 KB |
Output is correct |
47 |
Correct |
106 ms |
47216 KB |
Output is correct |
48 |
Correct |
22 ms |
26648 KB |
Output is correct |
49 |
Correct |
191 ms |
56000 KB |
Output is correct |
50 |
Correct |
203 ms |
55112 KB |
Output is correct |
51 |
Correct |
210 ms |
54572 KB |
Output is correct |
52 |
Correct |
183 ms |
54072 KB |
Output is correct |
53 |
Correct |
184 ms |
52948 KB |
Output is correct |
54 |
Correct |
143 ms |
46520 KB |
Output is correct |
55 |
Correct |
210 ms |
56920 KB |
Output is correct |
56 |
Correct |
204 ms |
57020 KB |
Output is correct |
57 |
Correct |
210 ms |
53056 KB |
Output is correct |
58 |
Correct |
204 ms |
51504 KB |
Output is correct |
59 |
Correct |
74 ms |
30484 KB |
Output is correct |
60 |
Correct |
175 ms |
53060 KB |
Output is correct |
61 |
Correct |
188 ms |
52808 KB |
Output is correct |
62 |
Correct |
198 ms |
53952 KB |
Output is correct |
63 |
Correct |
201 ms |
53360 KB |
Output is correct |
64 |
Correct |
5 ms |
22876 KB |
Output is correct |
65 |
Correct |
5 ms |
22872 KB |
Output is correct |
66 |
Correct |
6 ms |
22876 KB |
Output is correct |
67 |
Correct |
6 ms |
22876 KB |
Output is correct |
68 |
Correct |
5 ms |
22876 KB |
Output is correct |
69 |
Correct |
5 ms |
22876 KB |
Output is correct |
70 |
Correct |
6 ms |
22972 KB |
Output is correct |
71 |
Correct |
5 ms |
22876 KB |
Output is correct |
72 |
Correct |
6 ms |
22876 KB |
Output is correct |
73 |
Correct |
6 ms |
22876 KB |
Output is correct |
74 |
Correct |
109 ms |
48012 KB |
Output is correct |
75 |
Correct |
107 ms |
49260 KB |
Output is correct |
76 |
Correct |
108 ms |
47776 KB |
Output is correct |
77 |
Correct |
103 ms |
48584 KB |
Output is correct |
78 |
Correct |
54 ms |
29636 KB |
Output is correct |
79 |
Correct |
45 ms |
29640 KB |
Output is correct |
80 |
Correct |
85 ms |
42740 KB |
Output is correct |
81 |
Correct |
134 ms |
51644 KB |
Output is correct |
82 |
Correct |
131 ms |
50952 KB |
Output is correct |
83 |
Correct |
155 ms |
53672 KB |
Output is correct |
84 |
Correct |
82 ms |
49860 KB |
Output is correct |
85 |
Correct |
133 ms |
52024 KB |
Output is correct |
86 |
Correct |
59 ms |
35480 KB |
Output is correct |
87 |
Correct |
205 ms |
54952 KB |
Output is correct |
88 |
Correct |
193 ms |
52628 KB |
Output is correct |
89 |
Correct |
233 ms |
53272 KB |
Output is correct |
90 |
Correct |
127 ms |
28104 KB |
Output is correct |
91 |
Correct |
138 ms |
38740 KB |
Output is correct |
92 |
Correct |
178 ms |
44076 KB |
Output is correct |
93 |
Correct |
217 ms |
55740 KB |
Output is correct |
94 |
Correct |
271 ms |
54876 KB |
Output is correct |
95 |
Correct |
238 ms |
60488 KB |
Output is correct |
96 |
Correct |
213 ms |
55788 KB |
Output is correct |
97 |
Correct |
250 ms |
56748 KB |
Output is correct |