-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph_Traversal_1.cpp
More file actions
76 lines (71 loc) · 1.36 KB
/
Copy pathGraph_Traversal_1.cpp
File metadata and controls
76 lines (71 loc) · 1.36 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
71
72
73
74
75
76
/*
PRIYANSHU IS A PEACEFULL SOUL
*/
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define ff first
#define ll long long
#define ss second
using namespace std;
bool vis[20];
vi adj[20];
void dfs(int node)
{
vis[node] = 1;
cout << node << " ";
vi::iterator it;
for (it = adj[node].begin(); it != adj[node].end(); it++)
{
if (!vis[*it])
{
dfs(*it);
}
}
}
int main()
{
for (int i = 0; i < 20; i++)
{
vis[i] = 0;
}
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
int p;
cin >> p;
queue<int> q;
q.push(p);
vis[p] = true;
while (!q.empty())
{
int node = q.front();
q.pop();
cout << node << " ";
vi::iterator it;
for (it = adj[node].begin(); it != adj[node].end(); it++)
{
if (!vis[*it])
{
vis[*it] = 1;
q.push(*it);
}
}
}
cout<<endl;
for (int i = 0; i < 20; i++)
{
vis[i] = 0;
}
dfs(p);
return 0;
}