#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 20;
const int inf = 1e9 + 20;
bool flag[maxN];
vector<pair<int, int>> adj[maxN];
int deg[maxN];
int depth[maxN];
pair<int, int> par[maxN];
int T[maxN];
vector<pair<int, int>> tree;
int ROOT;
bool sub1, sub2;
int N, M, lim;
bool f1, f2;
int root(int u) {
if (par[u].first == -1) {
return u;
}
return root(par[u].first);
}
void update(int u, int v, int w) {
deg[u]++;
deg[v]++;
sub2 &= (deg[u] <= 2);
sub2 &= (deg[v] <= 2);
int ru = root(u);
int rv = root(v);
if (ru == rv) {
T[ru] = min(T[ru], w);
return;
}
if (depth[ru] > depth[rv]) {
swap(ru, rv);
}
T[rv] = min(T[rv], max(T[ru], w));
if (deg[u] >= 3 || deg[v] >= 3) {
T[rv] = min(T[rv], w);
}
par[ru] = {rv, w};
tree.push_back({ru, rv});
if (depth[ru] == depth[rv]) {
depth[rv]++;
}
}
void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
N = _N;
M = _M;
sub1 = (M == (N - 1));
sub2 = true;
vector<pair<int, pair<int, int>>> edges(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[i] = make_pair(W[i], make_pair(U[i], V[i]));
}
for (int i = 0; i < N; i++) {
T[i] = inf;
par[i] = {-1, -1};
}
sort(edges.begin(), edges.end());
for (auto e: edges) {
int w = e.first;
int u = e.second.first;
int v = e.second.second;
update(u, v, w);
}
for (int i = 0; i < N; i++) {
depth[i] = -1;
}
ROOT = root(0);
depth[ROOT] = 1;
reverse(tree.begin(), tree.end());
for (auto e: tree) {
int u = e.first;
int v = e.second;
assert(depth[v] != -1);
depth[u] = depth[v] + 1;
}
for (int i = 0; i < N; i++) {
assert(depth[i] != -1);
if (i != ROOT) {
assert(par[i].first != -1);
}
}
}
void dfs(int u) {
flag[u] = true;
int cnt = 0;
for (auto e: adj[u]) {
int v = e.first;
int w = e.second;
if (w <= lim) {
cnt++;
}
if (w <= lim && !flag[v]) {
dfs(v);
}
}
f1 &= (cnt <= 2);
f2 |= (cnt <= 1);
}
int getMinimumFuelCapacity(int u, int v) {
int tmp_u = u;
int tmp_v = v;
int res1 = 0;
int res2 = inf;
if (depth[u] < depth[v]) {
swap(u, v);
}
while (depth[u] != depth[v]) {
res1 = max(res1, par[u].second);
u = par[u].first;
assert(u != -1);
}
while (u != v) {
res1 = max(res1, par[u].second);
u = par[u].first;
res1 = max(res1, par[v].second);
v = par[v].first;
assert(u != -1);
assert(v != -1);
}
u = tmp_u;
int cur = 0;
int res7 = inf;
while (u != -1) {
res7 = min(res7, max(T[u], cur));
cur = max(cur, par[u].second);
u = par[u].first;
}
v = tmp_v;
cur = 0;
int res8 = inf;
while (v != -1) {
res8 = min(res8, max(T[v], cur));
cur = max(cur, par[v].second);
v = par[v].first;
}
int res = max(res1, min(res7, res8));
if (res == inf) {
return -1;
}
else {
return res;
}
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:114:6: warning: unused variable 'res2' [-Wunused-variable]
114 | int res2 = inf;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2676 KB |
Output is correct |
9 |
Correct |
56 ms |
12136 KB |
Output is correct |
10 |
Correct |
71 ms |
14028 KB |
Output is correct |
11 |
Correct |
61 ms |
13888 KB |
Output is correct |
12 |
Correct |
76 ms |
14436 KB |
Output is correct |
13 |
Correct |
65 ms |
14192 KB |
Output is correct |
14 |
Correct |
61 ms |
12536 KB |
Output is correct |
15 |
Correct |
135 ms |
17992 KB |
Output is correct |
16 |
Correct |
141 ms |
17716 KB |
Output is correct |
17 |
Correct |
139 ms |
18400 KB |
Output is correct |
18 |
Correct |
124 ms |
18056 KB |
Output is correct |
19 |
Correct |
63 ms |
9528 KB |
Output is correct |
20 |
Correct |
137 ms |
17780 KB |
Output is correct |
21 |
Correct |
139 ms |
17844 KB |
Output is correct |
22 |
Correct |
140 ms |
18416 KB |
Output is correct |
23 |
Correct |
124 ms |
18388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
131 ms |
18048 KB |
Output is correct |
4 |
Correct |
111 ms |
18428 KB |
Output is correct |
5 |
Correct |
113 ms |
18428 KB |
Output is correct |
6 |
Correct |
110 ms |
18412 KB |
Output is correct |
7 |
Correct |
109 ms |
18500 KB |
Output is correct |
8 |
Correct |
108 ms |
17980 KB |
Output is correct |
9 |
Correct |
104 ms |
18316 KB |
Output is correct |
10 |
Correct |
109 ms |
17936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2676 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
3 ms |
2676 KB |
Output is correct |
11 |
Correct |
2 ms |
2680 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2672 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2676 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2676 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
3 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
56 ms |
12136 KB |
Output is correct |
11 |
Correct |
71 ms |
14028 KB |
Output is correct |
12 |
Correct |
61 ms |
13888 KB |
Output is correct |
13 |
Correct |
76 ms |
14436 KB |
Output is correct |
14 |
Correct |
65 ms |
14192 KB |
Output is correct |
15 |
Correct |
3 ms |
2676 KB |
Output is correct |
16 |
Correct |
2 ms |
2680 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2672 KB |
Output is correct |
23 |
Correct |
3 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2676 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2676 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
3 ms |
2804 KB |
Output is correct |
34 |
Correct |
9 ms |
4436 KB |
Output is correct |
35 |
Correct |
57 ms |
14188 KB |
Output is correct |
36 |
Correct |
64 ms |
14180 KB |
Output is correct |
37 |
Correct |
63 ms |
14184 KB |
Output is correct |
38 |
Correct |
57 ms |
14040 KB |
Output is correct |
39 |
Correct |
62 ms |
14040 KB |
Output is correct |
40 |
Correct |
52 ms |
13292 KB |
Output is correct |
41 |
Correct |
63 ms |
14456 KB |
Output is correct |
42 |
Correct |
70 ms |
14244 KB |
Output is correct |
43 |
Correct |
63 ms |
14156 KB |
Output is correct |
44 |
Correct |
63 ms |
14948 KB |
Output is correct |
45 |
Correct |
88 ms |
18184 KB |
Output is correct |
46 |
Correct |
62 ms |
14196 KB |
Output is correct |
47 |
Correct |
57 ms |
14180 KB |
Output is correct |
48 |
Correct |
81 ms |
15084 KB |
Output is correct |
49 |
Correct |
77 ms |
15892 KB |
Output is correct |
50 |
Correct |
60 ms |
12768 KB |
Output is correct |
51 |
Correct |
77 ms |
15792 KB |
Output is correct |
52 |
Correct |
101 ms |
20092 KB |
Output is correct |
53 |
Correct |
117 ms |
21452 KB |
Output is correct |
54 |
Correct |
126 ms |
22976 KB |
Output is correct |
55 |
Correct |
72 ms |
14232 KB |
Output is correct |
56 |
Correct |
116 ms |
20912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2676 KB |
Output is correct |
9 |
Correct |
56 ms |
12136 KB |
Output is correct |
10 |
Correct |
71 ms |
14028 KB |
Output is correct |
11 |
Correct |
61 ms |
13888 KB |
Output is correct |
12 |
Correct |
76 ms |
14436 KB |
Output is correct |
13 |
Correct |
65 ms |
14192 KB |
Output is correct |
14 |
Correct |
61 ms |
12536 KB |
Output is correct |
15 |
Correct |
135 ms |
17992 KB |
Output is correct |
16 |
Correct |
141 ms |
17716 KB |
Output is correct |
17 |
Correct |
139 ms |
18400 KB |
Output is correct |
18 |
Correct |
124 ms |
18056 KB |
Output is correct |
19 |
Correct |
131 ms |
18048 KB |
Output is correct |
20 |
Correct |
111 ms |
18428 KB |
Output is correct |
21 |
Correct |
113 ms |
18428 KB |
Output is correct |
22 |
Correct |
110 ms |
18412 KB |
Output is correct |
23 |
Correct |
109 ms |
18500 KB |
Output is correct |
24 |
Correct |
108 ms |
17980 KB |
Output is correct |
25 |
Correct |
104 ms |
18316 KB |
Output is correct |
26 |
Correct |
109 ms |
17936 KB |
Output is correct |
27 |
Correct |
3 ms |
2676 KB |
Output is correct |
28 |
Correct |
2 ms |
2680 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2672 KB |
Output is correct |
35 |
Correct |
3 ms |
2772 KB |
Output is correct |
36 |
Correct |
9 ms |
4436 KB |
Output is correct |
37 |
Correct |
57 ms |
14188 KB |
Output is correct |
38 |
Correct |
64 ms |
14180 KB |
Output is correct |
39 |
Correct |
63 ms |
14184 KB |
Output is correct |
40 |
Correct |
57 ms |
14040 KB |
Output is correct |
41 |
Correct |
62 ms |
14040 KB |
Output is correct |
42 |
Correct |
52 ms |
13292 KB |
Output is correct |
43 |
Correct |
63 ms |
14456 KB |
Output is correct |
44 |
Correct |
70 ms |
14244 KB |
Output is correct |
45 |
Correct |
63 ms |
14156 KB |
Output is correct |
46 |
Correct |
63 ms |
14948 KB |
Output is correct |
47 |
Correct |
15 ms |
4616 KB |
Output is correct |
48 |
Correct |
137 ms |
18032 KB |
Output is correct |
49 |
Correct |
159 ms |
18164 KB |
Output is correct |
50 |
Correct |
145 ms |
18016 KB |
Output is correct |
51 |
Correct |
138 ms |
18060 KB |
Output is correct |
52 |
Correct |
137 ms |
17376 KB |
Output is correct |
53 |
Correct |
127 ms |
15740 KB |
Output is correct |
54 |
Correct |
161 ms |
18472 KB |
Output is correct |
55 |
Correct |
149 ms |
18012 KB |
Output is correct |
56 |
Correct |
163 ms |
18052 KB |
Output is correct |
57 |
Correct |
147 ms |
18988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
56 ms |
12136 KB |
Output is correct |
11 |
Correct |
71 ms |
14028 KB |
Output is correct |
12 |
Correct |
61 ms |
13888 KB |
Output is correct |
13 |
Correct |
76 ms |
14436 KB |
Output is correct |
14 |
Correct |
65 ms |
14192 KB |
Output is correct |
15 |
Correct |
61 ms |
12536 KB |
Output is correct |
16 |
Correct |
135 ms |
17992 KB |
Output is correct |
17 |
Correct |
141 ms |
17716 KB |
Output is correct |
18 |
Correct |
139 ms |
18400 KB |
Output is correct |
19 |
Correct |
124 ms |
18056 KB |
Output is correct |
20 |
Correct |
131 ms |
18048 KB |
Output is correct |
21 |
Correct |
111 ms |
18428 KB |
Output is correct |
22 |
Correct |
113 ms |
18428 KB |
Output is correct |
23 |
Correct |
110 ms |
18412 KB |
Output is correct |
24 |
Correct |
109 ms |
18500 KB |
Output is correct |
25 |
Correct |
108 ms |
17980 KB |
Output is correct |
26 |
Correct |
104 ms |
18316 KB |
Output is correct |
27 |
Correct |
109 ms |
17936 KB |
Output is correct |
28 |
Correct |
3 ms |
2676 KB |
Output is correct |
29 |
Correct |
2 ms |
2680 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2672 KB |
Output is correct |
36 |
Correct |
3 ms |
2772 KB |
Output is correct |
37 |
Correct |
9 ms |
4436 KB |
Output is correct |
38 |
Correct |
57 ms |
14188 KB |
Output is correct |
39 |
Correct |
64 ms |
14180 KB |
Output is correct |
40 |
Correct |
63 ms |
14184 KB |
Output is correct |
41 |
Correct |
57 ms |
14040 KB |
Output is correct |
42 |
Correct |
62 ms |
14040 KB |
Output is correct |
43 |
Correct |
52 ms |
13292 KB |
Output is correct |
44 |
Correct |
63 ms |
14456 KB |
Output is correct |
45 |
Correct |
70 ms |
14244 KB |
Output is correct |
46 |
Correct |
63 ms |
14156 KB |
Output is correct |
47 |
Correct |
63 ms |
14948 KB |
Output is correct |
48 |
Correct |
15 ms |
4616 KB |
Output is correct |
49 |
Correct |
137 ms |
18032 KB |
Output is correct |
50 |
Correct |
159 ms |
18164 KB |
Output is correct |
51 |
Correct |
145 ms |
18016 KB |
Output is correct |
52 |
Correct |
138 ms |
18060 KB |
Output is correct |
53 |
Correct |
137 ms |
17376 KB |
Output is correct |
54 |
Correct |
127 ms |
15740 KB |
Output is correct |
55 |
Correct |
161 ms |
18472 KB |
Output is correct |
56 |
Correct |
149 ms |
18012 KB |
Output is correct |
57 |
Correct |
163 ms |
18052 KB |
Output is correct |
58 |
Correct |
147 ms |
18988 KB |
Output is correct |
59 |
Correct |
63 ms |
9528 KB |
Output is correct |
60 |
Correct |
137 ms |
17780 KB |
Output is correct |
61 |
Correct |
139 ms |
17844 KB |
Output is correct |
62 |
Correct |
140 ms |
18416 KB |
Output is correct |
63 |
Correct |
124 ms |
18388 KB |
Output is correct |
64 |
Correct |
2 ms |
2772 KB |
Output is correct |
65 |
Correct |
2 ms |
2676 KB |
Output is correct |
66 |
Correct |
2 ms |
2772 KB |
Output is correct |
67 |
Correct |
2 ms |
2772 KB |
Output is correct |
68 |
Correct |
2 ms |
2676 KB |
Output is correct |
69 |
Correct |
3 ms |
2772 KB |
Output is correct |
70 |
Correct |
3 ms |
2772 KB |
Output is correct |
71 |
Correct |
3 ms |
2772 KB |
Output is correct |
72 |
Correct |
2 ms |
2772 KB |
Output is correct |
73 |
Correct |
3 ms |
2804 KB |
Output is correct |
74 |
Correct |
88 ms |
18184 KB |
Output is correct |
75 |
Correct |
62 ms |
14196 KB |
Output is correct |
76 |
Correct |
57 ms |
14180 KB |
Output is correct |
77 |
Correct |
81 ms |
15084 KB |
Output is correct |
78 |
Correct |
77 ms |
15892 KB |
Output is correct |
79 |
Correct |
60 ms |
12768 KB |
Output is correct |
80 |
Correct |
77 ms |
15792 KB |
Output is correct |
81 |
Correct |
101 ms |
20092 KB |
Output is correct |
82 |
Correct |
117 ms |
21452 KB |
Output is correct |
83 |
Correct |
126 ms |
22976 KB |
Output is correct |
84 |
Correct |
72 ms |
14232 KB |
Output is correct |
85 |
Correct |
116 ms |
20912 KB |
Output is correct |
86 |
Correct |
49 ms |
8992 KB |
Output is correct |
87 |
Correct |
177 ms |
18012 KB |
Output is correct |
88 |
Correct |
142 ms |
17996 KB |
Output is correct |
89 |
Correct |
134 ms |
18224 KB |
Output is correct |
90 |
Correct |
136 ms |
18932 KB |
Output is correct |
91 |
Correct |
133 ms |
18508 KB |
Output is correct |
92 |
Correct |
148 ms |
19088 KB |
Output is correct |
93 |
Correct |
195 ms |
23704 KB |
Output is correct |
94 |
Correct |
187 ms |
25188 KB |
Output is correct |
95 |
Correct |
214 ms |
26752 KB |
Output is correct |
96 |
Correct |
128 ms |
18188 KB |
Output is correct |
97 |
Correct |
178 ms |
21800 KB |
Output is correct |