Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //travverse a graph time
- #include<bits/stdc++.h>
- using namespace std;
- #define white 1
- #define gray 2
- #define black 3
- int ttime = 1;
- int stime[100];
- int fftime[100];
- int adj[100][100];
- int color[100];
- int node,edge;
- void dfsvisit(int x)
- {
- color[x]=gray;
- stime[x]=ttime;
- ttime++;
- // some work
- for(int i=0;i<node;i++)
- {
- if(adj[x][i]==1)
- {
- if(color[i]==white)
- dfsvisit(i);
- }
- }
- color[x]=black;
- fftime[x]=ttime;
- ttime++;
- }
- 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();
- cout<<"\n";
- for(int i=0;i<node;i++)
- {
- cout<<"node : " <<(char) ('A'+i)<<" "<<stime[i]<<" "<<fftime[i]<<endl;
- }
- cout<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement