CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Snakeless path
Sender:louaha1
Submission time:2024-10-21 17:32:15 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:49:9: error: 'reverse' was not declared in this scope
   49 |         reverse(path.begin(), path.end());
      |         ^~~~~~~

Code

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    
    vector<vector<int>> adj(n + 1);  // adjacency list
    vector<bool> snake(n + 1, false);  // stores snake paths
    vector<int> parent(n + 1, -1);  // parent array for path reconstruction
    vector<bool> visited(n + 1, false);  // visited array
    
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    
    queue<int> q;
    q.push(1);
    visited[1] = true;
    
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        
        if (u == n) break;
        
        for (int v : adj[u]) {
            if (!visited[v]) {
                visited[v] = true;
                parent[v] = u;
                q.push(v);
            }
        }
    }
    
    if (!visited[n]) {
        cout << 0 << endl;
    } else {
        vector<int> path;
        for (int v = n; v != -1; v = parent[v]) {
            path.push_back(v);
        }
        reverse(path.begin(), path.end());
        
        cout << path.size() << endl;
        for (int v : path) {
            cout << v << " ";
        }
        cout << endl;
    }

    return 0;
}