#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define ar array
const int mxN=1e5, mxM=2e5;
int n, m, p[2*mxN], anc[2*mxN][18], first[2*mxN], n2, d[2*mxN], ew[2*mxN], epa[2*mxN], epb[2*mxN];
ar<int, 3> e[mxM];
vector<int> adj[2*mxN];
int find(int i) {
return i^p[i]?p[i]=find(p[i]):i;
}
void dfs(int u) {
for (int v : adj[u]) {
d[v]=d[u]+1;
if (ew[u]!=-1&&(ew[v]==-1||ew[u]<ew[v]))
ew[v]=ew[u];
dfs(v);
}
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n=N, m=M, n2=n;
for (int i=0; i<m; ++i)
e[i]={W[i], U[i], V[i]};
sort(e, e+m);
iota(p, p+2*n, 0);
memset(anc, -1, sizeof(anc));
memset(first, -1, sizeof(first));
for (int i=0; i<n; ++i)
epa[i]=epb[i]=i;
for (int i=0; i<m; ++i) {
int u=find(e[i][1]), v=find(e[i][2]);
if (u!=v) {
p[u]=p[v]=anc[u][0]=anc[v][0]=n2;
first[n2]=first[v]==-1||first[u]!=-1&&first[u]<first[v]?first[u]:first[v];
if (first[n2]==-1) {
if (epa[u]!=e[i][1]&&epb[u]!=e[i][1]||epa[v]!=e[i][2]&&epb[v]!=e[i][2])
first[n2]=e[i][0];
else {
epa[n2]=epa[u]^epb[u]^e[i][1];
epb[n2]=epa[v]^epb[v]^e[i][2];
//cout << epa[u] << " " << epb[u] << " " << epa[v] << " " << epb[v] << " " << e[i][1] << " " << e[i][2] << " " << epa[n2] << " " << epb[n2] << endl;
}
}
ew[n2]=e[i][0];
adj[n2].push_back(u);
adj[n2].push_back(v);
++n2;
} else if (first[u]==-1)
first[u]=e[i][0];
}
assert(n2==2*n-1);
for (int j=1; j<18; ++j)
for (int i=0; i<n2; ++i)
anc[i][j]=anc[i][j-1]^-1?anc[anc[i][j-1]][j-1]:-1;
//for (int i=0; i<n2; ++i)
// cout << epa[i] << " " << epb[i] << " " << first[i] << endl;
for (int i=n; i<n2; ++i) {
ew[i]=first[i]^-1?max(ew[i], first[i]):-1;
//cout << i << " " << ew[i] << endl;
}
dfs(n2-1);
//for (int i=n; i<n2; ++i)
// cout << i << " " << ew[i] << endl;
}
int lca(int u, int v) {
if (d[u]>d[v])
swap(u, v);
for (int i=17; ~i; --i)
if (d[v]-(1<<i)>=d[u])
v=anc[v][i];
if (u==v)
return u;
for (int i=17; ~i; --i)
if (anc[u][i]!=anc[v][i])
u=anc[u][i], v=anc[v][i];
return anc[u][0];
}
int getMinimumFuelCapacity(int u, int v) {
return ew[lca(u, v)];
}
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:39:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
39 | first[n2]=first[v]==-1||first[u]!=-1&&first[u]<first[v]?first[u]:first[v];
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
swap.cpp:41:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
41 | if (epa[u]!=e[i][1]&&epb[u]!=e[i][1]||epa[v]!=e[i][2]&&epb[v]!=e[i][2])
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
9 ms |
19796 KB |
Output is correct |
4 |
Correct |
12 ms |
19924 KB |
Output is correct |
5 |
Correct |
9 ms |
19996 KB |
Output is correct |
6 |
Correct |
12 ms |
20052 KB |
Output is correct |
7 |
Correct |
9 ms |
19924 KB |
Output is correct |
8 |
Correct |
9 ms |
19924 KB |
Output is correct |
9 |
Correct |
96 ms |
27824 KB |
Output is correct |
10 |
Correct |
111 ms |
29640 KB |
Output is correct |
11 |
Correct |
98 ms |
29412 KB |
Output is correct |
12 |
Correct |
110 ms |
30048 KB |
Output is correct |
13 |
Correct |
100 ms |
32352 KB |
Output is correct |
14 |
Correct |
111 ms |
27960 KB |
Output is correct |
15 |
Correct |
222 ms |
31512 KB |
Output is correct |
16 |
Correct |
207 ms |
31232 KB |
Output is correct |
17 |
Correct |
224 ms |
31740 KB |
Output is correct |
18 |
Correct |
298 ms |
34180 KB |
Output is correct |
19 |
Correct |
78 ms |
24888 KB |
Output is correct |
20 |
Correct |
197 ms |
32656 KB |
Output is correct |
21 |
Correct |
200 ms |
32528 KB |
Output is correct |
22 |
Correct |
221 ms |
33124 KB |
Output is correct |
23 |
Correct |
260 ms |
35580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
311 ms |
36148 KB |
Output is correct |
4 |
Correct |
244 ms |
40652 KB |
Output is correct |
5 |
Correct |
251 ms |
40576 KB |
Output is correct |
6 |
Correct |
273 ms |
40624 KB |
Output is correct |
7 |
Correct |
336 ms |
40680 KB |
Output is correct |
8 |
Correct |
270 ms |
39936 KB |
Output is correct |
9 |
Correct |
243 ms |
40392 KB |
Output is correct |
10 |
Correct |
260 ms |
39932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
9 ms |
19796 KB |
Output is correct |
4 |
Correct |
12 ms |
19924 KB |
Output is correct |
5 |
Correct |
9 ms |
19996 KB |
Output is correct |
6 |
Correct |
12 ms |
20052 KB |
Output is correct |
7 |
Correct |
9 ms |
19924 KB |
Output is correct |
8 |
Correct |
9 ms |
19924 KB |
Output is correct |
9 |
Correct |
9 ms |
19852 KB |
Output is correct |
10 |
Correct |
10 ms |
19924 KB |
Output is correct |
11 |
Correct |
10 ms |
19924 KB |
Output is correct |
12 |
Correct |
9 ms |
19944 KB |
Output is correct |
13 |
Correct |
10 ms |
19924 KB |
Output is correct |
14 |
Correct |
9 ms |
20000 KB |
Output is correct |
15 |
Correct |
11 ms |
19924 KB |
Output is correct |
16 |
Correct |
10 ms |
19924 KB |
Output is correct |
17 |
Correct |
9 ms |
19924 KB |
Output is correct |
18 |
Correct |
11 ms |
19924 KB |
Output is correct |
19 |
Correct |
10 ms |
20004 KB |
Output is correct |
20 |
Correct |
10 ms |
19940 KB |
Output is correct |
21 |
Correct |
10 ms |
20000 KB |
Output is correct |
22 |
Correct |
9 ms |
20004 KB |
Output is correct |
23 |
Correct |
9 ms |
19924 KB |
Output is correct |
24 |
Correct |
10 ms |
19996 KB |
Output is correct |
25 |
Correct |
11 ms |
20052 KB |
Output is correct |
26 |
Correct |
10 ms |
20052 KB |
Output is correct |
27 |
Correct |
9 ms |
20000 KB |
Output is correct |
28 |
Correct |
10 ms |
20012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19852 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
9 ms |
19796 KB |
Output is correct |
4 |
Correct |
9 ms |
19796 KB |
Output is correct |
5 |
Correct |
12 ms |
19924 KB |
Output is correct |
6 |
Correct |
9 ms |
19996 KB |
Output is correct |
7 |
Correct |
12 ms |
20052 KB |
Output is correct |
8 |
Correct |
9 ms |
19924 KB |
Output is correct |
9 |
Correct |
9 ms |
19924 KB |
Output is correct |
10 |
Correct |
96 ms |
27824 KB |
Output is correct |
11 |
Correct |
111 ms |
29640 KB |
Output is correct |
12 |
Correct |
98 ms |
29412 KB |
Output is correct |
13 |
Correct |
110 ms |
30048 KB |
Output is correct |
14 |
Correct |
100 ms |
32352 KB |
Output is correct |
15 |
Correct |
10 ms |
19924 KB |
Output is correct |
16 |
Correct |
10 ms |
19924 KB |
Output is correct |
17 |
Correct |
9 ms |
19944 KB |
Output is correct |
18 |
Correct |
10 ms |
19924 KB |
Output is correct |
19 |
Correct |
9 ms |
20000 KB |
Output is correct |
20 |
Correct |
11 ms |
19924 KB |
Output is correct |
21 |
Correct |
10 ms |
19924 KB |
Output is correct |
22 |
Correct |
9 ms |
19924 KB |
Output is correct |
23 |
Correct |
11 ms |
19924 KB |
Output is correct |
24 |
Correct |
10 ms |
20004 KB |
Output is correct |
25 |
Correct |
10 ms |
19940 KB |
Output is correct |
26 |
Correct |
10 ms |
20000 KB |
Output is correct |
27 |
Correct |
9 ms |
20004 KB |
Output is correct |
28 |
Correct |
9 ms |
19924 KB |
Output is correct |
29 |
Correct |
10 ms |
19996 KB |
Output is correct |
30 |
Correct |
11 ms |
20052 KB |
Output is correct |
31 |
Correct |
10 ms |
20052 KB |
Output is correct |
32 |
Correct |
9 ms |
20000 KB |
Output is correct |
33 |
Correct |
10 ms |
20012 KB |
Output is correct |
34 |
Correct |
17 ms |
21456 KB |
Output is correct |
35 |
Correct |
104 ms |
31972 KB |
Output is correct |
36 |
Correct |
107 ms |
31880 KB |
Output is correct |
37 |
Correct |
124 ms |
31820 KB |
Output is correct |
38 |
Correct |
102 ms |
31628 KB |
Output is correct |
39 |
Correct |
105 ms |
31656 KB |
Output is correct |
40 |
Correct |
110 ms |
30620 KB |
Output is correct |
41 |
Correct |
128 ms |
32152 KB |
Output is correct |
42 |
Correct |
104 ms |
31880 KB |
Output is correct |
43 |
Correct |
99 ms |
35016 KB |
Output is correct |
44 |
Correct |
114 ms |
32212 KB |
Output is correct |
45 |
Correct |
115 ms |
34556 KB |
Output is correct |
46 |
Correct |
125 ms |
31876 KB |
Output is correct |
47 |
Correct |
110 ms |
31820 KB |
Output is correct |
48 |
Correct |
107 ms |
33496 KB |
Output is correct |
49 |
Correct |
63 ms |
28240 KB |
Output is correct |
50 |
Correct |
54 ms |
27136 KB |
Output is correct |
51 |
Correct |
90 ms |
32228 KB |
Output is correct |
52 |
Correct |
159 ms |
35568 KB |
Output is correct |
53 |
Correct |
170 ms |
36868 KB |
Output is correct |
54 |
Correct |
144 ms |
37484 KB |
Output is correct |
55 |
Correct |
120 ms |
34688 KB |
Output is correct |
56 |
Correct |
157 ms |
37628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
9 ms |
19796 KB |
Output is correct |
4 |
Correct |
12 ms |
19924 KB |
Output is correct |
5 |
Correct |
9 ms |
19996 KB |
Output is correct |
6 |
Correct |
12 ms |
20052 KB |
Output is correct |
7 |
Correct |
9 ms |
19924 KB |
Output is correct |
8 |
Correct |
9 ms |
19924 KB |
Output is correct |
9 |
Correct |
96 ms |
27824 KB |
Output is correct |
10 |
Correct |
111 ms |
29640 KB |
Output is correct |
11 |
Correct |
98 ms |
29412 KB |
Output is correct |
12 |
Correct |
110 ms |
30048 KB |
Output is correct |
13 |
Correct |
100 ms |
32352 KB |
Output is correct |
14 |
Correct |
111 ms |
27960 KB |
Output is correct |
15 |
Correct |
222 ms |
31512 KB |
Output is correct |
16 |
Correct |
207 ms |
31232 KB |
Output is correct |
17 |
Correct |
224 ms |
31740 KB |
Output is correct |
18 |
Correct |
298 ms |
34180 KB |
Output is correct |
19 |
Correct |
311 ms |
36148 KB |
Output is correct |
20 |
Correct |
244 ms |
40652 KB |
Output is correct |
21 |
Correct |
251 ms |
40576 KB |
Output is correct |
22 |
Correct |
273 ms |
40624 KB |
Output is correct |
23 |
Correct |
336 ms |
40680 KB |
Output is correct |
24 |
Correct |
270 ms |
39936 KB |
Output is correct |
25 |
Correct |
243 ms |
40392 KB |
Output is correct |
26 |
Correct |
260 ms |
39932 KB |
Output is correct |
27 |
Correct |
10 ms |
19924 KB |
Output is correct |
28 |
Correct |
10 ms |
19924 KB |
Output is correct |
29 |
Correct |
9 ms |
19944 KB |
Output is correct |
30 |
Correct |
10 ms |
19924 KB |
Output is correct |
31 |
Correct |
9 ms |
20000 KB |
Output is correct |
32 |
Correct |
11 ms |
19924 KB |
Output is correct |
33 |
Correct |
10 ms |
19924 KB |
Output is correct |
34 |
Correct |
9 ms |
19924 KB |
Output is correct |
35 |
Correct |
11 ms |
19924 KB |
Output is correct |
36 |
Correct |
17 ms |
21456 KB |
Output is correct |
37 |
Correct |
104 ms |
31972 KB |
Output is correct |
38 |
Correct |
107 ms |
31880 KB |
Output is correct |
39 |
Correct |
124 ms |
31820 KB |
Output is correct |
40 |
Correct |
102 ms |
31628 KB |
Output is correct |
41 |
Correct |
105 ms |
31656 KB |
Output is correct |
42 |
Correct |
110 ms |
30620 KB |
Output is correct |
43 |
Correct |
128 ms |
32152 KB |
Output is correct |
44 |
Correct |
104 ms |
31880 KB |
Output is correct |
45 |
Correct |
99 ms |
35016 KB |
Output is correct |
46 |
Correct |
114 ms |
32212 KB |
Output is correct |
47 |
Correct |
27 ms |
21872 KB |
Output is correct |
48 |
Correct |
200 ms |
36748 KB |
Output is correct |
49 |
Correct |
202 ms |
36892 KB |
Output is correct |
50 |
Correct |
210 ms |
36728 KB |
Output is correct |
51 |
Correct |
193 ms |
36700 KB |
Output is correct |
52 |
Correct |
196 ms |
36064 KB |
Output is correct |
53 |
Correct |
165 ms |
33836 KB |
Output is correct |
54 |
Correct |
213 ms |
37652 KB |
Output is correct |
55 |
Correct |
217 ms |
36768 KB |
Output is correct |
56 |
Correct |
273 ms |
39092 KB |
Output is correct |
57 |
Correct |
207 ms |
37640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19852 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
9 ms |
19796 KB |
Output is correct |
4 |
Correct |
9 ms |
19796 KB |
Output is correct |
5 |
Correct |
12 ms |
19924 KB |
Output is correct |
6 |
Correct |
9 ms |
19996 KB |
Output is correct |
7 |
Correct |
12 ms |
20052 KB |
Output is correct |
8 |
Correct |
9 ms |
19924 KB |
Output is correct |
9 |
Correct |
9 ms |
19924 KB |
Output is correct |
10 |
Correct |
96 ms |
27824 KB |
Output is correct |
11 |
Correct |
111 ms |
29640 KB |
Output is correct |
12 |
Correct |
98 ms |
29412 KB |
Output is correct |
13 |
Correct |
110 ms |
30048 KB |
Output is correct |
14 |
Correct |
100 ms |
32352 KB |
Output is correct |
15 |
Correct |
111 ms |
27960 KB |
Output is correct |
16 |
Correct |
222 ms |
31512 KB |
Output is correct |
17 |
Correct |
207 ms |
31232 KB |
Output is correct |
18 |
Correct |
224 ms |
31740 KB |
Output is correct |
19 |
Correct |
298 ms |
34180 KB |
Output is correct |
20 |
Correct |
311 ms |
36148 KB |
Output is correct |
21 |
Correct |
244 ms |
40652 KB |
Output is correct |
22 |
Correct |
251 ms |
40576 KB |
Output is correct |
23 |
Correct |
273 ms |
40624 KB |
Output is correct |
24 |
Correct |
336 ms |
40680 KB |
Output is correct |
25 |
Correct |
270 ms |
39936 KB |
Output is correct |
26 |
Correct |
243 ms |
40392 KB |
Output is correct |
27 |
Correct |
260 ms |
39932 KB |
Output is correct |
28 |
Correct |
10 ms |
19924 KB |
Output is correct |
29 |
Correct |
10 ms |
19924 KB |
Output is correct |
30 |
Correct |
9 ms |
19944 KB |
Output is correct |
31 |
Correct |
10 ms |
19924 KB |
Output is correct |
32 |
Correct |
9 ms |
20000 KB |
Output is correct |
33 |
Correct |
11 ms |
19924 KB |
Output is correct |
34 |
Correct |
10 ms |
19924 KB |
Output is correct |
35 |
Correct |
9 ms |
19924 KB |
Output is correct |
36 |
Correct |
11 ms |
19924 KB |
Output is correct |
37 |
Correct |
17 ms |
21456 KB |
Output is correct |
38 |
Correct |
104 ms |
31972 KB |
Output is correct |
39 |
Correct |
107 ms |
31880 KB |
Output is correct |
40 |
Correct |
124 ms |
31820 KB |
Output is correct |
41 |
Correct |
102 ms |
31628 KB |
Output is correct |
42 |
Correct |
105 ms |
31656 KB |
Output is correct |
43 |
Correct |
110 ms |
30620 KB |
Output is correct |
44 |
Correct |
128 ms |
32152 KB |
Output is correct |
45 |
Correct |
104 ms |
31880 KB |
Output is correct |
46 |
Correct |
99 ms |
35016 KB |
Output is correct |
47 |
Correct |
114 ms |
32212 KB |
Output is correct |
48 |
Correct |
27 ms |
21872 KB |
Output is correct |
49 |
Correct |
200 ms |
36748 KB |
Output is correct |
50 |
Correct |
202 ms |
36892 KB |
Output is correct |
51 |
Correct |
210 ms |
36728 KB |
Output is correct |
52 |
Correct |
193 ms |
36700 KB |
Output is correct |
53 |
Correct |
196 ms |
36064 KB |
Output is correct |
54 |
Correct |
165 ms |
33836 KB |
Output is correct |
55 |
Correct |
213 ms |
37652 KB |
Output is correct |
56 |
Correct |
217 ms |
36768 KB |
Output is correct |
57 |
Correct |
273 ms |
39092 KB |
Output is correct |
58 |
Correct |
207 ms |
37640 KB |
Output is correct |
59 |
Correct |
78 ms |
24888 KB |
Output is correct |
60 |
Correct |
197 ms |
32656 KB |
Output is correct |
61 |
Correct |
200 ms |
32528 KB |
Output is correct |
62 |
Correct |
221 ms |
33124 KB |
Output is correct |
63 |
Correct |
260 ms |
35580 KB |
Output is correct |
64 |
Correct |
10 ms |
20004 KB |
Output is correct |
65 |
Correct |
10 ms |
19940 KB |
Output is correct |
66 |
Correct |
10 ms |
20000 KB |
Output is correct |
67 |
Correct |
9 ms |
20004 KB |
Output is correct |
68 |
Correct |
9 ms |
19924 KB |
Output is correct |
69 |
Correct |
10 ms |
19996 KB |
Output is correct |
70 |
Correct |
11 ms |
20052 KB |
Output is correct |
71 |
Correct |
10 ms |
20052 KB |
Output is correct |
72 |
Correct |
9 ms |
20000 KB |
Output is correct |
73 |
Correct |
10 ms |
20012 KB |
Output is correct |
74 |
Correct |
115 ms |
34556 KB |
Output is correct |
75 |
Correct |
125 ms |
31876 KB |
Output is correct |
76 |
Correct |
110 ms |
31820 KB |
Output is correct |
77 |
Correct |
107 ms |
33496 KB |
Output is correct |
78 |
Correct |
63 ms |
28240 KB |
Output is correct |
79 |
Correct |
54 ms |
27136 KB |
Output is correct |
80 |
Correct |
90 ms |
32228 KB |
Output is correct |
81 |
Correct |
159 ms |
35568 KB |
Output is correct |
82 |
Correct |
170 ms |
36868 KB |
Output is correct |
83 |
Correct |
144 ms |
37484 KB |
Output is correct |
84 |
Correct |
120 ms |
34688 KB |
Output is correct |
85 |
Correct |
157 ms |
37628 KB |
Output is correct |
86 |
Correct |
73 ms |
26248 KB |
Output is correct |
87 |
Correct |
206 ms |
36752 KB |
Output is correct |
88 |
Correct |
214 ms |
36728 KB |
Output is correct |
89 |
Correct |
237 ms |
37628 KB |
Output is correct |
90 |
Correct |
140 ms |
32668 KB |
Output is correct |
91 |
Correct |
152 ms |
33332 KB |
Output is correct |
92 |
Correct |
210 ms |
37176 KB |
Output is correct |
93 |
Correct |
233 ms |
40312 KB |
Output is correct |
94 |
Correct |
291 ms |
41300 KB |
Output is correct |
95 |
Correct |
237 ms |
41272 KB |
Output is correct |
96 |
Correct |
318 ms |
39452 KB |
Output is correct |
97 |
Correct |
269 ms |
40600 KB |
Output is correct |