# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1123086 | SalihSahin | Game (IOI14_game) | C11 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define pb push_back
//#define int long long
using namespace std;
#include "game.h"
int N;
vector<int> par;
struct DSU{
vector<int> arr;
int option;
};
vector<DSU> dsuarr;
vector<vector<int> > edge;
int fnd(int a){
if(a == par[a]) return a;
else return par[a] = fnd(par[a]);
}
void merge(int a, int b){
a = fnd(a), b = fnd(b);
if(a == b) return;
int new_op = dsuarr[a].option + dsuarr[b].option;
for(auto itr: dsuarr[a].arr){
for(auto itr2: dsuarr[b].arr){
if(edge[itr][itr2] == 0){
new_op -= 2;
edge[itr][itr2] = edge[itr2][itr] = -1;
}
}
}
par[b] = a;
for(auto itr: dsuarr[b].arr){
dsuarr[a].arr.pb(itr);
}
dsuarr[a].option = new_op;
}
void initialize(int n) {
N = n;
edge.resize(n);
for(int i = 0; i < n; i++){
edge[i].resize(n);
edge[i][i] = -1;
}
dsuarr.resize(n);
for(int i = 0; i < n; i++){
dsuarr[i].arr.pb(i);
dsuarr[i].option = n-1;
}
par.resize(n);
for(int i = 0; i < n; i++){
par[i] = i;
}
}
int hasEdge(int u, int v) {
int uset = fnd(u), vset = fnd(v);
if(uset == vset) return 0;
if(dsuarr[uset].option == 1 || dsuarr[vset].option == 1){
merge(uset, vset);
edge[uset][vset] = edge[vset][uset] = 1;
return 1;
}
dsuarr[uset].option--;
dsuarr[vset].option--;
edge[uset][vset] = edge[vset][uset] = -1;
return 0;
}