Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
1.3 kB
4
Indexable
Never
#include <bits/stdc++.h>
 
using namespace std;
 
void File() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
#ifdef MON
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("errors.txt", "w", stderr);
#else
#endif
}
 
int main() {
    File();
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n + 1);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    queue<int> q;
    vector<bool> vis(n + 1, false);
    vector<int> par(n + 1);
    q.push(1);
    vis[1] = true, par[1] = -1;
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(auto &v: adj[u]) {
            if(!vis[v]) {
                vis[v] = true;
                par[v] = u;
                q.push(v);
            }
        }
    }
    if(!vis[n]) cout << "IMPOSSIBLE";
    else {
        int x = n;
        vector<int> path;
        while(x != -1) {
            path.push_back(x);
            x = par[x];
        }
        cout << path.size() << "\n";
        reverse(path.begin(), path.end());
        for(auto &x: path) cout << x << ' ';
    }
    return 0;
}
Leave a Comment