-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
44 lines (44 loc) · 780 Bytes
/
Graph.cpp
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
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool visited[N];
vector<int> adj[N];
queue<int> q;
void bfs(int source)
{
visited[source]=1;
q.push(source);
while(!q.empty())
{
int u = q.front();
q.pop();
cout<< u << endl;
for(auto x : adj[u])
{
if(visited[x]==0)
{
visited[x]=1;
q.push(x);
}
}
}
}
int main()
{
int v,e;
cin >> v >> e ;
for(int i=0;i < e; i++)
{
int x,y ;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
// for(int i=0;i < e;i++)
// {
// for(auto x : adj[i])
// cout<< x << " ";
// cout<<endl;
// }
bfs(3);
}