We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5a5510d commit f0e9e7dCopy full SHA for f0e9e7d
BFS STL....3
@@ -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