#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
vector <vector <pair <int, int>>> adj;
vector <array <int, 3>> edges;
int n, m;
bool comp(array <int, 3> a, array <int, 3> b) {
return a[2] < b[2];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
adj.resize(N+1);
n = N;
m = M;
for (int i = 0; i < M; i++) {
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
edges.push_back({U[i], V[i], W[i]});
}
sort(edges.begin(), edges.end(), comp);
}
struct DSU {
vector <int> link, sz;
vector <bool> cycle, connected;
DSU (int s) {
link = sz = vector <int> (s+1);
connected = cycle = vector <bool> (s+1);
for (int i = 1; i <= s; i++) {
link[i] = i;
sz[i] = 1;
}
}
int head(int x) {
if (link[x] != x)
return link[x] = head(link[x]);
return x;
}
bool same(int a, int b) {
return head(a) == head(b);
}
void unite(int a, int b) {
a = head(a);
b = head(b);
if (sz[a] < sz[b])
swap(a, b);
sz[a] += sz[b];
link[b] = a;
if (cycle[b])
cycle[a] = true;
if (connected[b])
connected[a] = true;
// cycle[a] |= cycle[b];
}
};
int getMinimumFuelCapacity(int x, int y) {
DSU dsu(n+1);
vector <int> connected(n);
for (int i = 0; i < m; i++) {
int a = edges[i][0], b = edges[i][1];
if (!dsu.same(a, b)) {
dsu.unite(a, b);
connected[a]++;
connected[b]++;
}
else {
dsu.cycle[dsu.head(a)] = true;
connected[a]++;
connected[b]++;
if (dsu.same(a, x)) {
if (dsu.same(x, y)) {
return edges[i][2];
}
}
}
if (connected[a] >= 3)
dsu.connected[dsu.head(a)] = true;
if (connected[b] >= 3)
dsu.connected[dsu.head(b)] = true;
if (dsu.same(x, y) && dsu.cycle[dsu.head(a)] && dsu.same(x, a))
return edges[i][2];
if (dsu.same(x, y)) {
if (dsu.same(x, a) && dsu.connected[dsu.head(x)])
return edges[i][2];
}
}
return -1;
}
// int main() {
// int N, M;
// cin >> N >> M;
// vector <int> u(M), v(M), w(M);
// for (int i = 0; i < M; i++)
// cin >> u[i] >> v[i] >> w[i];
// init(N, M, u, v, w);
// int q;
// cin >> q;
// while (q--) {
// int x, y;
// cin >> x >> y;
// cout << getMinimumFuelCapacity(x, y) << '\n';
// }
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
37 ms |
9492 KB |
Output is correct |
10 |
Correct |
54 ms |
11104 KB |
Output is correct |
11 |
Correct |
52 ms |
11028 KB |
Output is correct |
12 |
Correct |
57 ms |
11528 KB |
Output is correct |
13 |
Correct |
54 ms |
11532 KB |
Output is correct |
14 |
Execution timed out |
2080 ms |
10000 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
2035 ms |
15692 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
37 ms |
9492 KB |
Output is correct |
11 |
Correct |
54 ms |
11104 KB |
Output is correct |
12 |
Correct |
52 ms |
11028 KB |
Output is correct |
13 |
Correct |
57 ms |
11528 KB |
Output is correct |
14 |
Correct |
54 ms |
11532 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
6 ms |
1884 KB |
Output is correct |
35 |
Correct |
63 ms |
11252 KB |
Output is correct |
36 |
Correct |
60 ms |
11276 KB |
Output is correct |
37 |
Correct |
55 ms |
11276 KB |
Output is correct |
38 |
Correct |
59 ms |
11120 KB |
Output is correct |
39 |
Correct |
55 ms |
11020 KB |
Output is correct |
40 |
Correct |
52 ms |
10252 KB |
Output is correct |
41 |
Correct |
57 ms |
11576 KB |
Output is correct |
42 |
Correct |
56 ms |
11272 KB |
Output is correct |
43 |
Correct |
46 ms |
11284 KB |
Output is correct |
44 |
Correct |
60 ms |
12048 KB |
Output is correct |
45 |
Correct |
75 ms |
16028 KB |
Output is correct |
46 |
Correct |
55 ms |
11224 KB |
Output is correct |
47 |
Correct |
56 ms |
11136 KB |
Output is correct |
48 |
Correct |
59 ms |
12040 KB |
Output is correct |
49 |
Correct |
49 ms |
13724 KB |
Output is correct |
50 |
Correct |
40 ms |
10500 KB |
Output is correct |
51 |
Correct |
70 ms |
13260 KB |
Output is correct |
52 |
Correct |
86 ms |
17412 KB |
Output is correct |
53 |
Correct |
90 ms |
18524 KB |
Output is correct |
54 |
Correct |
105 ms |
19964 KB |
Output is correct |
55 |
Correct |
48 ms |
11228 KB |
Output is correct |
56 |
Correct |
137 ms |
17924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
37 ms |
9492 KB |
Output is correct |
10 |
Correct |
54 ms |
11104 KB |
Output is correct |
11 |
Correct |
52 ms |
11028 KB |
Output is correct |
12 |
Correct |
57 ms |
11528 KB |
Output is correct |
13 |
Correct |
54 ms |
11532 KB |
Output is correct |
14 |
Execution timed out |
2080 ms |
10000 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
37 ms |
9492 KB |
Output is correct |
11 |
Correct |
54 ms |
11104 KB |
Output is correct |
12 |
Correct |
52 ms |
11028 KB |
Output is correct |
13 |
Correct |
57 ms |
11528 KB |
Output is correct |
14 |
Correct |
54 ms |
11532 KB |
Output is correct |
15 |
Execution timed out |
2080 ms |
10000 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |