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 <algorithm>
using namespace std;
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define oo 1000000001
struct edge{
int u, v;
int cost;
bool operator< (edge e) const{
return cost < e.cost;
}
};
vector<int> pr;
vector<edge> ed;
vector<int> deg;
vector<vector<int>> cmp;
vector<vector<pii>> arr;
vector<int> when;
void Union(edge e){
deg[e.u]++; deg[e.v]++;
int x = pr[e.u];
int y = pr[e.v];
if (x == y){
when[x] = min(when[x], e.cost);
return;
}
if (cmp[x].size() < cmp[y].size()) swap(x, y);
for (auto node : cmp[y]){
cmp[x].pb(node);
pr[node] = x;
arr[node].pb({x, e.cost});
}
cmp[y].clear();
when[x] = min(when[x], when[y]);
if (deg[e.u] >= 3 || deg[e.v] >= 3) when[x] = min(when[x], e.cost);
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
pr.resize(n);
ed.resize(m);
deg.resize(n);
cmp.resize(n);
arr.resize(n);
when.resize(n);
for (int i = 0; i < m; ++i) {
ed[i].u = u[i];
ed[i].v = v[i];
ed[i].cost = w[i];
}
for (int i = 0; i < n; ++i) {
pr[i] = i;
cmp[i].pb(i);
deg[i] = 0;
arr[i].pb({i, 0});
when[i] = oo;
}
sort(ed.begin(), ed.end());
for (int i = 0; i < m; ++i) {
Union(ed[i]);
}
}
int getMinimumFuelCapacity(int x, int y) {
int ans = 0;
if (when[pr[x]] == oo) return -1;
int cmpx = x, cmpy = y;
int a = 0, b = 0;
while (cmpx != cmpy || when[cmpx] == oo){
if (a == arr[x].size()){
cmpy = arr[y][b].f;
ans = max(ans, arr[y][b++].s);
} else if (b == arr[y].size()){
cmpx = arr[x][a].f;
ans = max(ans, arr[x][a++].s);
} else if (arr[x][a].s < arr[y][b].s){
cmpx = arr[x][a].f;
ans = max(ans, arr[x][a++].s);
} else{
cmpy = arr[y][b].f;
ans = max(ans, arr[y][b++].s);
}
}
ans = max(ans, when[cmpx]);
return ans;
}
Compilation message (stderr)
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:92:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | if (a == arr[x].size()){
| ~~^~~~~~~~~~~~~~~~
swap.cpp:95:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | } else if (b == arr[y].size()){
| ~~^~~~~~~~~~~~~~~~
# | 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... |