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 <unordered_map>
#include <algorithm>
#include <vector>
#include <map>
#include "game.h"
using namespace std;
const int maxN = 1500;
int n;
int cnt[maxN][maxN];
unordered_map<int, int> nei[maxN];
int par[maxN], sz[maxN], centre[maxN];
int Root(int v) {
return v == par[v] ? v : par[v] = Root(par[v]);
}
void Union(int u, int v)
{
u = Root(u); v = Root(v);
if (u == v) return;
if (sz[u] > sz[v]) swap(u, v);
int c1 = centre[u], c2 = centre[u];
if (nei[c1].size() > nei[c2].size()) swap(c1, c2);
for (pair<int, int> to: nei[c1])
{
int t = to.first;
cnt[c1][t] -= to.second;
cnt[t][c1] -= to.second;
nei[c2][t] += to.second;
nei[t][c2] += to.second;
cnt[c2][t] += to.second;
cnt[t][c2] += to.second;
}
nei[c1].clear();
sz[v] += sz[u];
par[u] = v;
centre[v] = c2;
}
void initialize(int n_) {
n = n_;
for (int v = 0; v < n; ++v) {
par[v] = v, sz[v] = 1, centre[v] = v;
nei[v].reserve(n);
}
}
int hasEdge(int u, int v) {
int c1 = centre[Root(u)];
int c2 = centre[Root(v)];
++nei[c1][c2];
++nei[c2][c1];
++cnt[c1][c2];
++cnt[c2][c1];
if (cnt[c1][c2] == sz[Root(u)] * sz[Root(v)])
{
Union(u, v);
return 1;
}
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... |