Skip to content

Commit f0e9e7d

Browse files
authored
Create BFS STL....3
1 parent 5a5510d commit f0e9e7d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

BFS STL....3

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//bfs code 3
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
const int N = (int)1e5 + 5;
6+
vector<int>g[N];
7+
bool visit[N];
8+
int main()
9+
{
10+
int m, n, i;
11+
cin >> m >> n;
12+
for (i = 0; i < n; i++)
13+
{
14+
int u, v;
15+
cin >> u >> v;
16+
g[u].push_back(v);
17+
g[v].push_back(u);
18+
19+
}
20+
queue<int>q;
21+
q.push(1);
22+
while (q.size())
23+
{
24+
int u = q.front();
25+
if(!visit[u])
26+
cout << u << " ";
27+
q.pop();
28+
visit[u] = true;
29+
for (i = 0; i < g[u].size(); i++)
30+
{
31+
int v = g[u][i];
32+
if (!visit[v])
33+
{
34+
q.push(v);
35+
}
36+
}
37+
38+
}
39+
return 0;
40+
}

0 commit comments

Comments
 (0)