123456789101112131415 |
- int f[/*size*/];
- void init(int n) {
- for (int i = 1; i <= n; i++)
- f[i] = i;
- }
- int find(int x) {
- return (f[x] == x) ? x : (f[x] = find(f[x]));
- }
- void merge(int x, int y) {
- int fx = find(x), fy = find(y);
- f[fx] = fy;
- }
- bool query(int x, int y) {
- return find(x) == find(y);
- }
|