Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Abdullah Al Nayem
- Leading University, Sylhet.
- */
- #include <bits/stdc++.h>
- #define pb push_back
- using namespace std;
- vector<bool> v;
- vector<vector<int> > g;
- void bfs(int startingNode)
- {
- queue<int> q;
- q.push(startingNode);
- v[startingNode] = true;
- while (!q.empty()) {
- int x = q.front();
- q.pop();
- cout << x << " ";
- for (int i = g[x][0]; i<g[x].size(); i++) {
- if (!v[i]) {
- q.push(i);
- v[i] = true;
- }
- }
- }
- }
- int main()
- {
- int n, e;
- cin >> n >> e;
- v.assign(n, false);
- g.assign(n, vector<int>());
- int a, b;
- for (int i = 0; i < e; i++) {
- cin >> a >> b;
- g[a].pb(b);
- g[b].pb(a);
- }
- for (int i = 0; i < n; i++) {
- if (!v[i])
- bfs(i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement