#include "swap.h"
#include <bits/stdc++.h>
#define ii pair<int, int>
#define iii pair<int, ii>
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 5;
int n, m, par[N], p1[N], p2[N], past[N], cur[N];
vector <iii> edg;
int root(int u)
{
return (par[u] < 0) ? u : par[u] = root(par[u]);
}
bool check(int u, int v, int mid)
{
while (par[u] > 0 && past[u] <= mid) u = par[u];
while (par[v] > 0 && past[v] <= mid) v = par[v];
if(u != v) return 0;
if(cur[u] && cur[u] <= mid) return 1;
return 0;
}
void join(int u, int v, int w)
{
int r1 = u, r2 = v;
u = root(u), v = root(v);
if(u == v)
{
if(!cur[u]) cur[u] = w;
return ;
}
if(par[u] > par[v]) swap(u, v), swap(r1, r2);
par[u] += par[v];
past[v] = w;
if(par[v] == -1 && !cur[u])
{
if(p1[u] == r1) p1[u] = v;
else if(p2[u] == r1) p2[u] = v;
else cur[u] = w;
}
else if(!cur[u] && !cur[v])
{
if(p1[u] == r1 && p1[v] == r2) p1[u] = p2[v];
else if(p1[u] == r1 && p2[v] == r2) p1[u] = p1[v];
else if(p2[u] == r1 && p1[v] == r2) p2[u] = p2[v];
else if(p2[u] == r1 && p2[v] == r2) p1[u] = p1[v];
else cur[u] = w;
}
else if(!cur[u]) cur[u] = w;
par[v] = u;
}
void init(int n, int m, vector <int> U, vector <int> V, vector <int> W)
{
for(int i = 1; i <= n; i++)
{
p1[i] = p2[i] = i;
par[i] = -1;
}
for(int i = 0; i < m; i++)
{
U[i]++, V[i]++;
edg.push_back({W[i], {U[i], V[i]}});
}
sort(edg.begin(), edg.end());
for(auto [w, e] : edg) join(e.fi, e.se, w);
}
int getMinimumFuelCapacity(int u, int v)
{
u++, v++;
int l = 1, r = edg[edg.size() - 1].fi, mid, ans = -1;
while (l <= r)
{
mid = (l + r) / 2;
if(check(u, v, mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
146 ms |
8312 KB |
Output is correct |
4 |
Correct |
139 ms |
8500 KB |
Output is correct |
5 |
Correct |
148 ms |
8772 KB |
Output is correct |
6 |
Correct |
140 ms |
8344 KB |
Output is correct |
7 |
Correct |
143 ms |
8604 KB |
Output is correct |
8 |
Correct |
141 ms |
8364 KB |
Output is correct |
9 |
Correct |
142 ms |
8420 KB |
Output is correct |
10 |
Correct |
139 ms |
8444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |