This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "swap.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct dsu {
vector<int> fa, sz, mx_deg;
vector<bool> ok;
dsu(int n) {
fa.resize(n + 10);
iota(fa.begin(), fa.end(), 0);
sz.assign(n + 10, 1);
mx_deg.assign(n + 10, 0);
ok.assign(n + 10, false);
}
int find(int x) {
while (x != fa[x]) x = fa[x] = fa[fa[x]];
return x;
}
bool merge(int x, int y) {
int px = find(x), py = find(y);
if (px == py) return false;
if (sz[py] > sz[px]) swap(px, py);
sz[px] += sz[py]; fa[py] = px;
mx_deg[px] = max(mx_deg[px], mx_deg[py]);
if (mx_deg[px] >= 3) ok[px] = true;
return true;
}
bool same(int x, int y) {
int px = find(x), py = find(y);
return px == py;
}
};
vector<pair<int, pair<int, int>>> edges;
int n;
void init(int _N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n = _N;
for (int i = 0; i < M; i++)
edges.push_back(make_pair(W[i], make_pair(U[i], V[i])));
sort(edges.begin(), edges.end());
}
int getMinimumFuelCapacity(int X, int Y) {
// cout << "++++++++++++++++\n";
dsu D(n);
vector<int> deg(n);
for (int i = 0; i < edges.size(); i++) {
int w = edges[i].first;
int u = edges[i].second.first, v = edges[i].second.second;
// cout << w << ' ' << u << ' ' << v << '\n';
if (D.same(u, v)) D.ok[D.find(u)] = true;
deg[u]++; deg[v]++;
if (deg[u] >= 3) {
D.mx_deg[D.find(u)] = 3;
D.ok[D.find(u)] = true;
}
if (deg[v] >= 3) {
D.mx_deg[D.find(v)] = 3;
D.ok[D.find(v)] = true;
}
D.merge(u, v);
if (D.same(X, Y) && D.ok[D.find(X)] == true) return w;
}
return -1;
}
// #include "grader.cpp"
Compilation message (stderr)
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |