Advertisement
backlog

grp---004

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