-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDfs.cpp
More file actions
66 lines (52 loc) · 766 Bytes
/
Copy pathDfs.cpp
File metadata and controls
66 lines (52 loc) · 766 Bytes
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
#include<bits/stdc++.h>
#include<queue>
using namespace std;
vector<int> adj[202];
bool vis[102];
int edge_id[100];
void dfs(int s, int n)
{
for (int i = 0; i < n; ++i)
{
edge_id[i]=vis[i]=0;
}
stack<int>S;
S.push(s);
vis[s]=1;
while(!S.empty())
{
int u=S.top();
S.pop();
if(edge_id[u]==0)
{
cout<<u<<"\t ";
}
while(edge_id[u]<adj[u].size())
{
int v=adj[u][edge_id[u]];
edge_id[u]++;
if(vis[v]==0)
{
vis[v]=1;
S.push(u);
S.push(v);
break;
}
}
}
}
int main()
{
int n,Test,i,j,b,m,x,y,z,sum=0,s;
cin>>n>>m;
for(i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin>>s;
dfs(s,n);
return 0;
}