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>
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
struct my_dsu {
int n, cc;
vector<int> e, cant;
vector<vector<int>> cnt;
my_dsu() {};
my_dsu(int n): n(n), cc(n) {
e.resize(n);
cant.resize(n, 0);
cnt.resize(n, vector<int>(n, 0));
for (int i = 0; i < n; ++i) {
e[i] = -1;
cant[i] = 1;
for (int j = 0; j < n; ++j) {
if (i != j) {
cnt[i][j]++;
}
}
}
}
int get(int u) {
return e[u] < 0 ? u : u = get(e[u]);
}
bool need_merge(int u) {
return cant[u] == n - 1;
}
void del(int u, int v) {
u = get(u);
v = get(v);
cnt[u][v]--;
cnt[v][u]--;
cant[u] += cnt[u][v] == 0;
cant[v] += cnt[u][v] == 0;
}
void unite(int u, int v, bool big) {
if (e[u] > e[v]) {
swap(u, v);
}
cc--;
e[u] += e[v];
e[v] = u;
cant[u] = 0;
for (int j = 0; j < n; ++j) {
if (get(j) != u && j == get(j)) {
cnt[u][j] += cnt[v][j];
cant[j] -= cnt[j][v] == 0;
cant[j] -= cnt[j][u] == 0;
cnt[j][u] += cnt[j][v];
cnt[j][v] = 0;
cant[j]++;
cant[j] += cnt[j][u] == 0;
} else {
cnt[u][j] = 0;
}
cant[u] += cnt[u][j] == 0;
}
if (big) {
upd(u);
}
}
int check(int u, int v) {
u = get(u);
v = get(v);
if (cnt[u][v] == 1) {
unite(u, v, false);
return 1;
}
del(u, v);
return 0;
}
void upd(int u) {
u = get(u);
if (need_merge(u)) {
int v = -1;
for (int i = 0; i < n; ++i) {
if (cnt[u][i]) {
v = i;
break;
}
}
unite(u, v, true);
}
}
bool same(int u, int v) {
return get(u) == get(v);
}
} G1, G2;
int hasEdge(int u, int v) {
if (G1.same(u, v)) {
return G2.check(u, v);
}
G1.del(u, v);
G2.del(u, v);
G1.upd(u);
G1.upd(v);
return 0;
}
void initialize(int n) {
G1 = my_dsu(n);
G2 = my_dsu(n);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |