-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDFS.cpp
More file actions
52 lines (50 loc) · 906 Bytes
/
DFS.cpp
File metadata and controls
52 lines (50 loc) · 906 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
#include<cstdio>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<list>
#include<algorithm>
#include<utility>
using namespace std;
vector<int>v[100];
int visited[100];
int DFS(int src)
{
int i,j;
visited[src]=1;
printf("%d ",src);
for(i=0;i<v[src].size();i++)
{
j=v[src][i];
if(visited[j]==0)
{
DFS(j);
}
}
return 0;
}
int main()
{
int i,j,node,edge,node1,node2,source;
while(scanf("%d %d",&node,&edge)!=EOF)
{
if(node==0 && edge==0)
break;
memset(visited,0,100);
for(i=1;i<=edge;i++)
{
scanf("%d %d",&node1,&node2);
v[node1].push_back(node2);
}
scanf("%d",&source);
DFS(source);
}
return 0;
}