Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct Comp{
- int v;
- vector<int> route;
- };
- int n, m;
- vector<int> link[100001];
- queue<Comp> q;
- bool visited[100001];
- int main(){
- //freopen("message.in", "r", stdin);
- cin >> n >> m;
- int a, b;
- for(int i=0; i<m; i++){
- cin >> a >> b;
- link[a].push_back(b);
- link[b].push_back(a);
- }
- vector<int> r;
- r.push_back(1);
- q.push({1, r});
- visited[1] = true;
- while(!q.empty()){
- Comp cur = q.front();
- q.pop();
- if(cur.v == n){
- cout << cur.route.size() << '\n';
- string p = "";
- //int ans = 1;
- for(int i=0; i<cur.route.size(); i++){
- //if(cur.route[i] == ' ') ans++;
- cout << p << cur.route[i];
- p = " ";
- }
- //cout << ans << '\n';
- //cout << cur.route << '\n';
- cout << '\n';
- return 0;
- }
- for(auto v: link[cur.v]){
- if(!visited[v]){
- visited[v] = true;
- cur.route.push_back(v);
- //cout << cur.route << '\n';
- q.push({v, cur.route});
- cur.route.pop_back();
- //!!!use vector because saves a lot of run time instead of string
- //visited[v] = false;
- //do not need to turn visited back because it will cause double check which is not needed
- }
- }
- }
- cout << "IMPOSSIBLE" << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement