1
0
Fork 0
cp-templates/trees/quick_union.cc

23 lines
410 B
C++
Raw Normal View History

2024-03-07 18:52:59 +08:00
class quick_union {
private:
vector<size_t> c;
public:
quick_union(size_t n) : c(n) {
iota(c.begin(), c.end(), 0);
}
size_t query(size_t i) {
if (c[i] != i) c[i] = query(c[i]);
return c[i];
}
void merge(size_t i, size_t j) {
c[query(i)] = query(j);
}
bool connected(size_t i, size_t j) {
return query(i) == query(j);
}
};