#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
#include "swap.h"
const int MAX = 1e5 + 5;
int N, M;
vector<ii> adj[MAX]; // {vertex, dist}
vi wt;
bool checkm(int cut) { // minimum weight so has tree from 0
vi adjc[N];
for (int i = 0; i < N; i++) {
for (ii p : adj[i]) {
if (p.se <= cut)
adjc[i].pb(p.fi);
}
}
vector<bool> vis(N, false);
queue<int> q;
q.push(0);
vis[0] = true;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int y : adjc[x]) {
if (!vis[y]) {
q.push(y);
vis[y] = true;
}
}
}
vi adjd[N];
for (int i = 0; i < N; i++) {
if (!vis[i])
continue;
for (int x : adjc[i]) {
if (vis[x])
adjd[i].pb(x);
}
}
for (int i = 0; i < N; i++) {
if (adjd[i].size() >= 3)
return true;
}
return false;
}
int comp() {
int low = 0;
int high = M - 1;
int ans = -1; // minimum so can pass
while (low <= high) { // false, false, false, true, true, true
int mid = (low + high) / 2;
if (checkm(wt[mid])) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
if (ans != -1)
return wt[ans];
return -1;
}
int minv; // minimum so there's a tree
vi maxw(MAX, -1);
void dfs(int n) {
for (ii p : adj[n]) {
if (maxw[p.fi] == -1) {
maxw[p.fi] = max(maxw[n], p.se);
dfs(p.fi);
}
}
}
void init(int N_g, int M_g, vi U_g, vi V_g, vi W_g) {
N = N_g;
M = M_g;
for (int i = 0; i < M; i++) {
adj[U_g[i]].pb({V_g[i], W_g[i]});
adj[V_g[i]].pb({U_g[i], W_g[i]});
wt.pb(W_g[i]);
}
sort(wt.begin(), wt.end());
minv = comp();
maxw[0] = 0;
dfs(0);
/* cout<<"minv: "<<minv<<endl;
cout<<"maxw: ";
for (int i = 0; i < N; i++) {
cout<<maxw[i]<<" ";
}
cout<<endl;*/
}
bool check(int cut, int X, int Y) {
vi adjc[N];
for (int i = 0; i < N; i++) {
for (ii p : adj[i]) {
if (p.se <= cut)
adjc[i].pb(p.fi);
}
}
vector<bool> vis(N, false);
queue<int> q;
q.push(X);
vis[X] = true;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int y : adjc[x]) {
if (!vis[y]) {
q.push(y);
vis[y] = true;
}
}
}
if (!vis[Y])
return false;
vi adjd[N];
for (int i = 0; i < N; i++) {
if (!vis[i])
continue;
for (int x : adjc[i]) {
if (vis[x])
adjd[i].pb(x);
}
}
int cnte = 0; // number of edges
int sz = 0; // number of vertices
for (int i = 0; i < N; i++) {
cnte += adjd[i].size();
if (adjd[i].size() >= 1)
sz++;
}
cnte /= 2;
// cout<<"sz, cnte: "<<sz<<" "<<cnte<<endl;
if (cnte > sz - 1) // has a cycle
return true;
for (int i = 0; i < N; i++) {
if (adjd[i].size() >= 3)
return true;
}
return false;
}
int getMinimumFuelCapacity(int X, int Y) {
if (M == N)
return wt[M - 1];
if (minv == -1)
return -1;
int low = 0;
int high = M - 1;
int ans = -1; // minimum so can pass
while (low <= high) { // false, false, false, true, true, true
int mid = (low + high) / 2;
if (check(wt[mid], X, Y)) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
if (ans != -1)
return max(minv, ans);
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
2 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Correct |
2 ms |
3164 KB |
Output is correct |
9 |
Correct |
135 ms |
18000 KB |
Output is correct |
10 |
Correct |
155 ms |
21200 KB |
Output is correct |
11 |
Correct |
152 ms |
20936 KB |
Output is correct |
12 |
Correct |
166 ms |
21984 KB |
Output is correct |
13 |
Correct |
231 ms |
21964 KB |
Output is correct |
14 |
Correct |
130 ms |
18260 KB |
Output is correct |
15 |
Correct |
192 ms |
25076 KB |
Output is correct |
16 |
Correct |
192 ms |
24520 KB |
Output is correct |
17 |
Correct |
228 ms |
25824 KB |
Output is correct |
18 |
Correct |
268 ms |
25800 KB |
Output is correct |
19 |
Correct |
49 ms |
11092 KB |
Output is correct |
20 |
Correct |
212 ms |
25796 KB |
Output is correct |
21 |
Correct |
193 ms |
25224 KB |
Output is correct |
22 |
Correct |
205 ms |
26676 KB |
Output is correct |
23 |
Correct |
276 ms |
26932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Execution timed out |
2071 ms |
25376 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
2 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Correct |
2 ms |
3164 KB |
Output is correct |
9 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
2 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Correct |
2 ms |
3164 KB |
Output is correct |
9 |
Correct |
135 ms |
18000 KB |
Output is correct |
10 |
Correct |
155 ms |
21200 KB |
Output is correct |
11 |
Correct |
152 ms |
20936 KB |
Output is correct |
12 |
Correct |
166 ms |
21984 KB |
Output is correct |
13 |
Correct |
231 ms |
21964 KB |
Output is correct |
14 |
Correct |
130 ms |
18260 KB |
Output is correct |
15 |
Correct |
192 ms |
25076 KB |
Output is correct |
16 |
Correct |
192 ms |
24520 KB |
Output is correct |
17 |
Correct |
228 ms |
25824 KB |
Output is correct |
18 |
Correct |
268 ms |
25800 KB |
Output is correct |
19 |
Execution timed out |
2071 ms |
25376 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |