# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
581318 | Josia | 게임 (IOI14_game) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
int N;
struct chefNode {
int chef;
set<pair<int, int>> connections;
};
vector<chefNode> chefs;
void initUnionfind(int n) {
chefs.clear();
chefs.resize(n);
for (int i = 0; i<n; i++) {
chefNode x;
x.chef = i;
for (int j = 0; j<n; j++) {
if (i == j) continue;
x.connections.insert({i, j});
}
chefs[i] = x;
}
}
int find(int x) {
if (chefs[x].chef == x) return x;
return chefs[x].chef = find(chefs[x].chef);
}
void unite(int a, int b) {
a = find(a); b = find(b);
if (a == b) return;
chefs[a].chef = b;
for (auto i: chefs[a].connections)
chefs[b].connections.insert(i);
chefs[a].connections.clear();
set<pair<int, int>> newConnections;
for (auto i: chefs[b].connections)
if (find(i.first) != find(i.second)) newConnections.insert(i);
chefs[b].connections = newConnections;
}
void initialize(int n) {
N = n;
initUnionfind(n);
}
int hasEdge(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return 0; // return rand()%2;
if (chefs[u].connections.size() == 1 || chefs[v].connections.size() == 1) {
unite(u, v);
return 1;
}
return 0;
}