Advertisement
FanaticExplorer

find_circle_in_graph_cpp

Oct 29th, 2022
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int>used;
  6. bool found = true;
  7. int n = 5;
  8.    
  9. vector<vector<int>>g {
  10.   {2, 4}, //1
  11.   {1, 3}, //2
  12.   {2, 4}, //3
  13.   {1, 3, 5}, //4
  14.   {4}, //5  
  15. };
  16.  
  17. void dfs(int x){
  18.  
  19.     used[x] = 1;
  20.    
  21.     for(int i = 0;i<g[x].size();++i){
  22.        
  23.         int num = g[x][i];
  24.        
  25.         if(used[num] == 1)found = 1;
  26.         if(used[num] == 0)dfs(num);
  27.        
  28.     }
  29.    
  30.     used[x] = 2;
  31. }
  32.  
  33.  
  34. int main() {
  35.     for(int i = 0;i<n;++i){
  36.         if(!used[i]) dfs(i);
  37.     }
  38.     if(found)cout << "Circle";
  39.     else cout << "No circle";
  40.    
  41.     return 0;
  42. }
  43.  
Tags: cpp circle graph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement