Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- using pii = pair<int, int>;
- vector<int> dfn, low;
- int ts, m, n, cc;
- bool ans;
- set<pii> vis;
- stack<pii> st;
- map<pii, int> id;
- map<int, int> cut;
- int root;
- void tarjan(int x, int y, const vector<vector<int>> &g) {
- int u = x * n + y;
- dfn[u] = low[u] = ++ts;
- int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
- vis.insert({x, y});
- int flag = 0;
- for (int i = 0; i < 4; ++i) {
- int nx = x + dir[i][0], ny = y + dir[i][1];
- if (nx >= 0 && nx < m && ny >= 0 && ny < n && g[nx][ny] == 1) {
- int v = nx * n + ny;
- if (dfn[v] == 0) {
- tarjan(nx, ny, g);
- low[u] = min(low[u], low[v]);
- if (dfn[u] <= low[v]) {
- flag++;
- if (u != root || flag > 1) {
- cut[u] = true;
- }
- }
- } else
- low[u] = min(low[u], dfn[v]);
- }
- }
- }
- public:
- bool isPossibleToCutPath(vector<vector<int>>& grid) {
- m = grid.size(), n = grid[0].size();
- if (m == 2 && n == 1 || m == 1 && n == 2) return false;
- dfn = low = vector<int>(m * n);
- ts = 0; ans = false; cc = 1;
- for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) {
- int u = i * n + j;
- if (!dfn[i]) root = u, tarjan(i, j, grid);
- }
- // tarjan(0, 0, grid);
- cout << "id0: " << id[{0, 0}] << " idlast: " << id[{m-1,n-1}] << endl;
- cout << "cut.size(): " << cut.size() << endl;
- // return id[{0, 0}] != id[{m-1, n-1}];
- return cut.size() != 0 || vis.count({m -1, n -1}) == 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement