Submission details
Task:Course Schedule
Sender:aalto25e_007
Submission time:2025-10-05 19:21:26 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.18 sdetails
#7ACCEPTED0.18 sdetails
#8ACCEPTED0.18 sdetails
#9ACCEPTED0.18 sdetails
#10ACCEPTED0.17 sdetails
#11ACCEPTED0.17 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.12 sdetails
#15ACCEPTED0.12 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.15 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:104:21: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  104 |     if(order.size() != n) return 0;
      |        ~~~~~~~~~~~~~^~~~

Code

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

void dijkstra(const vector<vector<pair<long long,long long>>> &adj, long long start, vector<long long> &dist) {
    long long n = adj.size();
    dist.assign(n, LLONG_MAX);
    vector<bool> processed(n, false);

    priority_queue<pair<long long,long long>, vector<pair<long long,long long>>, greater<pair<long long,long long>>> pq;
    dist[start] = 0;
    pq.push({0, start});

    while (!pq.empty()) {
        long long a = pq.top().second;
        pq.pop();
        if (processed[a]) continue;
        processed[a] = true;

        for (auto [b, w] : adj[a]) {
            if (dist[a] + w < dist[b]) {
                dist[b] = dist[a] + w;
                pq.push({dist[b], b});
            }
        }
    }
}

void topological_sort(const vector<vector<long long>> &adj, vector<long long> &order) {
    int n = adj.size();
    vector<long long> indegree(n, 0);

    for (int u = 0; u < n; u++) {
        for (int v : adj[u]) {
            indegree[v]++;
        }
    }

    queue<int> q;

    for (int i = 0; i < n; i++) {
        if (indegree[i] == 0) q.push(i);
    }

    order.reserve(n);

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


        for (int v : adj[u]) {
            indegree[v]--;
            if (indegree[v] == 0) q.push(v);
        }
    }


    if ((int)order.size() != n) {
        cout << "IMPOSSIBLE" << endl;
        return ;

    }


}


int main() {
    long long n, m;
    cin >> n >> m;
    /*vector<vector<pair<long long,long long>>> adj(n);

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

    vector<long long> dist;
    dijkstra(adj, 0, dist);

    for (long long i = 0; i < n; i++) {
       
        cout << dist[i] << " ";
    }*/
    vector<vector<long long>> adj(n);

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

    }

    vector<long long > order;
    topological_sort(adj, order);
    if(order.size() != n) return 0;
    for(size_t i = 0; i < order.size(); i++){
        cout << order[i] + 1 << " ";
    }

}

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
5 7 10 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
1 8 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
4 6 7 9 10 2 8 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
7 8 6 4 2 1 5 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
2 3 8 9 16 18 21 22 27 34 36 4...

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
9 11 13 16 22 35 37 38 40 44 5...

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
1 7 15 17 18 34 38 41 48 49 51...

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
6 10 11 16 17 19 21 22 23 28 3...

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
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

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