This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,O3,unroll-loops")
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MaxN = 100002;
const int MaxM = 200002;
int N, M;
const int blockLength = 850;
struct Set{
int parent;
bool line;
pair<int, int> tail;
Set(){};
Set(int i){
parent = i;
line = true;
tail = make_pair(i, i);
}
};
Set t[MaxN];
int MAGIC;
bool flag[MaxN];
Set mem[MaxN][MaxM / blockLength + 3];
int re[MaxN];
int _nxt;
int leader(int x){
if (MAGIC != -1 && !flag[x]){
t[x] = (MAGIC == 0 ? Set(x) : mem[x][MAGIC - 1]);
flag[x] = true;
re[_nxt++] = x;
}
return t[x].parent == x ? x : t[x].parent = leader(t[x].parent);
}
void unite(int x, int y){
int X = x, Y = y;
x = leader(x);
y = leader(y);
if (rng() & 1){
swap(x, y);
swap(X, Y);
}
if (x == y){
t[y].line = false;
return;
}
t[x].parent = y;
if (!t[x].line){
t[y].line = false;
return;
}
if (!t[y].line) return;
if (t[x].tail.first != X && t[x].tail.second != X){
t[y].line = false;
return;
}
if (t[y].tail.first != Y && t[y].tail.second != Y){
t[y].line = false;
return;
}
pair<int, int> new_tail;
new_tail.first = t[x].tail.first ^ t[x].tail.second ^ X;
new_tail.second = t[y].tail.first ^ t[y].tail.second ^ Y;
t[y].tail = new_tail;
}
int el[MaxM], er[MaxM];
int blockCount;
bool sub1, sub2;
tuple<int, int, int> edges[MaxM];
int foo[MaxN];
void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
N = _N, M = _M;
vector<int> deg(N, 0);
sub2 = true;
for (int i = 0; i < M; i++){
edges[i] = make_tuple(W[i], U[i], V[i]);
if (U[i] != 0) sub2 = false;
deg[U[i]]++;
deg[V[i]]++;
foo[V[i]] = W[i];
}
sub1 = true;
for (int i = 0; i < N; i++){
if (deg[i] > 2) sub1 = false;
}
sort(edges, edges + M);
for (int i = 0; i < N; i++) t[i] = Set(i);
MAGIC = -1;
blockCount = 0;
for (int i = 0; i < M; i++){
int u = get<1>(edges[i]), v = get<2>(edges[i]);
unite(u, v);
if ((i + 1) % blockLength == 0 || i == M - 1){
int id = blockCount++;
el[id] = 0, er[id] = i;
if (id > 0) el[id] = er[id - 1] + 1;
for (int j = 0; j < N; j++) mem[j][id] = t[leader(j)];
}
}
for (int i = 0; i < N; i++) flag[i] = false;
}
bool can(int x, int y){
x = leader(x);
y = leader(y);
return x == y && !t[x].line;
}
int getMinimumFuelCapacity(int X, int Y) {
if (sub1) return (M == N ? get<0>(edges[M - 1]) : -1);
if (sub2){
if (X == 0){
return max(foo[Y], get<0>(edges[2]));
} else {
multiset<int> s;
for (int i = 0; i < 3; i++) s.insert(get<0>(edges[i]));
if (s.find(foo[X]) != s.end()) s.erase(s.find(foo[X]));
if (s.find(foo[Y]) != s.end()) s.erase(s.find(foo[Y]));
return max({foo[X], foo[Y], *s.begin()});
}
}
int id = 0;
while (id < blockCount){
int xx = mem[X][id].parent, yy = mem[Y][id].parent;
if (xx == yy && !mem[xx][id].line){
break;
} else {
id++;
}
}
if (id == blockCount) return -1;
MAGIC = id;
int ans = -1;
_nxt = 0;
for (int i = el[id]; i <= er[id]; i++){
auto [w, u, v] = edges[i];
unite(u, v);
if (can(X, Y)){
ans = w;
break;
}
}
for (int i = 0; i < _nxt; i++) flag[re[i]] = false;
return ans;
}
# | 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... |