CSES - Aalto Competitive Programming 2024 - wk6 - Homework - Results
Submission details
Task:Course Schedule
Sender:joaquimballester
Submission time:2024-10-06 14:47:54 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:44:23: error: 'topSort' was not declared in this scope; did you mean 'TopSort'?
   44 |     vector<int> sol = topSort(n, graph, degree);
      |                       ^~~~~~~
      |                       TopSort
input/code.cpp:46:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |     if (sol.size() != n) {
      |         ~~~~~~~~~~~^~~~
input/code.cpp:50:17: error: expected ';' before 'i'
   50 |         for (sol i = 0; i < n; i++)
      |                 ^~
      |                 ;
input/code.cpp:50:14: warning: statement has no effect [-Wunused-value]
   50 |         for (sol i = 0; i < n; i++)
      |              ^~~
input/code.cpp:50:25: error: 'i' was not declared in this scope
   50 |         for (sol i = 0; i < n; i++)
      |                         ^
input/code.cpp:51:21: error: 'ans' was not declared in th...

Code

#include<iostream>
#include<vector>
#include<limits>
#include<queue>
#include <utility>
#include <functional>
using namespace std;


vector<int> TopSort(int n, const vector<vector<int> >& adj,
                  vector<int>& degree)
{
    queue<int> q;
    vector<int> ans;
    for (int i = 1; i <= n; ++i)
        if (degree[i] == 0)
            q.push(i);
    while (!q.empty()) {
        int node = q.front();
        q.pop();
        ans.push_back(node);
        for (auto aux : adj[node]) {
            degree[aux] -= 1;
            if (degree[aux] == 0) {
                q.push(aux);
            }
        }
    }
    return ans;
}
int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int> > graph(n + 1);
    vector<int> degree(n + 1, 0);
    for (int i = 0; i < m; ++i) {
        int u,v;
        cin>>u>>v;
        graph[u].push_back(v);
        degree[v] += 1;
    }

    vector<int> sol = topSort(n, graph, degree);

    if (sol.size() != n) {
        cout << "IMPOSSIBLE";
    }
    else {
        for (sol i = 0; i < n; i++)
            cout << ans[i] << " ";
    }
    cout<<endl;
}