Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define white 1
- #define gray 2
- #define black 3
- int adj[100][100];
- int color[100];
- int node,edge;
- void dfsvisit(int x)
- {
- color[x]=gray;
- // some work
- for(int i=0;i<node;i++)
- {
- if(adj[x][i]==1)
- {
- if(color[i]==white)
- dfsvisit(i);
- }
- }
- color[x]=black;
- }
- void dfs()
- {
- for(int i=0;i<node;i++)
- {
- color[i]=white;
- }
- for(int i=0;i<node;i++)
- {
- if(color[i]==white)
- dfsvisit(i);
- }
- }
- int main()
- {
- freopen("input.txt","r",stdin);
- cin>>node;
- cin>>edge;
- int n1,n2;
- for(int i=0;i<edge;i++)
- {
- cin>>n1>>n2;
- adj[n1][n2]=1;
- adj[n2][n1]=1;
- }
- dfs();
- /*
- for(int i=0;i<node ;i++)
- {
- if(adj[1][i] == 1)
- cout<<i<<" ";
- }
- cout<<endl;&*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement