-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsq4-B.cpp
More file actions
66 lines (59 loc) · 952 Bytes
/
Copy pathsq4-B.cpp
File metadata and controls
66 lines (59 loc) · 952 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
/* Graph - STL */
#include <bits/stdc++.h>
using namespace std;
int n, *vis;
list<int> *graph;
void bfs(int);
void dfs(int);
int main()
{
freopen("in", "r", stdin);
freopen("out1", "w", stdout);
int ni, e;
cin>>ni>>e;
n = ni;
vis = new int[n];
for(int i=0; i<n; i++) vis[i] = 0;
graph = new list<int>[n];
while(e--)
{
int u, v;
cin>>u>>v;
graph[u].push_back(v);
}
cout<<"\n\nBFS:\n\n\n";
bfs(0);
cout<<"\n\nDFS:\n\n\n";
dfs(0);
}
void bfs(int u)
{
int visited[n];
for(int i=0; i<n; i++) visited[i] = 0;
queue<int> q;
q.push(u);
visited[u] = 1;
while(!q.empty())
{
u = q.front();
cout<<u<<endl;
q.pop();
for(list<int>::iterator it=graph[u].begin(); it!=graph[u].end(); it++)
{
if(!visited[*it])
{
q.push(*it);
visited[*it] = 1;
}
}
}
}
void dfs(int u)
{
vis[u] = 1;
cout<<u<<endl;
for(list<int>::iterator it=graph[u].begin(); it!=graph[u].end(); it++)
{
if(!vis[*it]) dfs(*it);
}
}