自定义并查集模板.cpp 225 B

1234567891011
  1. int f[/*size*/];
  2. int find(int x) {
  3. return (f[x] == x) ? x : (f[x] = find(f[x]));
  4. }
  5. void merge(int x, int y) {
  6. int fx = find(x), fy = find(y);
  7. f[fx] = fy;
  8. }
  9. bool query(int x, int y) {
  10. return find(x) == find(y);
  11. }