Submission details
Task:Course Schedule
Sender:aalto26fh_035
Submission time:2026-10-05 19:11:25 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.10 sdetails
#7ACCEPTED0.10 sdetails
#8ACCEPTED0.10 sdetails
#9ACCEPTED0.10 sdetails
#10ACCEPTED0.09 sdetails
#11ACCEPTED0.07 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.06 sdetails
#15ACCEPTED0.06 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.06 sdetails

Compiler report

input/code.cpp: In function 'void solve()':
input/code.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |             if (i < edges[a].size()) {
      |                 ~~^~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>
using namespace std;

// debug
template<class T> ostream& operator<<(ostream&, const vector<T>&);
template<class T> ostream& operator<<(ostream&, const set<T>&);
template<class T> ostream& operator<<(ostream&, const multiset<T>&);
template<class K, class V> ostream& operator<<(ostream&, const map<K, V>&);
template<class T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
template<class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << "(" << p.first << ", " << p.second << ")"; }
template<class T>
void print_collection(ostream& os, const T& v) {
    os << "{";
    bool first = true;
    for (const auto& x : v) {
        if (!first) os << ", ";
        first = false;
        os << x;
    }
    os << "}";
}
template<class T> ostream& operator<<(ostream& os, const vector<T>& v) { print_collection(os, v); return os; }
template<class T> ostream& operator<<(ostream& os, const set<T>& v) { print_collection(os, v); return os; }
template<class T> ostream& operator<<(ostream& os, const multiset<T>& v) { print_collection(os, v); return os; }
template<class K, class V> ostream& operator<<(ostream& os, const map<K, V>& v) { print_collection(os, v); return os; }
template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) { print_collection(os, v); return os; }
#define dbg(x) cerr << #x << " = " << (x) << '\n'

using ll = long long;
constexpr int INF = 1'000'000'000; // 1e9
constexpr ll LINF = 1'000'000'000'000'000'000LL; // 1e18
constexpr int MOD = 1'000'000'007; // 1e9 + 7

inline ll add(ll a, ll b) { return (a + b) % MOD; }
inline ll sub(ll a, ll b) { return ((a - b) % MOD + MOD) % MOD; }
inline ll mul(ll a, ll b) { return (a * b) % MOD; }


void solve() {
    int N, M;
    cin >> N >> M;

    vector<vector<int>> edges(N + 1);
    for (int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
    }

    vector<int> state(N + 1);
    vector<int> order;
    bool cycle = false;

    for (int start = 1; start <= N; ++start) {
        if (state[start] != 0)
            continue;

        stack<pair<int, int>> s;

        s.push({start, 0});
        state[start] = 1;

        while (!s.empty()) {
            int a = s.top().first;
            int &i = s.top().second;

            if (i < edges[a].size()) {
                int b = edges[a][i++];

                if (state[b] == 0) {
                    state[b] = 1;
                    s.push({b, 0});
                }
                else if (state[b] == 1) {
                    cycle = true;
                }
            }
            else {
                state[a] = 2;
                order.push_back(a);
                s.pop();
            }
        }
    }

    if (cycle) {
        cout << "IMPOSSIBLE\n";
    } else {
        reverse(order.begin(), order.end());
        for(auto o: order) cout << o << " ";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 20
5 2
2 4
8 9
6 4
...

correct output
5 7 10 2 1 8 3 9 6 4 

user output
10 7 5 2 1 8 3 9 6 4 

Test 2

Verdict: ACCEPTED

input
10 20
2 7
1 10
9 5
9 7
...

correct output
1 8 3 6 10 2 9 4 5 7 

user output
8 1 3 6 10 2 9 4 5 7 

Test 3

Verdict: ACCEPTED

input
10 20
8 5
2 3
10 1
9 1
...

correct output
4 6 7 9 10 2 8 3 1 5 

user output
9 7 10 6 8 4 2 3 1 5 

Test 4

Verdict: ACCEPTED

input
10 20
5 10
10 3
9 10
6 2
...

correct output
7 8 6 4 2 1 5 9 10 3 

user output
8 7 6 4 2 5 1 9 10 3 

Test 5

Verdict: ACCEPTED

input
10 20
2 9
4 8
9 1
10 6
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 6

Verdict: ACCEPTED

input
100000 200000
78359 8853
18190 30703
11401 30087
34627 11535
...

correct output
2 3 8 9 16 18 21 22 27 34 36 4...

user output
99998 99996 99994 99993 99992 ...

Test 7

Verdict: ACCEPTED

input
100000 200000
32395 2098
67067 31866
31867 67167
78488 33397
...

correct output
9 11 13 16 22 35 37 38 40 44 5...

user output
100000 99994 99991 99986 99983...

Test 8

Verdict: ACCEPTED

input
100000 200000
19035 36947
13730 46121
99449 77790
15626 11731
...

correct output
1 7 15 17 18 34 38 41 48 49 51...

user output
100000 99998 99996 99993 99992...

Test 9

Verdict: ACCEPTED

input
100000 200000
14188 9709
46541 20871
32203 88809
99879 54779
...

correct output
6 10 11 16 17 19 21 22 23 28 3...

user output
99996 99992 99991 99986 99985 ...

Test 10

Verdict: ACCEPTED

input
100000 200000
41882 61162
28138 18053
74649 74863
69760 74508
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 11

Verdict: ACCEPTED

input
100000 199998
1 100000
1 100000
2 100000
2 100000
...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
99999 99998 99997 99996 99995 ...

Test 12

Verdict: ACCEPTED

input
2 2
1 2
2 1

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 13

Verdict: ACCEPTED

input
6 6
1 2
2 3
4 3
4 5
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 14

Verdict: ACCEPTED

input
99999 149997
1 3
3 5
5 7
7 9
...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 15

Verdict: ACCEPTED

input
100000 149998
2 1
3 2
4 3
5 4
...

correct output
100000 99999 99998 99997 99996...

user output
100000 99999 99998 99997 99996...

Test 16

Verdict: ACCEPTED

input
6 6
1 2
1 3
2 4
3 5
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 17

Verdict: ACCEPTED

input
100000 200000
1 1
1 1
2 2
2 2
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE