[Java] UnionFind
\nclass UnionFind{\n    int[] f;\n    public UnionFind(int size){\n        f = new int[size];\n        for(int i = 0; i < size; i++){\n            f[i] = i;\n  
```\nclass UnionFind{\n    int[] f;\n    public UnionFind(int size){\n        f = new int[size];\n        for(int i = 0; i < size; i++){\n            f[i] = i;\n        }\n    }\n    public int find(int x){\n        if (f[x] != x){\n            f[x] = find(f[x]);\n        }\n        return f[x];\n    }\n    public void unite(int x, int y){\n        int fx = find(x);\n        int fy = find(y);\n        f[f[y]] = fx;\n    }    \n}\nclass Solution {\n    public int findCircleNum(int[][] M) {\n        if (M.length == 0 || M[0].length == 0) return 0;\n        int row = M.length, column = M[0].length;\n        UnionFind uf = new UnionFind(row);\n        Set<Integer> set = new HashSet<Integer>();\n        for (int i = 0; i < row; i++){\n            for(int j = i + 1; j < column; j ++){\n                if (M[i][j] == 1){\n                    uf.unite(i,j);\n                }\n            }\n        }\n        for(int i=0; i<row; i++){\n            uf.f[i] = uf.find(i);\n            set.add(uf.f[i]);\n        }\n        return set.size();\n        \n    }\n}\n```
['Union Find', 'Java']