Simple Union Find
Code\n\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        int n = isConnected.length;\n        int[] parent = new int[n+1];\n      
# Code\n```\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        int n = isConnected.length;\n        int[] parent = new int[n+1];\n        int[] size = new int[n+1];\n\n        for(int i=0; i<= n; i++){\n            parent[i] = i;\n            size[i] = 1;\n        }\n\n        for(int i= 0; i< n; i++){\n            for(int j =0; j< n; j++){\n                if(isConnected[i][j] == 1){\n                    int u = find(parent, i+1);\n                    int v = find(parent, j+1);\n\n                    if(u != v){\n                        union(parent, size, u, v);\n                    }\n                }\n            }\n        }\n        int provinces =0;\n        for(int i=1; i<= n; i++){\n            if(parent[i] == i) provinces++;\n        }\n        return provinces;\n    }\n    private int find(int[] parent, int i){\n        if(parent[i] != i){\n            parent[i] = find(parent, parent[i]);\n        }\n        return parent[i];\n    }\n    private void union(int[] parent, int[] size, int u, int v){\n        if(u < v){\n            parent[u] =v;\n            size[v] += size[u];\n        }\n        else{\n            parent[v] =u;\n            size[u] += size[v];\n        }\n    }\n}\n```
['Java']