#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, w;
};
int n, m;
vector<vector<edge>> v;
vector<bool> benne, seen;
bool dfs(int x, int d, int a, bool elso = true){
if(x == d) return true;
if(seen[x])return false;
if(benne[x])return false; // a masodik futashoz
seen[x] = true;
for(edge&i:v[x]){
if(i.w>a)continue;
if(elso && i.to == d)continue;
if(dfs(i.to, d, a, false)){
benne[x] = true;
return true;
}
}
return false;
}
bool dfs2(int x, int a){ // TODO: meg kell csinálni
if (benne[x] || seen[x]) return false;
seen[x] = true;
int szam = 0;
for (edge&i:v[x]){
if (i.w > a || benne[i.to]) continue;
szam++;
if (dfs2(i.to, a)) return true;
}
if (szam >= 3) return true;
return false;
}
bool jo(int a, int x, int y){
benne.assign(n, 0);
seen.assign(n, 0);
bool talaltelso = dfs(x, y, a);
benne[x] = benne[y] = false;
bool kozvetlen = false;
for(edge&i:v[x]){
if(i.to == y && i.w <= a){
kozvetlen = true;
if(talaltelso) return true;
}
}
if(talaltelso){
seen.assign(n, 0);
if(dfs(x, y, a))return true;
benne[x] = benne[y] = false;
} else if(!kozvetlen) {
return false;
}
for(int i = 0; i < n; i++){
if(!benne[i])continue;
for(edge&j:v[i]){
if(j.w <= a && !benne[j.to]){
return true;
}
}
}
seen.assign(n, false);
if(dfs2(x, a)) return true;
seen.assign(n, false);
if(dfs2(y, a)) return true;
return false;
}
void init(int N, int M,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n = N, m = M;
v.assign(n, {});
for(int i = 0; i < M; i++){
v[U[i]].push_back({V[i], W[i]});
v[V[i]].push_back({U[i], W[i]});
}
}
int getMinimumFuelCapacity(int X, int Y) {
int l = 0, r = 1e9+1;
while(l < r){
int m = (l+r)/2;
if(jo(m, X, Y)){
r = m;
} else {
l = m+1;
}
}
return l == 1e9+1 ? -1 : l;
}
/*
6 5
0 1 1
1 2 2
2 3 4
2 4 3
3 5 5
5
0 5
0 3
1 5
4 3
2 4
6 5
0 1 1
1 2 2
2 3 4
2 4 3
3 5 5
1
0 5
*/
/*
4 3
0 1 1
0 2 2
0 3 3
4
1 2
1 3
0 1
3 0
*/
/*
4 4
0 1 3
1 2 5
2 3 7
3 0 4
3
1 3
0 2
0 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |