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 "grader.cpp"
#include <map>
#include <vector>
#include <cstring>
#include <assert.h>
#include <iostream>
using namespace std;
const int MAXN = 1505;
struct DSU
{
int n;
int parent[MAXN];
vector <int> nodes[MAXN];
int edges[MAXN][MAXN];
DSU(){}
DSU(int n)
{
this->n = n;
memset(this->edges, 0, sizeof(this->edges));
for(int i = 0;i<=n;i++)
{
this->parent[i] = i;
this->nodes[i] = {i};
}
}
int Find(int x)
{
if(parent[x]==x) return x;
parent[x] = Find(parent[x]);
return parent[x];
}
bool Union(int u, int v)
{
u = Find(u);
v = Find(v);
if(u==v) return false;
for(int x = 0;x<n;x++)
{
if(x!=Find(x)) continue;
edges[u][x] += edges[v][x];
edges[x][u] += edges[v][x];
}
for(int x: nodes[v]) nodes[u].push_back(x);
//for(int x: presentEdges[v]) welko.remEdge(Find(allEdges[x].first), Find(allEdges[x].second));
parent[v] = u;
//for(int x: presentEdges[v]) welko.addEdge(Find(allEdges[x].first), Find(allEdges[x].second));
return true;
}
void addEdge(int u, int v)
{
u = Find(u);
v = Find(v);
edges[u][v]++;
edges[v][u]++;
}
int getEdges(int u, int v)
{
u = Find(u);
v = Find(v);
return edges[u][v];
}
};
DSU T;
void initialize(int n)
{
T = DSU(n);
}
int eval(int u, int v)
{
u = T.Find(u);
v = T.Find(v);
return T.nodes[u].size()*T.nodes[v].size() - T.getEdges(u, v);
}
int hasEdge(int u, int v)
{
T.addEdge(u, v);
if(T.Find(u)==T.Find(v))
{
return 1;
}
int found = eval(u, v);
if(found==0)
{
T.Union(u, v);
return 1;
}
else
{
return 0;
}
}
/*
4
0 2
1 3
0 3
1 2
0 1
2 3
7
2 4
4 6
0 6
1 3
0 1
1 5
0 2
3 5
2 3
3 4
2 6
1 6
1 2
0 5
5 6
0 3
3 6
4 5
---------
2 5
0 4
1 4
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |