#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define F first
#define S second
//#define endl '\n'
//#define int ll
using namespace std;
vt<pair<int, int>> dsu;
int dsuValue(int x)
{
if (dsu[x].F == x) return x;
return dsu[x].F = dsuValue(dsu[x].F);
}
void dsuMerge(int x, int y)
{
x = dsuValue(x), y = dsuValue(y);
if (dsu[y].S < dsu[x].S) swap(x, y);
dsu[x].F = y;
dsu[y].S += dsu[x].S;
}
vt<vt<pair<int, int>>> g, mst;
vt<bool> marked, used;
void init(int N, int M, vt<int> U, vt<int> V, vt<int> W)
{
g.resize(N), mst.resize(N), dsu.resize(N), marked.resize(N), used.resize(N);
vt<pair<int, pair<int, int>>> edges;
for(int i = 0; i < M; i++)
{
g[U[i]].pb({W[i], V[i]});
g[V[i]].pb({W[i], U[i]});
edges.pb({W[i], {U[i], V[i]}});
}
sort(all(edges));
for(int i = 0; i < N; i++) sort(all(g[i])), dsu[i] = {i, 1};
for(int i = 0; i < M; i++)
{
int u, v, w;
u = edges[i].S.F, v = edges[i].S.S, w = edges[i].F;
if (dsuValue(u) == dsuValue(v)) continue;
mst[u].pb({v, w}), mst[v].pb({u, w});
dsuMerge(u, v);
}
}
bool completed;
vt<int> path, pathW;
void dfs1(int v, int to, int p = -1)
{
if (v == to) completed = 1;
if (completed) return;
for(auto u : mst[v])
{
if (u.F == p) continue;
path.pb(u.F), pathW.pb(u.S);
dfs1(u.F, to, v);
if (completed) return;
path.pop_back(), pathW.pop_back();
}
}
int maxFuel;
void dfs2(int v, int firstV, int firstU, int p = -1, int mx = 0)
{
used[v] = 1;
for(int i = 0; i < sz(g[v]); i++)
{
int u = g[v][i].S, w = g[v][i].F;
if ((u == p) || (v == firstV && u == firstU)) continue;
if (used[u])
{
maxFuel = min(maxFuel, max(mx, w));
break;
}
mx = max(mx, w);
if (sz(g[v]) - i >= 2)
{
if (g[v][i + 1].S == p || marked[g[v][i + 1].S])
{
if (sz(g[v]) - i >= 3) maxFuel = min(maxFuel, max(mx, g[v][i + 2].F));
}
else maxFuel = min(maxFuel, max(mx, g[v][i + 1].F));
}
dfs2(u, firstV, firstU, v, mx);
break;
}
}
int getMinimumFuelCapacity(int X, int Y)
{
completed = 0;
path.clear(), pathW.clear(), fill(all(marked), 0);
// CLEAR (FILL MARKED WITH 0) EVERYTHING
path.pb(X);
dfs1(X, Y);
for(int x : path) marked[x] = 1;
maxFuel = INT_MAX;
for(int i = 1; i < sz(path) - 1; i++)
{
for(auto v : g[path[i]])
{
if (marked[v.S]) continue;
maxFuel = min(maxFuel, v.F);
}
}
fill(all(used), 0);
used[Y] = 1;
dfs2(X, X, path[1]);
fill(all(used), 0);
used[X] = 1;
dfs2(Y, Y, path[sz(path) - 2]);
for(int x : pathW) maxFuel = max(maxFuel, x);
if (maxFuel == INT_MAX) return -1;
return maxFuel;
}
/**
main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("input.txt", "r", stdin);
int N, M, Q;
cin >> N >> M;
vt<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);
cin >> Q;
while(Q--)
{
int X, Y;
cin >> X >> Y;
cout << getMinimumFuelCapacity(X, Y) << endl;
}
}
/**/
Compilation message
swap.cpp:152:1: warning: "/*" within comment [-Wcomment]
152 | /**/
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
100 ms |
15232 KB |
Output is correct |
10 |
Correct |
183 ms |
21492 KB |
Output is correct |
11 |
Correct |
180 ms |
20340 KB |
Output is correct |
12 |
Correct |
192 ms |
20892 KB |
Output is correct |
13 |
Correct |
185 ms |
21576 KB |
Output is correct |
14 |
Execution timed out |
2068 ms |
18856 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Execution timed out |
2073 ms |
17700 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
3 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
248 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
100 ms |
15232 KB |
Output is correct |
11 |
Correct |
183 ms |
21492 KB |
Output is correct |
12 |
Correct |
180 ms |
20340 KB |
Output is correct |
13 |
Correct |
192 ms |
20892 KB |
Output is correct |
14 |
Correct |
185 ms |
21576 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
3 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
588 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
17 ms |
2736 KB |
Output is correct |
35 |
Correct |
166 ms |
21340 KB |
Output is correct |
36 |
Correct |
140 ms |
19012 KB |
Output is correct |
37 |
Correct |
124 ms |
17308 KB |
Output is correct |
38 |
Correct |
118 ms |
17112 KB |
Output is correct |
39 |
Correct |
134 ms |
17144 KB |
Output is correct |
40 |
Correct |
102 ms |
16108 KB |
Output is correct |
41 |
Correct |
162 ms |
20036 KB |
Output is correct |
42 |
Correct |
194 ms |
19892 KB |
Output is correct |
43 |
Correct |
137 ms |
19764 KB |
Output is correct |
44 |
Correct |
125 ms |
18716 KB |
Output is correct |
45 |
Correct |
141 ms |
19952 KB |
Output is correct |
46 |
Correct |
205 ms |
20728 KB |
Output is correct |
47 |
Correct |
135 ms |
17400 KB |
Output is correct |
48 |
Correct |
138 ms |
18328 KB |
Output is correct |
49 |
Correct |
92 ms |
13700 KB |
Output is correct |
50 |
Correct |
74 ms |
10616 KB |
Output is correct |
51 |
Correct |
120 ms |
16952 KB |
Output is correct |
52 |
Correct |
193 ms |
22772 KB |
Output is correct |
53 |
Correct |
236 ms |
25200 KB |
Output is correct |
54 |
Correct |
285 ms |
28116 KB |
Output is correct |
55 |
Correct |
138 ms |
22320 KB |
Output is correct |
56 |
Correct |
178 ms |
24104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
100 ms |
15232 KB |
Output is correct |
10 |
Correct |
183 ms |
21492 KB |
Output is correct |
11 |
Correct |
180 ms |
20340 KB |
Output is correct |
12 |
Correct |
192 ms |
20892 KB |
Output is correct |
13 |
Correct |
185 ms |
21576 KB |
Output is correct |
14 |
Execution timed out |
2068 ms |
18856 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
248 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
100 ms |
15232 KB |
Output is correct |
11 |
Correct |
183 ms |
21492 KB |
Output is correct |
12 |
Correct |
180 ms |
20340 KB |
Output is correct |
13 |
Correct |
192 ms |
20892 KB |
Output is correct |
14 |
Correct |
185 ms |
21576 KB |
Output is correct |
15 |
Execution timed out |
2068 ms |
18856 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |