Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int v;
- int adj[100][100];
- char color[100];
- stack<int> s;
- void visit_dfs(int u)
- {
- color[u]='g';
- for(int i=1;i<=v;i++)
- {
- if(adj[u][i]==1 && color[i]=='w')
- {
- visit_dfs(i);
- }
- }
- color[u]='b';
- s.push(u);
- }
- void dfs()
- {
- for(int i=1;i<=v;i++)
- {
- color[i]='w';
- }
- for(int i=1;i<=v;i++)
- {
- if(color[i]=='w')
- {
- visit_dfs(i);
- }
- }
- }
- int main()
- {
- int a,b;
- cout<<" Enter number of vartex : ";
- cin>>v;
- while(cin>>a>>b && a!=0 && b!=0)
- {
- adj[a][b]=1;
- }
- dfs();
- while(!s.empty())
- {
- cout<<s.top()<<" ";
- s.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement