Facebook
From Idiotic Lemur, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 284
  1. void dfs(int x){
  2.       if(cykl == false){
  3.               color[x] = 1;
  4.  
  5.               for(list<int>::iterator K = graf[x].begin(); K != graf[x].end(); K++){
  6.                       if(color[*K] == 0){
  7.                               dfs(*K);
  8.                       }
  9.                       else{
  10.                               if(color[*K] == 1){
  11.                                       cykl = true;
  12.                                       return;
  13.                               }
  14.                       }
  15.               }
  16.  
  17.               topsort.push_back(x);
  18.               color[x] = 2;
  19.       }
  20. }
  21.  
  22. void dfsMAX(int V){
  23.       cykl = false;
  24.  
  25.       topsort.clear();
  26.  
  27.       for(int i = 1; i <= V; i++){
  28.               if(color[i] != 2){
  29.                       dfs(i);
  30.               }
  31.               if(cykl == true)
  32.                       break;
  33.       }
  34. }