Fastrail08

Number of Islands (DSU Approach)

Aug 31st, 2025
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int dx[4] = {-1, 0, 1, 0};
  5. const int dy[4] = {0, 1, 0, -1};
  6.  
  7. class DS{
  8.     public:
  9.     map<pair<int, int>, pair<int, int> > parent;
  10.     map<pair<int, int>, int > size;
  11.    
  12.     DS(){}
  13.    
  14.     DS(int n, int m){
  15.         for(int i = 0; i < n; i++){
  16.             for(int j = 0; j < m; j++){
  17.                 parent[{i, j}] = {i, j};
  18.                 size[{i, j}] = 1;
  19.             }
  20.         }
  21.     }
  22.    
  23.    
  24.     pair<int, int> find(int r, int c){
  25.         if(make_pair(r, c) == parent[{r, c}]){
  26.             return {r, c};
  27.         }
  28.         auto p = parent[{r, c}];
  29.         return parent[{r, c}] = find(p.first, p.second);
  30.     }
  31.    
  32.     void Union(int u1, int v1, int u2, int v2){
  33.         pair<int, int> a = find(u1, v1), b = find(u2, v2);
  34.        
  35.         int ar = a.first, ac = a.second, br = b.first, bc = b.second;
  36.        
  37.         if(a != b){
  38.             if(size[{ar, ac}] >= size[{br, bc}]){
  39.                 parent[{br, bc}] = {ar, ac};
  40.                 size[{ar, ac}] += size[{br, bc}];
  41.             }
  42.             else{
  43.                 parent[{ar, ac}] = {br, bc};
  44.                 size[{br, bc}] += size[{ar, ac}];
  45.             }
  46.         }
  47.     }
  48. };
  49.  
  50. bool isValid(int nr, int nc, int n, int m){
  51.     if(nr < 0 || nr >= n || nc < 0 || nc >= m){
  52.         return false;
  53.     }
  54.     return true;
  55. }
  56.  
  57. vector<int> numOfIslands(int n, int m, vector<vector<int> > &operations){
  58.     vector<int> res;
  59.     vector<vector<int> > matrix(n, vector<int>(m, 0));
  60.     DS dsu(n, m);
  61.     int islands = 0;
  62.     for(int i = 0; i < operations.size(); i++){
  63.         int r = operations[i][0], c = operations[i][1];
  64.        
  65.         //If queries are repeated, such as [(1, 2), (1, 2)]
  66.         //If not continued, it will again do islands++, but it was already done.
  67.         if(matrix[r][c] == 1){
  68.             res.push_back(islands);
  69.             continue;
  70.         }
  71.         matrix[r][c] = 1;
  72.         islands++;
  73.         set<pair<int, int> > cc;
  74.         for(int k = 0; k < 4; k++){
  75.             int nr = r + dx[k], nc = c + dy[k];
  76.             if(isValid(nr, nc, n, m)){
  77.                 if(matrix[nr][nc] == 1 && cc.count(dsu.find(nr, nr)) == 0){
  78.                     islands--;
  79.                     dsu.Union(r, c, nr, nc);
  80.                     cc.insert(dsu.find(nr, nc));
  81.                     //don't break, it is possible that any of the neighbour can have 1
  82.                     //it will combine every neighbour into 1 cc
  83.                     //break;
  84.                 }
  85.             }
  86.         }
  87.         res.push_back(islands);
  88.     }
  89.     return res;
  90. }
  91.  
  92. int main() {
  93.     // your code goes here
  94.     int n, m, k;
  95.     cin >> n >> m >> k;
  96.     vector<vector<int> > operations(k, vector<int>(2));
  97.     for(int i = 0; i < k; i++){
  98.         cin >> operations[i][0] >> operations[i][1];
  99.     }
  100.     vector<int> res =  numOfIslands(n, m, operations);
  101.     for(int i = 0; i < res.size(); i++){
  102.         cout << res[i] << " ";
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment