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 <bits/stdc++.h>
constexpr int N = 1e5;
std::vector<int> e[N];
std::vector<int> U, V, W;
int n, m;
void init(int n, int m, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
::U = U;
::V = V;
::W = W;
::n = n;
::m = m;
}
std::vector<int> p;
std::vector<bool> vis;
bool have;
void dfs(int u, int P) {
p[u] = P;
vis[u] = true;
if (e[u].size() > 2) have = true;
for (auto v : e[u]) {
if (!vis[v]) {
dfs(v, u);
}
}
}
bool check(int X, int Y, int mid) {
for (int i = 0; i < n; i++) e[i].clear();
for (int i = 0; i < m; i++) {
if (W[i] <= mid) {
e[U[i]].emplace_back(V[i]);
e[V[i]].emplace_back(U[i]);
}
}
p.assign(n, -1);
vis.assign(n, false);
have = false;
dfs(X, -1);
if (p[Y] == -1)
return false;
if (have) {
return true;
}
int y = Y;
std::vector<int> got;
y = p[y];
while (y != -1 && p[y] != -1) {
got.push_back(y);
y = p[y];
}
y = Y;
std::vector<bool> bad(n);
for (auto x : got) {
bad[x] = true;
y = x;
}
for (int i = 0; i < n; i++) e[i].clear();
for (int i = 0; i < m; i++) {
if (W[i] <= mid && !bad[U[i]] && !bad[V[i]] && !(U[i] == X && V[i] == Y || U[i] == Y && V[i] == X)) {
e[U[i]].emplace_back(V[i]);
e[V[i]].emplace_back(U[i]);
}
}
p.assign(n, -1);
vis.assign(n, false);
dfs(X, -1);
if (p[Y] != -1)
return true;
return false;
}
int getMinimumFuelCapacity(int X, int Y) {
int l = -1, r = 1e9 + 2;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (check(X, Y, mid)) {
r = mid;
} else {
l = mid;
}
}
if (r == 1e9 + 2) r = -1;
return r;
}
Compilation message (stderr)
swap.cpp: In function 'bool check(int, int, int)':
swap.cpp:59:68: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
59 | if (W[i] <= mid && !bad[U[i]] && !bad[V[i]] && !(U[i] == X && V[i] == Y || U[i] == Y && V[i] == X)) {
# | 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... |