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("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<ll, ll> ii;
typedef vector<ll> vi;
#include "game.h"
struct DSU {
vi e;
void init(int N) {
e = vi(N, -1);
}
int root(int x) {
if (e[x] < 0) {
return x;
}
else {
e[x] = root(e[x]);
return e[x];
}
}
bool sameset(int x, int y) {
return(root(x) == root(y));
}
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (-e[x] < -e[y])
swap(x, y);
e[x] += e[y];
e[y] = x;
return true;
}
int size(int x) {
return -e[root(x)];
}
};
const int MAX = 1505;
int N;
DSU dsu;
int ccs;
vector<vi> adj(MAX, vi(MAX, 0));
// 0 if not done
// 1 if missing
// 2 if filled
void initialize(int n) {
N = n;
dsu.init(N);
ccs = N;
}
int hasEdge(int u, int v) {
/* cout<<"adj: "<<endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout<<adj[i][j]<<" ";
}
cout<<endl;
}
cout<<"ccs: "<<ccs<<endl;*/
// check if connecting u to v makes any "bad triangle"
// bad triangle u, v, w if (u, v) = 2, (u, w) = 2, and (v, w) isn't 1
// or if (v, w) = 2 and (u, w) isn't 1
bool can = true;
for (int i = 0; i < N; i++) {
if (adj[u][i] == 2 && adj[v][i] != 1)
can = false;
if (adj[v][i] == 2 && adj[u][i] != 1)
can = false;
}
if (can && ccs >= 3) {
adj[u][v] = 2;
adj[v][u] = 2;
if (!dsu.sameset(u, v)) {
dsu.merge(u, v);
ccs--;
}
return 1;
}
else {
adj[u][v] = 1;
adj[v][u] = 1;
return 0;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |