# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
700840 | primenumber_zz | Game (IOI14_game) | C++14 | 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<bits/stdc++.h>
using namespace std;
int N;
vector<int>road[1515];
struct UnionFind {
vector<int>par,size,cnt;
vector<vector<int>>cld;
UnionFind() {};
UnionFind(int n) {
par.resize(n);
size.resize(n,1);
cnt.resize(n);
cld.resize(n);
for(int i = 0; i < n; i++) {
par[i] = i;
cld[i].push_back(i);
}
}
int find(int x) {
if(par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
bool same(int u,int v) {
u = find(u);
v = find(v);
return (u == v);
}
void unite(int u,int v) {
u = find(u);
v = find(v);
if(u == v) return;
if(size[u] > size[v]) swap(u,v);
for(int i:cld[u]) {
cld[v].push_back(i);
for(int j:road[i]) {
if(!same(i,j) && same(j,v)) {
cnt[v] -= 2;
}
}
}
par[u] = v;
size[v] += size[u];
cnt[v] += cnt[u];
}
int consize(int x) {
x = find(x);
return size[x];
}
int concnt(int x) {
x = find(x);
return cnt[x];
}
};
UnionFind uf;
void initialize(int n) {
N = n;
UnionFind init(n);
uf = init;
}
int hasEdge(int u, int v){
if(uf.same(u,v)) {
return 0;
}
int a = uf.consize(u);
int b = uf.concnt(u);
if(a*(N-a) == b+1) {
uf.unite(u,v);
return 1;
}
a = uf.consize(v);
b = uf.concnt(v);
if(a*(N-a) == b+1) {
uf.unite(u,v);
return 1;
}
uf.cnt[uf.find(u)]++;
uf.cnt[uf.find(v)]++;
return 0;
}
int main() {
int N;
cin >> N;
initialize(N);
for(int i = 0; i < N*(N-1)/2; i++) {
int u,v;
cin >> u >> v;
cout << hasEdge(u,v) << endl;
}
}