#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
struct EDGE {
int u, v, w;
};
struct DSUNODE;
vector<DSUNODE> nodes;
struct DSUNODE {
int vert;
int parent;
int distToParent;
int size;
int left;
int right;
int when;
int leader(int mxpath = 2e9) {
if (parent == -1 || distToParent > mxpath) return vert;
return nodes[nodes[parent].leader(mxpath)].vert;
}
};
vector<EDGE> edges;
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
edges = vector<EDGE>(M);
nodes = vector<DSUNODE>(N);
for (int i = 0; i < N; i++) {
DSUNODE& a = nodes[i];
a.parent = -1;
a.distToParent = 0;
a.size = 1;
a.vert = a.left = a.right = i;
a.when = 2e9;
}
for (int i = 0; i < M; i++) {
edges[i] = {U[i], V[i], W[i]};
}
sort(edges.begin(), edges.end(), [](const EDGE& a, const EDGE& b){return a.w < b.w;});
//cout << "WORKS" << endl;
for (int i = 0; i < M; i++) {
//cout << nodes[edges[i].u].leader() << " " << nodes[edges[i].v].leader() << endl;
DSUNODE& u = nodes[nodes[edges[i].u].leader()];
DSUNODE& v = nodes[nodes[edges[i].v].leader()];
if (u.vert == v.vert) {
if ((edges[i].u == u.left && edges[i].v == u.right) || (edges[i].u == u.right && edges[i].v == u.left)) {
u.left = -1;
u.right = -1;
u.when = min(u.when, edges[i].w);
}
continue;
}
int newleft = -1, newright = -1;
if (edges[i].u == u.left && edges[i].v == v.left) {
newleft = u.right;
newright = v.right;
} else if (edges[i].u == u.left && edges[i].v == v.right) {
newleft = u.right;
newright = v.left;
} else if (edges[i].u == u.right && edges[i].v == v.left) {
newleft = u.left;
newright = v.right;
} else if (edges[i].u == u.right && edges[i].v == v.right) {
newleft = u.left;
newright = v.left;
}
if (u.size > v.size) {
v.parent = u.vert;
v.distToParent = edges[i].w;
u.size += v.size;
u.left = newleft;
u.right = newright;
if (newleft == -1) {
u.when = min(u.when, edges[i].w);
}
} else {
u.parent = v.vert;
u.distToParent = edges[i].w;
v.size += u.size;
v.left = newleft;
v.right = newright;
if (newleft == -1) {
v.when = min(v.when, edges[i].w);
}
}
}
}
bool can(int X, int Y, int M) {
int u = nodes[X].leader(M), v = nodes[Y].leader(M);
return u == v && M >= nodes[u].when;
}
int getMinimumFuelCapacity(int X, int Y) {
int l = 0, r = 1e9;
while (l + 1 < r) {
int m = (l + r) / 2;
if (can(X, Y, m)) r = m;
else l = m;
}
return r;
}
int mai2n() {
// int N, M;
//assert(2 == scanf("%d %d", &N, &M));
//std::vector<int> U(M), V(M), W(M);
//for (int i = 0; i < M; ++i) {
// assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
//}
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> X(Q), Y(Q);
for (int i = 0; i < Q; ++i) {
assert(2 == scanf("%d %d", &X[i], &Y[i]));
}
init(5, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});
std::vector<int> minimum_fuel_capacities(Q);
for (int i = 0; i < Q; ++i) {
minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
}
for (int i = 0; i < Q; ++i) {
printf("%d\n", minimum_fuel_capacities[i]);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |