Advertisement
backlog

grp---005

Feb 3rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. //travverse a graph time
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define white 1
  7. #define gray 2
  8. #define black 3
  9.  
  10. int ttime = 1;
  11. int stime[100];
  12. int fftime[100];
  13.  
  14.  
  15. int adj[100][100];
  16. int color[100];
  17. int node,edge;
  18.  
  19. void dfsvisit(int x)
  20. {
  21.     color[x]=gray;
  22.     stime[x]=ttime;
  23.     ttime++;
  24.  
  25.    // some work
  26.     for(int i=0;i<node;i++)
  27.     {
  28.         if(adj[x][i]==1)
  29.         {
  30.             if(color[i]==white)
  31.                 dfsvisit(i);
  32.         }
  33.     }
  34.  
  35.     color[x]=black;
  36.     fftime[x]=ttime;
  37.     ttime++;
  38. }
  39.  
  40.  
  41. void dfs()
  42. {
  43.     for(int i=0;i<node;i++)
  44.     {
  45.         color[i]=white;
  46.     }
  47.     for(int i=0;i<node;i++)
  48.     {
  49.         if(color[i]==white)
  50.             dfsvisit(i);
  51.     }
  52. }
  53. int main()
  54. {
  55.  
  56.     freopen("input.txt","r",stdin);
  57.     cin>>node;
  58.     cin>>edge;
  59.  
  60.     int n1,n2;
  61.     for(int i=0;i<edge;i++)
  62.     {
  63.         cin>>n1>>n2;
  64.  
  65.         adj[n1][n2]=1;
  66.         adj[n2][n1]=1;
  67.     }
  68.     dfs();
  69.    cout<<"\n";
  70.    for(int i=0;i<node;i++)
  71.    {
  72.        cout<<"node : " <<(char) ('A'+i)<<" "<<stime[i]<<" "<<fftime[i]<<endl;
  73.    }
  74.    cout<<endl;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement