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 "game.h"
#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define sz(x) x.size()
using namespace std;
#define N 1505
set< pair<int,int> > edge_set[N];
int ata[N],rem[N],n;
int bul(int node) {
if(ata[node]==node) return node;
return ata[node]=bul(ata[node]);
}
int ok[N][N];
int cnt=0;
void initialize(int n) {
::n=n;
for(int i=0;i<n;i++) {
edge_set[i].clear();
for(int j=0;j<n;j++) {
if(j==i) continue ;
edge_set[i].insert({min(i,j),max(i,j)});
}
cnt=0;
ata[i]=i;
}
for(int i=0;i<n;i++) for(int j=0;j<n;j++) ok[i][j]=0;
}
void finish(int au) {
int node1=edge_set[au].begin()->st;
int node2=edge_set[au].begin()->nd;
ok[node1][node2]=ok[node2][node1]=1;
edge_set[bul(node1)].erase(edge_set[bul(node1)].find({min(node1,node2),max(node1,node2)}));
edge_set[bul(node2)].erase(edge_set[bul(node2)].find({min(node1,node2),max(node1,node2)}));
int anode1=bul(node1);
int anode2=bul(node2);
if(sz(edge_set[anode1])<sz(edge_set[anode2])) swap(anode1,anode2);
ata[anode2]=anode1;
if(sz(edge_set[bul(node1)])==1) {
finish(bul(node1));
}
}
int tot=0;
bool flag=false;
int hasEdge(int u, int v) {
tot++;
if(flag) printf("ok[u][v]-->%d\n",ok[u][v]);
if(ok[u][v]) {
++cnt;
assert(!(cnt==n && tot<n*(n-1)/2));
return 1;
}
int au=bul(u);
int av=bul(v);
if(flag) printf("au-->%d av-->%d\n",au,av);
edge_set[au].erase(edge_set[au].find({min(u,v),max(u,v)}));
edge_set[av].erase(edge_set[av].find({min(u,v),max(u,v)}));
if(sz(edge_set[au])==1) {
if(flag) printf("au-->%d\n",au);
finish(au);
}
if(sz(edge_set[av])==1) {
if(flag) printf("av-->%d\n",av);
finish(av);
}
return 0;
}
/*
#include <cstdio>
#include <cassert>
#include "game.h"
int read_int() {
int x;
assert(scanf("%d", &x) == 1);
return x;
}
int bab[N];
int fin(int node) {
if(bab[node]==node) return node;
return bab[node]=fin(bab[node]);
}
void bug(vector< pair<int,int> > ed,int n) {
flag=1;
initialize(n);
for(auto x:ed) {
printf("%d %d\n",x.st,x.nd);
printf("%d\n",hasEdge(x.st,x.nd));
}
}
int main() {
int n, u, v;
n = 5;
vector< pair<int,int> > ed;
for(int i=0;i<n;i++) {
for(int j=0;j<i;j++) {
ed.push_back(mp(i,j));
}
}
int tot=0;
do {
int comp=n;
for(int i=0;i<n;i++) bab[i]=i;
initialize(n);
for (int i = 0; i < n * (n - 1) / 2; i++) {
u = ed[i].st;
v = ed[i].nd;
// printf("%d %d ",u,v);
int res=hasEdge(u,v);
if(res) {
int bu=fin(u);
int bv=fin(v);
if(bu!=bv) {
bab[bu]=bv;
comp--;
}
}
if(comp==1 && i<n*(n-1)/2-1) {
bug(ed,n);
return 0;
}
}
if(comp>1) {
bug(ed,n);
return 0;
}
} while(next_permutation(ed.begin(),ed.end()));
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... |