Advertisement
pb_jiang

LC2556 AC

Feb 4th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. class Solution {
  2.     using pii = pair<int, int>;
  3.     vector<int> dfn, low;
  4.     int ts, m, n, cc;
  5.     bool ans;
  6.     set<pii> vis;
  7.     stack<pii> st;
  8.     map<pii, int> id;
  9.     map<int, int> cut;
  10.     int root;
  11.    
  12.     void tarjan(int x, int y, const vector<vector<int>> &g) {
  13.         int u = x * n + y;
  14.         dfn[u] = low[u] = ++ts;
  15.         int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  16.         vis.insert({x, y});
  17.         int flag = 0;
  18.        
  19.         for (int i = 0; i < 4; ++i) {
  20.             int nx = x + dir[i][0], ny = y + dir[i][1];
  21.             if (nx >= 0 && nx < m && ny >= 0 && ny < n && g[nx][ny] == 1) {
  22.                 int v = nx * n + ny;
  23.                 if (dfn[v] == 0) {
  24.                     tarjan(nx, ny, g);
  25.                     low[u] = min(low[u], low[v]);
  26.                     if (dfn[u] <= low[v]) {
  27.                         flag++;
  28.                         if (u != root || flag > 1) {
  29.                             cut[u] = true;
  30.                         }
  31.                     }
  32.                 } else
  33.                     low[u] = min(low[u], dfn[v]);
  34.             }
  35.         }
  36.     }
  37. public:
  38.     bool isPossibleToCutPath(vector<vector<int>>& grid) {
  39.         m = grid.size(), n = grid[0].size();
  40.         if (m == 2 && n == 1 || m == 1 && n == 2) return false;
  41.        
  42.         dfn = low = vector<int>(m * n);
  43.         ts = 0; ans = false; cc = 1;
  44.        
  45.         for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) {
  46.             int u = i * n + j;
  47.             if (!dfn[i]) root = u, tarjan(i, j, grid);
  48.         }
  49.         // tarjan(0, 0, grid);
  50.         cout << "id0: " << id[{0, 0}] << " idlast: " << id[{m-1,n-1}] << endl;
  51.         cout << "cut.size(): " << cut.size() << endl;
  52.         // return id[{0, 0}] != id[{m-1, n-1}];
  53.         return cut.size() != 0 || vis.count({m -1, n -1}) == 0;
  54.     }    
  55. };
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement