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 <bits/stdc++.h>
// #include "grader_swap.cpp"
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define test(x) cout << "Line(" << __LINE__ << ") " #x << ' ' << x << endl
#define printv(x) {\
for (auto i : x) cout << i << ' ';\
cout << endl;\
}
#define pii pair <int, int>
#define pll pair <lli, lli>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template<typename A, typename B>
ostream& operator << (ostream& o, pair<A, B> a){
return o << a.X << ' ' << a.Y;
}
template<typename A, typename B>
istream& operator >> (istream& o, pair<A, B> &a){
return o >> a.X >> a.Y;
}
const int mod = 1e9 + 7, abc = 864197532, Doludu = 123, N = 100001, logN = 18, K = 40;
vector <pii> adj[N], out_of_tree[N];
int n, m;
vector <int> dsu;
int Find(int v) {
if (dsu[v] == v) return v;
return dsu[v] = Find(dsu[v]);
}
bool Union(int u, int v) {
u = Find(u), v = Find(v);
if (u == v) return false;
dsu[u] = v;
return true;
}
void init(int _n, int _m, vector <int> u, vector <int> v, vector <int> w) {
n = _n, m = _m;
dsu.resize(n);
iota(all(dsu), 0);
vector <int> p(m);
iota(all(p), 0);
sort(all(p), [&](int i, int j) {
return w[i] < w[j];
});
for (int i : p) {
if (Union(u[i], v[i])) {
adj[u[i]].eb(v[i], w[i]);
adj[v[i]].eb(u[i], w[i]);
} else {
out_of_tree[u[i]].eb(v[i], w[i]);
out_of_tree[v[i]].eb(u[i], w[i]);
}
}
for (int i = 0; i < n; ++i) {
sort(all(out_of_tree[i]), [](pii i, pii j) {
return i.Y < j.Y;
});
}
}
vector <int> road, W;
bool vis[N];
bool dfs(int v, int goal) {
road.pb(v);
vis[v] = true;
if (v == goal) return true;
for (auto [u, w] : adj[v]) if (!vis[u]) {
W.pb(w);
if (dfs(u, goal)) return true;
W.pop_back();
}
road.pop_back();
return false;
}
int getMinimumFuelCapacity(int u, int v) {
road.clear();
W.clear();
for (int i = 0; i < n; ++i) vis[i] = false;
dfs(u, v);
int ans = 1 << 30;
for (int i = 1; i < road.size() - 1; ++i) {
for (auto [u, w] : adj[road[i]]) if (road[i - 1] != u && road[i + 1] != u) {
ans = min(ans, w);
}
for (auto [u, w] : out_of_tree[road[i]]) {
ans = min(ans, w);
}
}
vector <int> tmp;
for (auto [i, w] : adj[u]) if (i != road[1]) {
tmp.pb(w);
}
for (auto [i, w] : out_of_tree[u]) {
tmp.pb(w);
}
for (auto [i, w] : adj[v]) if (i != road[road.size() - 2]) {
tmp.pb(w);
}
for (auto [i, w] : out_of_tree[v]) {
tmp.pb(w);
}
sort(all(tmp));
if (tmp.size() >= 2) ans = min(ans, tmp[1]);
ans = max(ans, *max_element(all(W)));
if (ans == 1 << 30) ans = -1;
return ans;
}
/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
2 3 3
1 4 10
3
1 2
2 4
0 1
3 2
0 1 5
0 2 5
1
1 2
*/
Compilation message (stderr)
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 1; i < road.size() - 1; ++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... |