Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin ("bfs.in");
- ofstream fout ("bfs.out");
- vector <int> graf[105];
- queue <int> sol;
- int viz[105];
- void bfs()
- {
- while ( sol.size() )
- {
- int nod = sol.front();
- fout<<nod<<" ";
- sol.pop();
- viz[nod] = 1;
- for ( auto x:graf[nod] )
- if ( viz[x] == 0 )
- {
- viz[x] = 1;
- sol.push(x);
- }
- }
- }
- int main()
- {
- int n, m, x;
- fin>>n>>m>>x;
- while ( m-- )
- {
- int a, b;
- fin>>a>>b;
- graf[a].push_back(b);
- graf[b].push_back(a);
- }
- sol.push(x);
- bfs();
- for ( int i = 1; i <= n; ++i )
- {
- if ( viz[i] == 0 )
- {
- sol.push(i);
- bfs();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement