# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
393449 | peuch | Game (IOI14_game) | C++17 | 0 ms | 0 KiB |
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 <cstdio>
#include <cassert>
#include "game.h"
#include "game.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1510;
int N;
int rep[MAXN], tam[MAXN];
vector<int> grp[MAXN];
int conect[MAXN][MAXN];
int find(int a){
if(rep[a] == a) return a;
return rep[a] = find(rep[a]);
}
bool test(int u, int v){
u = find(u);
v = find(v);
if(rand() & 1) swap(u, v);
conect[u][v]++;
conect[v][u]++;
int cnt = 0;
for(int i = 0; i < grp[u].size(); i++)
cnt += conect[v][grp[u][i]];
return cnt == tam[u] * tam[v];
}
void uni(int a, int b){
a = find(a);
b = find(b);
if(rand() & 1) swap(a, b);
for(int i = 0; i < grp[a].size(); i++){
grp[b].push_back(grp[a][i]);
}
for(int i = 0; i < N; i++)
conect[b][i] += conect[a][i];
rep[a] = b;
tam[b] += tam[a];
}
void initialize(int n) {
srand(time(0));
N = n;
for(int i = 0; i < N; i++){
rep[i] = i;
grp[i].push_back(i);
tam[i] = 1;
}
}
int hasEdge(int u, int v) {
if(test(u, v)) {
uni(u, v);
return 1;
}
return 0;
}
int read_int() {
int x;
assert(scanf("%d", &x) == 1);
return x;
}
int main() {
int n, u, v;
n = read_int();
initialize(n);
for (int i = 0; i < n * (n - 1) / 2; i++) {
u = read_int();
v = read_int();
printf("%d\n", hasEdge(u, v));
}
return 0;
}