#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Function to perform BFS traversal of the graph
void BFS( int start, const vector< vector< int >> & adjList, int n) {
vector< bool > visited( n, false ) ; // Track visited nodes
queue< int > q; // Queue for BFS
q.push ( start) ;
visited[ start] = true ;
while ( ! q.empty ( ) ) {
int node = q.front ( ) ; // Get the node from the front of the queue
q.pop ( ) ;
cout << node << " " ; // Print the node
// Visit all the adjacent nodes of the current node
for ( int neighbor : adjList[ node] ) {
if ( ! visited[ neighbor] ) {
visited[ neighbor] = true ;
q.push ( neighbor) ;
}
}
}
}
int main( ) {
int n, m;
cin >> n >> m; // Number of nodes and edges
vector< vector< int >> adjList( n) ; // Adjacency list for the graph
// Read the graph edges
for ( int i = 0 ; i < m; i++ ) {
int u, v;
cin >> u >> v;
adjList[ u] .push_back ( v) ; // Add v to the adjacency list of u
adjList[ v] .push_back ( u) ; // Add u to the adjacency list of v (for undirected graph)
}
int start;
cin >> start; // Input the starting node for BFS
BFS( start, adjList, n) ; // Perform BFS starting from the given node
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byBwZXJmb3JtIEJGUyB0cmF2ZXJzYWwgb2YgdGhlIGdyYXBoCnZvaWQgQkZTKGludCBzdGFydCwgY29uc3QgdmVjdG9yPHZlY3RvcjxpbnQ+PiYgYWRqTGlzdCwgaW50IG4pIHsKICAgIHZlY3Rvcjxib29sPiB2aXNpdGVkKG4sIGZhbHNlKTsgLy8gVHJhY2sgdmlzaXRlZCBub2RlcwogICAgcXVldWU8aW50PiBxOyAvLyBRdWV1ZSBmb3IgQkZTCiAgICBxLnB1c2goc3RhcnQpOwogICAgdmlzaXRlZFtzdGFydF0gPSB0cnVlOwoKICAgIHdoaWxlICghcS5lbXB0eSgpKSB7CiAgICAgICAgaW50IG5vZGUgPSBxLmZyb250KCk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