[C++] Find components around 0s | DSU | O(n^2) Time and Space
\n/* \n    Time: O(n^2)\n    Space: O(n^2)\n    Tag: DSU, Matrix, Graph\n    Difficulty: M\n*/\n\nclass Solution {\n\n    vector<int> parent;\n    vector<int> r
```\n/* \n    Time: O(n^2)\n    Space: O(n^2)\n    Tag: DSU, Matrix, Graph\n    Difficulty: M\n*/\n\nclass Solution {\n\n    vector<int> parent;\n    vector<int> rank;\n\n    int find_(int node) {\n        return parent[node] == -1 ? node : parent[node] = find_(parent[node]);\n    }\n\n    void union_(int node1, int node2) {\n        node1 = find_(node1);\n        node2 = find_(node2);\n\n        if (node1 != node2) {\n            if (rank[node1] < rank[node2]) {\n                parent[node1] = node2;\n                rank[node2] += rank[node1];\n            } else {\n                parent[node2] = node1;\n                rank[node1] += rank[node2];\n            }\n        }\n    }\n\npublic:\n    int largestIsland(vector<vector<int>> &grid) {\n        int n = grid.size();\n        parent.resize(n * n, -1);\n        rank.resize(n * n, 1);\n\n        vector<pair<int, int>> moves = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j]) {\n                    for (auto move : moves) {\n                        if (i + move.first >= 0 && j + move.second >= 0 && i + move.first < n && j + move.second < n && grid[i + move.first][j + move.second]) {\n                            union_(i * n + j, (i + move.first) * n + (j + move.second));\n                        }\n                    }\n                }\n            }\n        }\n\n        int res = *max_element(rank.begin(), rank.end());\n\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j] == 0) {\n                    int curRes = 0;\n                    unordered_set<int> st;\n                    for (auto move : moves) {\n                        if (i + move.first >= 0 && j + move.second >= 0 && i + move.first < n && j + move.second < n && grid[i + move.first][j + move.second]) {\n                            int comp = find_((i + move.first) * n + (j + move.second));\n                            if (st.find(comp) == st.end()) {\n                                curRes += rank[comp];\n                                st.insert(comp);\n                            }\n                        }\n                        res = max(res, 1 + curRes);\n                    }\n                }\n            }\n        }\n        return res;\n    }\n};\n```
['Union Find', 'C', 'Matrix', 'C++']