Exemple #1
0
int &unionFind(const int &a)
{
    if(repr[a] == a)
        return repr[a];

    return repr[a] = unionFind(repr[a]);
}
Exemple #2
0
inline short int &unionFind(const short int &a)
{
    if(repr[a] != a)
        repr[a] = unionFind(repr[a]);

    return repr[a];
}
Exemple #3
0
bool unionUnion(const int &a, const int &b)
{
    int &fa = unionFind(a),
        &fb = unionFind(b);

    if(fa == fb)
        return false;

    if(fa < fb)
        repr[fb] = fa;

    else
        repr[fa] = fb;

    return true;
}
 int numIslands(vector<vector<char>> &grid) {
   if (grid.empty())
     return 0;
   int count = 0, R = grid.size(), C = grid[0].size();
   for (int row = 0; row < R; ++row) {
     for (int column = 0; column < C; ++column) {
       if (grid[row][column] == '1') {
         unionFind(grid, row, column, R, C);
         ++count;
       }
     }
   }
   return count;
 }