_no0B

Untitled

Dec 13th, 2021
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define N ((int)1e5 + 8)
  4. #define MAX ((int)1e9 + 8)
  5. #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
  6.  
  7. using namespace std;
  8.  
  9. vector < int > edge[N]; /// edge[i] = list of adjacent nodes of node 'i'
  10.  
  11. bool vis[N];
  12. /// finding component
  13. void dfs(int node, vector < int >& comp)
  14. {
  15.     comp.push_back(node);
  16.     vis[node] = 1;
  17.     for(int a:edge[node]){
  18.         if(vis[a] == 0) dfs(a);
  19.     }
  20. } /// O ( n + m )
  21.  
  22. vector < int > comp[N];
  23.  
  24. /// finding cycle:
  25.  
  26. int par[N];
  27. void dfs(int node)
  28. {
  29.     vis[node] = 1;
  30.     for(int a:edge[node]){
  31.         if(vis[a] == 0){
  32.             dep[a] = dep[node] + 1;
  33.             par[a] = node;
  34.             dfs(a);
  35.         }
  36.         else if(dep[a] < dep[node] - 1){
  37.             /// cycle found;
  38.             int cur = node;
  39.             while(cur != a){
  40.                 cout<<cur<<" ";
  41.                 cur = par[cur];
  42.             }
  43.             cout<<a<<endl;
  44.         }
  45.     }
  46. }
  47.  
  48. void bfs(int source)
  49. {
  50.     memset(vis,0,sizeof vis);
  51.     queue < int > que;
  52.     que.push(source);
  53.     vis[source] = 1;
  54.     dis[source] = 0;
  55.     while(!que.empty()){
  56.         int node = que.front();
  57.         que.pop();
  58.         for(int a:edge[node]){
  59.             if(vis[a] == 0){
  60.                 que.push(a);
  61.                 vis[a] = 1;
  62.                 dis[a] = dis[node] + 1;
  63.             }
  64.         }
  65.     } /// O (n + m)
  66. }
  67.  
  68. int dxx[] = {0 , 1, 0, -1}; /// change in row
  69. int dyy[] = {-1 , 0, 1, 0}; /// change in col
  70.  
  71. /// +dxx[0], +dyy[0] => left
  72. /// +dxx[1] , +dyy[1] => down
  73.  
  74. bool isValid(int row , int col)
  75. {
  76.     if(min(row , col) < 0) return 0;
  77.     if(row >= n || col >= m) return 0;
  78.  
  79.     if(grid[row][col] == '#' || grid[row][col] == 'm') return 0;
  80.     return 1;
  81. }
  82.  
  83. void dfs(int row , int col) /// {row,col} => node
  84. {
  85.     for(int i = 0 ; i < 4 ; i++){
  86.         int x = row + dxx[i] , y = col = dyy[i];
  87.         if(isValid(x,y) && vis[x][y] == 0) dfs(x , y);
  88.     }
  89. }
  90.  
  91.  
  92. void bfs(int row , int col)
  93. {
  94.     queue < pair < int , int > > que;
  95.     que.push({row , col});
  96.     vis[row][col] = 1;
  97.     dis[row][col] = 0;
  98.     while(!que.empty()){
  99.         int row = que.front().first , col = que.front().second;
  100.         que.pop();
  101.         for(int i = 0 ; i < 4 ; i++){
  102.             int x = row + dxx[i] , y = col = dyy[i];
  103.             if(isValid(x,y) && vis[x][y] == 0){
  104.                 dis[x][y] = dis[row][col] + 1;
  105.                 vis[x][y] = 1;
  106.                 que.push({x,y});
  107.             }
  108.         }
  109.     } /// O (n + m)
  110. }
  111.  
  112.  
  113. int main()
  114. {
  115.  
  116.     int n , m;
  117.     cin>>n>>m; /// n = nodes , m = edges
  118.     while(m--){
  119.         int a, b; /// undirected
  120.         cin>>a>>b; /// a --> b , b --> a
  121.         edge[a].push_back(b);
  122.         edge[b].push_back(a);
  123.     }
  124.  
  125.     for(int node = 1 ; node <= n ; node++){ /// O(n)
  126.         for(int i:edge[node]){ /// O(sizeOf(edge[node]))
  127.             /// code
  128.         }
  129.     } /// O(m)
  130.  
  131.     for(int node = 1 ; node <= n ; node++){ /// O(n)
  132.         for(int i = 1 ; i <= n ; i++){ /// O(n)
  133.             if(edge[node][i] == 1){
  134.                 /// code
  135.             }
  136.         }
  137.     } /// O(n*n)
  138.  
  139.     /// finding connected components
  140.     int tot = 0;
  141.     for(int i = 1 ; i <= n ; i++){
  142.         if(vis[i] == 0){
  143.             dfs(i , comp[tot]);
  144.             tot++;
  145.         }
  146.     }
  147.  
  148.     cout<<tot<<endl;
  149.     for(int i = 0 ; i < tot; i++){
  150.         for(int a:comp[i]){
  151.             /// code;
  152.         }
  153.     }
  154.  
  155.     /// finding cycle
  156.     dep[1] = 0;
  157.     par[1] = 0;
  158.     dfs(1);
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.     BuildGraph();
  180.  
  181.     while(t--){
  182.         int src , des;
  183.         bfs(src);
  184.         if(vis[des] == 0) cout<<"impossible\n";
  185.         else cout<<dis[des]<<endl;
  186.     }
  187.  
  188. }
  189.  
  190. void BuildGraph()
  191. {
  192.     vector < int > prime = Seive(1000);
  193.     for(int i = 1 ; i <= 1000 ; i++){
  194.         for(int p:prime){
  195.             if(p >= i) break;
  196.             if(i % p == 0 && i+p <= 1000){
  197.                 edge[i].push_back(i+p);
  198.             }
  199.         }
  200.     }
  201. }
  202.  
  203.  
  204.  
  205.  
Advertisement
Add Comment
Please, Sign In to add comment