CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Distinct Routes
Sender:louaha1
Submission time:2024-10-21 17:24:53 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'bool bfs(int)':
input/code.cpp:29:13: error: 'reverse' was not declared in this scope
   29 |             reverse(path.begin(), path.end());
      |             ^~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:61:45: error: cannot convert 'std::vector<int>::iterator' to 'const char*'
   61 |             adj[p].erase(remove(adj[p].begin(), adj[p].end(), u), adj[p].end());
      |                                 ~~~~~~~~~~~~^~
      |                                             |
      |                                             std::vector<int>::iterator
In file included from /usr/include/c++/11/cstdio:42,
                 from /usr/include/c++/11/ext/string_conversions.h:43,
                 from /usr/include/c++/11/bits/basic_string.h:6608,
                 from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 f...

Code

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const int MAXN = 501;
vector<int> adj[MAXN];
vector<int> visited(MAXN, 0);
vector<int> parent(MAXN, -1);
vector<vector<int>> routes;

bool bfs(int n) {
    queue<int> q;
    q.push(1);
    visited.assign(MAXN, 0);
    parent.assign(MAXN, -1);
    visited[1] = 1;

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

        if (u == n) {
            vector<int> path;
            while (u != -1) {
                path.push_back(u);
                u = parent[u];
            }
            reverse(path.begin(), path.end());
            routes.push_back(path);
            return true;
        }

        for (int v : adj[u]) {
            if (!visited[v]) {
                visited[v] = 1;
                parent[v] = u;
                q.push(v);
            }
        }
    }
    return false;
}

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

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

    int days = 0;
    while (bfs(n)) {
        days++;
        int u = n;
        while (parent[u] != -1) {
            int p = parent[u];
            adj[p].erase(remove(adj[p].begin(), adj[p].end(), u), adj[p].end());
            u = p;
        }
    }

    cout << days << endl;
    for (const auto& route : routes) {
        cout << route.size() << endl;
        for (int room : route) {
            cout << room << " ";
        }
        cout << endl;
    }

    return 0;
}