-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickFind.cpp
More file actions
26 lines (26 loc) · 938 Bytes
/
QuickFind.cpp
File metadata and controls
26 lines (26 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct QuickFind {
int n;
vector<int> group;
vector<vector<int>> item;
QuickFind(int n_) {
n = n_;
group.resize(n_);
item.resize(n_);
for (int i = 0; i < n_; i ++) {
group[i] = i;
item[i].assign(1, i);
}
}
bool same(int x, int y) { return group[x] == group[y]; }
void merge(int x, int y) {
if (same(x, y)) return;
if (item[group[x]].size() < item[group[y]].size()) swap(x, y);
int gx = group[x], gy = group[y];
for (int i : item[gy]) group[i] = gx;
item[gx].insert(item[gx].end(), item[gy].begin(), item[gy].end());
item[gy].clear();
n --;
}
int get() { return n; }
int get(int x) { return item[group[x]].size(); }
};