2024-05-02 06:31:31 +01:00
|
|
|
return [[
|
|
|
|
class quick_union {
|
|
|
|
private:
|
|
|
|
vector<size_t> c, sz;
|
|
|
|
public:
|
|
|
|
quick_union(size_t n) : c(n), sz(n) {
|
|
|
|
iota(c.begin(), c.end(), 0);
|
|
|
|
sz.assign(n, 1);
|
|
|
|
}
|
2024-10-05 12:28:38 +01:00
|
|
|
|
2024-05-02 06:31:31 +01:00
|
|
|
size_t query(size_t i) {
|
|
|
|
if (c[i] != i) c[i] = query(c[i]);
|
|
|
|
return c[i];
|
|
|
|
}
|
2024-10-05 12:28:38 +01:00
|
|
|
|
2024-05-02 06:31:31 +01:00
|
|
|
void merge(size_t i, size_t j) {
|
|
|
|
if (connected(i, j)) return;
|
|
|
|
sz[query(j)] += sz[query(i)];
|
|
|
|
c[query(i)] = query(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool connected(size_t i, size_t j) {
|
|
|
|
return query(i) == query(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t query_size(size_t i) {
|
|
|
|
return sz[query(i)];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
]]
|