CCCPaste

7052e913af838

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e5 + 5;
int n, m;
vector<int> graph[maxn];

vector<int> bfs(int S, int E) {
queue<int> q;
q.push(S);
q.push(0);

vector<bool> visited(n + 1, false);
visited[S] = true;
vector<int> path(n, -1);

while(!q.empty()) {
int node = q.front();
q.pop();
int dist = q.front();
q.pop();



for(int neighbour : graph[node]) {
if(!visited[neighbour]) {
visited[neighbour] = true;
q.push(neighbour);
q.push(dist + 1);

path[neighbour] = node;
}
}

}
return path;
}
int main() {
ios_base::sync_with_stdio(false);

cin >> n >> m;

for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}

int S, E;
cin >> S >> E;

vector<int> p = bfs(S, E);

while(E != S) {
cout << E << " ";
E = p[E];
}
cout << S << endl;
return 0;
}