int[] f;
int N;
int n,m;
public UnionFind(int n) {
N = n+10;
f = new int[N];
for(int i=0;i<N;i++) f[i] = i;
}
public UnionFind() {
this((int)5e5);
}
int find(int x){
return x==f[x]?x:(f[x] = find(f[x]));
}
boolean merge(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx == fy) return false;
m++;
f[fx] = fy;
return true;
}
}
class Solution {
int[] dx = {1,0,-1,0};
int[] dy = {0,1,0,-1};
public List numIslands2(int m, int n, int[][] positions) {
int[][] g = new int[m][n];
UnionFind uf = new UnionFind(m*n);
int k = positions.length;
List list = new ArrayList<>();
for(var dot:positions){
if(g[dot[0]][dot[1]]==1){
list.add(uf.n-uf.m);
continue;
}
uf.n++;
g[dot[0]][dot[1]] = 1;