-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
39 lines (37 loc) · 734 Bytes
/
DFS.cpp
File metadata and controls
39 lines (37 loc) · 734 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
#include <bits/stdc++.h>
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;
}