#include "swap.h"
#include <vector>
#include <algorithm>
using namespace std;
/*
Use Kruskal's algorithm.
For every node, compute sorted (by wt) list of edges that doubled its component's size.
Also compute the minimum edge wt that made it's component 'good'.
A component is good if it is not a single path.
*/
vector<int> W1;
vector<int> merges[100000]; //edge
vector<int> newcol[100000]; //color after merging
vector<int> goodEdge(100000, 2e9); //weight of smallest edge that made node's component good
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
W1 = W;
int I[M];
for(int i = 0; i < M; i++) I[i] = i;
sort(I, I+M, [] (int x, int y)
{
return W1[x] < W1[y];
});
// for(int i: I) cerr << i << ' ';
// cerr << '\n';
vector<int> col(N);
vector<int> col_list[N];
vector<int> endpoints[N];
for(int i = 0; i < N; i++)
{
col[i] = i;
col_list[i].push_back(i);
merges[i].push_back(0);
newcol[i].push_back(i);
for(int e = 0; e < 2; e++)
endpoints[i].push_back(i);
}
for(int i = 0; i < M; i++)
{
int u = U[I[i]], v = V[I[i]], w = W[I[i]];
// cerr << "cc:\n";
// for(int j = 0; j < N; j++)
// {
// for(int q: col_list[j]) cerr << q << ' ';
// cerr << '\n';
// }
// cerr << "wt = " << w << '\n';
if(col[u] == col[v])
{
if(endpoints[ col[u] ].size() == 0) continue;
for(int x: col_list[ col[u] ])
goodEdge[x] = w;
endpoints[ col[u] ].clear();
continue;
}
if(col_list[col[u]].size() < col_list[col[v]].size())
swap(u, v);
bool flag = 1;
if(goodEdge[u] == 2e9 && goodEdge[v] < 2e9)
{
endpoints[ col[u] ].clear();
for(int x: col_list[col[u]])
goodEdge[x] = w;
flag = 0;
}
else if(goodEdge[u] < 2e9 && goodEdge[v] == 2e9)
{
endpoints[ col[v] ].clear();
for(int x: col_list[col[v]])
goodEdge[x] = w;
flag = 0;
}
else if(goodEdge[u] < 2e9 && goodEdge[v] < 2e9)
flag = 0;
if(flag && (u == endpoints[col[u]][0] || u == endpoints[col[u]][1]) && (v == endpoints[col[v]][0] || v == endpoints[col[v]][1]))
{
if(u == endpoints[col[u]][1])
swap(endpoints[col[u]][0], endpoints[col[u]][1]);
if(v == endpoints[col[v]][1])
swap(endpoints[col[v]][0], endpoints[col[v]][1]);
endpoints[col[u]] = {endpoints[col[u]][1], endpoints[col[v]][1]};
}
else if(flag)
{
endpoints[ col[u] ].clear();
for(int x: col_list[col[u]])
goodEdge[x] = w;
endpoints[ col[v] ].clear();
for(int x: col_list[col[v]])
goodEdge[x] = w;
}
int colV = col[v];
for(int x: col_list[colV])
{
merges[x].push_back(w);
newcol[x].push_back(col[u]);
col_list[ col[u] ].push_back(x);
col[x] = col[u];
}
col_list[colV].clear();
}
// for(int i = 0; i < N; i++)
// {
// cerr << "i = " << i << '\n';
// cerr << goodEdge[i] << '\n';
// cerr << "merges: ";
// for(int m: merges[i]) cerr << m << ' ';
// cerr << '\n';
// cerr << "newcol; ";
// for(int n: newcol[i]) cerr << n << ' ';
// cerr << '\n';
// }
}
int getMinimumFuelCapacity(int X, int Y)
{
// for(int i = 1; i <= 100000; i++);
if(goodEdge[X] == 2e9) return -1;
// cerr << "\n";
// cerr << goodEdge[X] << '\n';
// for(int i = 0; i < merges[X].size(); i++)
// {
// cerr << merges[X][i] << ' ' << newcol[X][i] << '\n';
// }
// cerr << '\n';
// cerr << goodEdge[Y] << '\n';
// for(int i = 0; i < merges[Y].size(); i++)
// {
// cerr << merges[Y][i] << ' ' << newcol[Y][i] << '\n';
// }
// cerr << '\n';
int res = 2e9;
for(int i = 0; i < merges[X].size(); i++)
for(int j = 0; j < merges[Y].size(); j++)
if(newcol[X][i] == newcol[Y][j])
res = min(res, max(merges[X][i], merges[Y][j]));
res = max(res, goodEdge[X]);
return res;
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:159:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for(int i = 0; i < merges[X].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
swap.cpp:160:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | for(int j = 0; j < merges[Y].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5324 KB |
Output is correct |
3 |
Correct |
3 ms |
5392 KB |
Output is correct |
4 |
Correct |
4 ms |
5452 KB |
Output is correct |
5 |
Correct |
5 ms |
5580 KB |
Output is correct |
6 |
Correct |
4 ms |
5580 KB |
Output is correct |
7 |
Correct |
5 ms |
5656 KB |
Output is correct |
8 |
Correct |
4 ms |
5580 KB |
Output is correct |
9 |
Correct |
218 ms |
29348 KB |
Output is correct |
10 |
Correct |
355 ms |
34368 KB |
Output is correct |
11 |
Correct |
297 ms |
33760 KB |
Output is correct |
12 |
Correct |
337 ms |
35604 KB |
Output is correct |
13 |
Correct |
165 ms |
27276 KB |
Output is correct |
14 |
Correct |
241 ms |
30852 KB |
Output is correct |
15 |
Correct |
373 ms |
40380 KB |
Output is correct |
16 |
Correct |
359 ms |
39344 KB |
Output is correct |
17 |
Correct |
391 ms |
41668 KB |
Output is correct |
18 |
Correct |
225 ms |
32960 KB |
Output is correct |
19 |
Correct |
95 ms |
15252 KB |
Output is correct |
20 |
Correct |
441 ms |
40772 KB |
Output is correct |
21 |
Correct |
434 ms |
40084 KB |
Output is correct |
22 |
Correct |
481 ms |
41972 KB |
Output is correct |
23 |
Correct |
277 ms |
33512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5324 KB |
Output is correct |
3 |
Correct |
232 ms |
28152 KB |
Output is correct |
4 |
Correct |
236 ms |
29300 KB |
Output is correct |
5 |
Correct |
235 ms |
28640 KB |
Output is correct |
6 |
Correct |
231 ms |
29008 KB |
Output is correct |
7 |
Correct |
241 ms |
28968 KB |
Output is correct |
8 |
Correct |
231 ms |
28036 KB |
Output is correct |
9 |
Correct |
241 ms |
28588 KB |
Output is correct |
10 |
Correct |
227 ms |
27780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5324 KB |
Output is correct |
3 |
Correct |
3 ms |
5392 KB |
Output is correct |
4 |
Correct |
4 ms |
5452 KB |
Output is correct |
5 |
Correct |
5 ms |
5580 KB |
Output is correct |
6 |
Correct |
4 ms |
5580 KB |
Output is correct |
7 |
Correct |
5 ms |
5656 KB |
Output is correct |
8 |
Correct |
4 ms |
5580 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
5 ms |
5580 KB |
Output is correct |
11 |
Correct |
6 ms |
5580 KB |
Output is correct |
12 |
Correct |
5 ms |
5524 KB |
Output is correct |
13 |
Correct |
5 ms |
5580 KB |
Output is correct |
14 |
Correct |
4 ms |
5580 KB |
Output is correct |
15 |
Correct |
4 ms |
5648 KB |
Output is correct |
16 |
Correct |
5 ms |
5580 KB |
Output is correct |
17 |
Correct |
4 ms |
5516 KB |
Output is correct |
18 |
Correct |
5 ms |
5580 KB |
Output is correct |
19 |
Correct |
4 ms |
5452 KB |
Output is correct |
20 |
Correct |
5 ms |
5580 KB |
Output is correct |
21 |
Correct |
4 ms |
5528 KB |
Output is correct |
22 |
Correct |
4 ms |
5452 KB |
Output is correct |
23 |
Correct |
4 ms |
5452 KB |
Output is correct |
24 |
Correct |
5 ms |
5660 KB |
Output is correct |
25 |
Correct |
5 ms |
5580 KB |
Output is correct |
26 |
Correct |
6 ms |
5708 KB |
Output is correct |
27 |
Correct |
4 ms |
5580 KB |
Output is correct |
28 |
Correct |
5 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5324 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5392 KB |
Output is correct |
5 |
Correct |
4 ms |
5452 KB |
Output is correct |
6 |
Correct |
5 ms |
5580 KB |
Output is correct |
7 |
Correct |
4 ms |
5580 KB |
Output is correct |
8 |
Correct |
5 ms |
5656 KB |
Output is correct |
9 |
Correct |
4 ms |
5580 KB |
Output is correct |
10 |
Correct |
218 ms |
29348 KB |
Output is correct |
11 |
Correct |
355 ms |
34368 KB |
Output is correct |
12 |
Correct |
297 ms |
33760 KB |
Output is correct |
13 |
Correct |
337 ms |
35604 KB |
Output is correct |
14 |
Correct |
165 ms |
27276 KB |
Output is correct |
15 |
Correct |
5 ms |
5580 KB |
Output is correct |
16 |
Correct |
6 ms |
5580 KB |
Output is correct |
17 |
Correct |
5 ms |
5524 KB |
Output is correct |
18 |
Correct |
5 ms |
5580 KB |
Output is correct |
19 |
Correct |
4 ms |
5580 KB |
Output is correct |
20 |
Correct |
4 ms |
5648 KB |
Output is correct |
21 |
Correct |
5 ms |
5580 KB |
Output is correct |
22 |
Correct |
4 ms |
5516 KB |
Output is correct |
23 |
Correct |
5 ms |
5580 KB |
Output is correct |
24 |
Correct |
4 ms |
5452 KB |
Output is correct |
25 |
Correct |
5 ms |
5580 KB |
Output is correct |
26 |
Correct |
4 ms |
5528 KB |
Output is correct |
27 |
Correct |
4 ms |
5452 KB |
Output is correct |
28 |
Correct |
4 ms |
5452 KB |
Output is correct |
29 |
Correct |
5 ms |
5660 KB |
Output is correct |
30 |
Correct |
5 ms |
5580 KB |
Output is correct |
31 |
Correct |
6 ms |
5708 KB |
Output is correct |
32 |
Correct |
4 ms |
5580 KB |
Output is correct |
33 |
Correct |
5 ms |
5580 KB |
Output is correct |
34 |
Correct |
24 ms |
9232 KB |
Output is correct |
35 |
Correct |
326 ms |
36868 KB |
Output is correct |
36 |
Correct |
355 ms |
36540 KB |
Output is correct |
37 |
Correct |
374 ms |
36108 KB |
Output is correct |
38 |
Correct |
331 ms |
35124 KB |
Output is correct |
39 |
Correct |
303 ms |
34616 KB |
Output is correct |
40 |
Correct |
258 ms |
31592 KB |
Output is correct |
41 |
Correct |
348 ms |
37696 KB |
Output is correct |
42 |
Correct |
351 ms |
37312 KB |
Output is correct |
43 |
Correct |
171 ms |
28840 KB |
Output is correct |
44 |
Correct |
354 ms |
34748 KB |
Output is correct |
45 |
Correct |
192 ms |
28460 KB |
Output is correct |
46 |
Correct |
348 ms |
36560 KB |
Output is correct |
47 |
Correct |
274 ms |
32956 KB |
Output is correct |
48 |
Correct |
215 ms |
29632 KB |
Output is correct |
49 |
Correct |
74 ms |
13280 KB |
Output is correct |
50 |
Correct |
79 ms |
12784 KB |
Output is correct |
51 |
Correct |
150 ms |
24388 KB |
Output is correct |
52 |
Correct |
354 ms |
39300 KB |
Output is correct |
53 |
Correct |
288 ms |
35264 KB |
Output is correct |
54 |
Correct |
395 ms |
42644 KB |
Output is correct |
55 |
Correct |
169 ms |
28736 KB |
Output is correct |
56 |
Correct |
248 ms |
33856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5324 KB |
Output is correct |
3 |
Correct |
3 ms |
5392 KB |
Output is correct |
4 |
Correct |
4 ms |
5452 KB |
Output is correct |
5 |
Correct |
5 ms |
5580 KB |
Output is correct |
6 |
Correct |
4 ms |
5580 KB |
Output is correct |
7 |
Correct |
5 ms |
5656 KB |
Output is correct |
8 |
Correct |
4 ms |
5580 KB |
Output is correct |
9 |
Correct |
218 ms |
29348 KB |
Output is correct |
10 |
Correct |
355 ms |
34368 KB |
Output is correct |
11 |
Correct |
297 ms |
33760 KB |
Output is correct |
12 |
Correct |
337 ms |
35604 KB |
Output is correct |
13 |
Correct |
165 ms |
27276 KB |
Output is correct |
14 |
Correct |
241 ms |
30852 KB |
Output is correct |
15 |
Correct |
373 ms |
40380 KB |
Output is correct |
16 |
Correct |
359 ms |
39344 KB |
Output is correct |
17 |
Correct |
391 ms |
41668 KB |
Output is correct |
18 |
Correct |
225 ms |
32960 KB |
Output is correct |
19 |
Correct |
232 ms |
28152 KB |
Output is correct |
20 |
Correct |
236 ms |
29300 KB |
Output is correct |
21 |
Correct |
235 ms |
28640 KB |
Output is correct |
22 |
Correct |
231 ms |
29008 KB |
Output is correct |
23 |
Correct |
241 ms |
28968 KB |
Output is correct |
24 |
Correct |
231 ms |
28036 KB |
Output is correct |
25 |
Correct |
241 ms |
28588 KB |
Output is correct |
26 |
Correct |
227 ms |
27780 KB |
Output is correct |
27 |
Correct |
5 ms |
5580 KB |
Output is correct |
28 |
Correct |
6 ms |
5580 KB |
Output is correct |
29 |
Correct |
5 ms |
5524 KB |
Output is correct |
30 |
Correct |
5 ms |
5580 KB |
Output is correct |
31 |
Correct |
4 ms |
5580 KB |
Output is correct |
32 |
Correct |
4 ms |
5648 KB |
Output is correct |
33 |
Correct |
5 ms |
5580 KB |
Output is correct |
34 |
Correct |
4 ms |
5516 KB |
Output is correct |
35 |
Correct |
5 ms |
5580 KB |
Output is correct |
36 |
Correct |
24 ms |
9232 KB |
Output is correct |
37 |
Correct |
326 ms |
36868 KB |
Output is correct |
38 |
Correct |
355 ms |
36540 KB |
Output is correct |
39 |
Correct |
374 ms |
36108 KB |
Output is correct |
40 |
Correct |
331 ms |
35124 KB |
Output is correct |
41 |
Correct |
303 ms |
34616 KB |
Output is correct |
42 |
Correct |
258 ms |
31592 KB |
Output is correct |
43 |
Correct |
348 ms |
37696 KB |
Output is correct |
44 |
Correct |
351 ms |
37312 KB |
Output is correct |
45 |
Correct |
171 ms |
28840 KB |
Output is correct |
46 |
Correct |
354 ms |
34748 KB |
Output is correct |
47 |
Correct |
29 ms |
9476 KB |
Output is correct |
48 |
Correct |
462 ms |
40912 KB |
Output is correct |
49 |
Correct |
460 ms |
40556 KB |
Output is correct |
50 |
Correct |
442 ms |
40208 KB |
Output is correct |
51 |
Correct |
445 ms |
39560 KB |
Output is correct |
52 |
Correct |
414 ms |
37384 KB |
Output is correct |
53 |
Correct |
291 ms |
29864 KB |
Output is correct |
54 |
Correct |
458 ms |
41812 KB |
Output is correct |
55 |
Correct |
478 ms |
41564 KB |
Output is correct |
56 |
Correct |
272 ms |
32648 KB |
Output is correct |
57 |
Correct |
408 ms |
39128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5324 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5392 KB |
Output is correct |
5 |
Correct |
4 ms |
5452 KB |
Output is correct |
6 |
Correct |
5 ms |
5580 KB |
Output is correct |
7 |
Correct |
4 ms |
5580 KB |
Output is correct |
8 |
Correct |
5 ms |
5656 KB |
Output is correct |
9 |
Correct |
4 ms |
5580 KB |
Output is correct |
10 |
Correct |
218 ms |
29348 KB |
Output is correct |
11 |
Correct |
355 ms |
34368 KB |
Output is correct |
12 |
Correct |
297 ms |
33760 KB |
Output is correct |
13 |
Correct |
337 ms |
35604 KB |
Output is correct |
14 |
Correct |
165 ms |
27276 KB |
Output is correct |
15 |
Correct |
241 ms |
30852 KB |
Output is correct |
16 |
Correct |
373 ms |
40380 KB |
Output is correct |
17 |
Correct |
359 ms |
39344 KB |
Output is correct |
18 |
Correct |
391 ms |
41668 KB |
Output is correct |
19 |
Correct |
225 ms |
32960 KB |
Output is correct |
20 |
Correct |
232 ms |
28152 KB |
Output is correct |
21 |
Correct |
236 ms |
29300 KB |
Output is correct |
22 |
Correct |
235 ms |
28640 KB |
Output is correct |
23 |
Correct |
231 ms |
29008 KB |
Output is correct |
24 |
Correct |
241 ms |
28968 KB |
Output is correct |
25 |
Correct |
231 ms |
28036 KB |
Output is correct |
26 |
Correct |
241 ms |
28588 KB |
Output is correct |
27 |
Correct |
227 ms |
27780 KB |
Output is correct |
28 |
Correct |
5 ms |
5580 KB |
Output is correct |
29 |
Correct |
6 ms |
5580 KB |
Output is correct |
30 |
Correct |
5 ms |
5524 KB |
Output is correct |
31 |
Correct |
5 ms |
5580 KB |
Output is correct |
32 |
Correct |
4 ms |
5580 KB |
Output is correct |
33 |
Correct |
4 ms |
5648 KB |
Output is correct |
34 |
Correct |
5 ms |
5580 KB |
Output is correct |
35 |
Correct |
4 ms |
5516 KB |
Output is correct |
36 |
Correct |
5 ms |
5580 KB |
Output is correct |
37 |
Correct |
24 ms |
9232 KB |
Output is correct |
38 |
Correct |
326 ms |
36868 KB |
Output is correct |
39 |
Correct |
355 ms |
36540 KB |
Output is correct |
40 |
Correct |
374 ms |
36108 KB |
Output is correct |
41 |
Correct |
331 ms |
35124 KB |
Output is correct |
42 |
Correct |
303 ms |
34616 KB |
Output is correct |
43 |
Correct |
258 ms |
31592 KB |
Output is correct |
44 |
Correct |
348 ms |
37696 KB |
Output is correct |
45 |
Correct |
351 ms |
37312 KB |
Output is correct |
46 |
Correct |
171 ms |
28840 KB |
Output is correct |
47 |
Correct |
354 ms |
34748 KB |
Output is correct |
48 |
Correct |
29 ms |
9476 KB |
Output is correct |
49 |
Correct |
462 ms |
40912 KB |
Output is correct |
50 |
Correct |
460 ms |
40556 KB |
Output is correct |
51 |
Correct |
442 ms |
40208 KB |
Output is correct |
52 |
Correct |
445 ms |
39560 KB |
Output is correct |
53 |
Correct |
414 ms |
37384 KB |
Output is correct |
54 |
Correct |
291 ms |
29864 KB |
Output is correct |
55 |
Correct |
458 ms |
41812 KB |
Output is correct |
56 |
Correct |
478 ms |
41564 KB |
Output is correct |
57 |
Correct |
272 ms |
32648 KB |
Output is correct |
58 |
Correct |
408 ms |
39128 KB |
Output is correct |
59 |
Correct |
95 ms |
15252 KB |
Output is correct |
60 |
Correct |
441 ms |
40772 KB |
Output is correct |
61 |
Correct |
434 ms |
40084 KB |
Output is correct |
62 |
Correct |
481 ms |
41972 KB |
Output is correct |
63 |
Correct |
277 ms |
33512 KB |
Output is correct |
64 |
Correct |
4 ms |
5452 KB |
Output is correct |
65 |
Correct |
5 ms |
5580 KB |
Output is correct |
66 |
Correct |
4 ms |
5528 KB |
Output is correct |
67 |
Correct |
4 ms |
5452 KB |
Output is correct |
68 |
Correct |
4 ms |
5452 KB |
Output is correct |
69 |
Correct |
5 ms |
5660 KB |
Output is correct |
70 |
Correct |
5 ms |
5580 KB |
Output is correct |
71 |
Correct |
6 ms |
5708 KB |
Output is correct |
72 |
Correct |
4 ms |
5580 KB |
Output is correct |
73 |
Correct |
5 ms |
5580 KB |
Output is correct |
74 |
Correct |
192 ms |
28460 KB |
Output is correct |
75 |
Correct |
348 ms |
36560 KB |
Output is correct |
76 |
Correct |
274 ms |
32956 KB |
Output is correct |
77 |
Correct |
215 ms |
29632 KB |
Output is correct |
78 |
Correct |
74 ms |
13280 KB |
Output is correct |
79 |
Correct |
79 ms |
12784 KB |
Output is correct |
80 |
Correct |
150 ms |
24388 KB |
Output is correct |
81 |
Correct |
354 ms |
39300 KB |
Output is correct |
82 |
Correct |
288 ms |
35264 KB |
Output is correct |
83 |
Correct |
395 ms |
42644 KB |
Output is correct |
84 |
Correct |
169 ms |
28736 KB |
Output is correct |
85 |
Correct |
248 ms |
33856 KB |
Output is correct |
86 |
Correct |
81 ms |
14960 KB |
Output is correct |
87 |
Correct |
469 ms |
39916 KB |
Output is correct |
88 |
Correct |
455 ms |
40060 KB |
Output is correct |
89 |
Correct |
315 ms |
32280 KB |
Output is correct |
90 |
Correct |
151 ms |
17764 KB |
Output is correct |
91 |
Correct |
154 ms |
19432 KB |
Output is correct |
92 |
Correct |
284 ms |
28928 KB |
Output is correct |
93 |
Correct |
461 ms |
42320 KB |
Output is correct |
94 |
Correct |
405 ms |
39504 KB |
Output is correct |
95 |
Correct |
553 ms |
47076 KB |
Output is correct |
96 |
Correct |
271 ms |
32684 KB |
Output is correct |
97 |
Correct |
354 ms |
36588 KB |
Output is correct |