#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,fma")
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1500;
int n, p[N], sz[N];
unordered_set<int> adj[N];
bitset<N> vis;
int findSet(int x) {
return (p[x] == x ? x : p[x] = findSet(p[x]));
}
void unite(int x, int y) {
x = findSet(x);
y = findSet(y);
if (sz[x] < sz[y]) swap(x, y);
p[y] = x;
sz[x] += sz[y];
while (!adj[y].empty()) {
adj[*adj[y].begin()].erase(y);
adj[y].erase(adj[y].begin());
}
}
void addEdge(int x, int y) {
adj[x].insert(y);
adj[y].insert(x);
unite(x, y);
}
void removeEdge(int x, int y) {
adj[x].erase(y);
adj[y].erase(x);
}
int dfs(int x) {
vis[x] = 1;
int cnt = sz[x];
for (int y : adj[x]) {
if (vis[y]) continue;
cnt += dfs(y);
}
return cnt;
}
void initialize(int NN) {
n = NN;
for (int i = 0; i < n; i++) {
p[i] = i;
sz[i] = 1;
for (int j = i+1; j < n; j++) {
adj[i].insert(j);
adj[j].insert(i);
}
}
}
int hasEdge(int u, int v) {
u = findSet(u);
v = findSet(v);
if (u == v) return 0;
removeEdge(u, v);
vis.reset();
if (dfs(u) < n) {
addEdge(u, v);
return 1;
}
vis.reset();
if (dfs(v) < n) {
addEdge(u, v);
return 1;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |