-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFSStack.cpp
More file actions
70 lines (52 loc) · 1.11 KB
/
DFSStack.cpp
File metadata and controls
70 lines (52 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std;
//maximum 1000 nodes
vector<int>G[1000];
int D[1002]={0};
bool visited[1002]={false};
bool pushed[1002]={false};
void DFS(int src)
{
stack <int> S;
S.push(src);
pushed[src]=true;
while(!S.empty())
{
int a=S.top();
S.pop();
if(!visited[a])
{
visited[a]=true;
printf("Now visited : %d\n",a);
}
for(int i=G[a].size()-1;i>=0;i--)
{
int b=G[a][i];
if(!pushed[b])
{
S.push(b);
pushed[b]=true;
printf("Now pushed %d in the stack\n",b);
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
int nodes,edges;
int a,b;
scanf("%d%d",&nodes,&edges);
for(int i=0;i<edges;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
DFS(1);
// for(int i=1;i<=nodes;i++)
// {
// printf("d to %d = %d\n",1,i,D[i]);
// }
return 0;
}