Advertisement
_no0B

Untitled

Dec 24th, 2021
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define N ((int)6e4 + 5)
  4. #define MOD ((int)1e9 + 7)
  5. #define MAX ((int)1e9 + 7)
  6. #define MAXL ((ll)1e18 + 7)
  7. #define MAXP ((int)1e3 + 7)
  8. #define thr 1e-8
  9. #define pi acos(-1)  /// pi = acos ( -1 )
  10. #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
  11. #define endl "\n"
  12.  
  13. using namespace std;
  14.  
  15. /// coloring
  16.  
  17. /// coloring type 1
  18.  
  19. bool dfs(int n, int c)
  20. {
  21.     if(vis[n] == 1){
  22.         if(col[n] == c) return true;
  23.         else return false;
  24.     }
  25.  
  26.     vis[n] = 1;
  27.     col[n] = c;
  28.     bool ans = true;
  29.     for(int a:edge[n]){
  30.         ans &= dfs(a , !c);
  31.     }
  32.     return ans;
  33. }
  34.  
  35. vector < pair < int , int > > edge[N];
  36.  
  37. vector < int > comp[2];
  38.  
  39. /// coloring type 2
  40.  
  41. bool dfs(int n , int c)
  42. {
  43.     if(vis[n] == 1){
  44.         if(col[n] == c) return true;
  45.         else return false;
  46.     }
  47.  
  48.     vis[n] = 1;
  49.     col[n] = c;
  50.     comp[c].push_back(n);
  51.  
  52.     bool ans = true;
  53.  
  54.     for(auto p:edge[n]){
  55.         int a = p.first , type = p.second;
  56.         ans &= dfs(a , c ^ type);
  57.     }
  58.     return ans;
  59. }
  60.  
  61.  
  62.  
  63. /// problem: https://www.spoj.com/problems/SERGRID/
  64.  
  65. int dxx[] = {0,0,1,-1};
  66. int dyy[] = {1,-1,0,0};
  67.  
  68. bool IsValid(int  row , int col)
  69. {
  70.     if(min(row , col) < 0 || row >= n || col >= m || dis[row][col] != -1) return 0;
  71.     return 1;
  72. }
  73.  
  74. void bfs(int row , int col)
  75. {
  76.     memset(dis,-1,sizeof dis);
  77.     queue <pair < int , int > > que;
  78.     que.push({row , col});
  79.     dis[row][col] = 0;
  80.  
  81.     while(!que.empty()){
  82.         row = que.front().first, col = que.front().second;
  83.         que.pop();
  84.         int digit = str[row][col] - '0';
  85.         for(int i = 0 ; i < 4 ; i++){
  86.             int x  = row + dxx[i]*digit , y = col + dyy[i]*digit;
  87.             if(IsValid(x,y)){
  88.                 dis[x][y] = dis[row][col] + 1;
  89.                 que.push({x,y});
  90.             }
  91.         }
  92.     }
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement