6122021-11-04 18:34:54goczbaliJárda-L (40)cpp14Runtime error 0/404ms2140 KiB
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    vector<vector<int>> graph(n);

    while (m--) {
        int a, b;
        cin >> a >> b;

        a--;
        b--;

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    queue<int> q;
    vector<int> d(n, -1);
    vector<bool> v(n);
    vector<int> p(n, -1);

    q.push(0);
    d[0] = 1;
    p[0] = 0;

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

        v[a] = true;

        for (int x : graph[a]) {
            if (!v[x]) {
                if (d[x] == -1) {
                    d[x] = d[a] + 1;
                }

                if (p[x] == -1) {
                    p[x] = a;
                }

                q.push(x);
            }
        }
    }

    if (d[n - 1] == -1) {
        cout << "IMPOSSIBLE" << endl;
    } else {
        cout << d[n - 1] << endl;

        vector<int> solution;

        int i = n - 1;
        while (i != p[i]) {
            solution.push_back(i + 1);
            i = p[i];
        }

        solution.push_back(i + 1);

        for (auto j = solution.end() - 1; j >= solution.begin(); j--) {
            cout << *j << " ";
        }

        cout << endl;
    }
}
SubtaskSumTestVerdictTimeMemory
base0/40
1Runtime error0/03ms1764 KiB
2Runtime error0/03ms1828 KiB
3Runtime error0/24ms2140 KiB
4Runtime error0/22ms1836 KiB
5Runtime error0/22ms1848 KiB
6Runtime error0/22ms1836 KiB
7Runtime error0/22ms1860 KiB
8Runtime error0/32ms1844 KiB
9Runtime error0/32ms1848 KiB
10Runtime error0/32ms1868 KiB
11Runtime error0/32ms1872 KiB
12Runtime error0/31ms1868 KiB
13Runtime error0/32ms1872 KiB
14Runtime error0/31ms1872 KiB
15Runtime error0/32ms1880 KiB
16Runtime error0/32ms1884 KiB
17Runtime error0/31ms1884 KiB