Submission details
Task:Course Schedule
Sender:aalto26fh_034
Submission time:2026-10-01 01:01:41 +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.20 sdetails
#7ACCEPTED0.20 sdetails
#8ACCEPTED0.20 sdetails
#9ACCEPTED0.20 sdetails
#10ACCEPTED0.18 sdetails
#11ACCEPTED0.18 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.14 sdetails
#15ACCEPTED0.14 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.16 sdetails

Compiler report

input/code.cpp: In function 'std::vector<long long int> indexSeq(Int)':
input/code.cpp:581:1: warning: no return statement in function returning non-void [-Wreturn-type]
  581 | }
      | ^
input/code.cpp: In function 'void courseOrdering()':
input/code.cpp:616:21: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'std::tuple_element<0, std::tuple<long long int, long long int> >::type' {aka 'long long int'} [-Wsign-compare]
  616 |     if (list.size() == n){
      |         ~~~~~~~~~~~~^~~~

Code

#include <iostream>
#include <vector>
#include <functional>
#include <set>
#include <algorithm>
#include <stack>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <limits>
#include <queue>

typedef long long Int;

std::tuple<Int> readInt1(){
    Int a;
    std::cin >> a;
    return {a};
}
std::tuple<Int,Int> readInt2(){
    Int a, b;
    std::cin >> a >> b;
    return {a,b};
}
std::tuple<Int,Int,Int> readInt3(){
    Int a, b, c;
    std::cin >> a >> b >> c;
    return {a,b,c};
}

std::vector<Int> readVecInt(Int n){
    std::vector<Int> arr(n);
    for (Int i = 0; i < n; i++){
        Int a;
        std::cin >> a;
        arr[i] = a;
    }
    return arr;
}
std::vector<std::string> readVecStr(Int n){
    std::vector<std::string> arr(n);
    for (Int i = 0; i < n; i++){
        std::string a;
        std::cin >> a;
        arr[i] = a;
    }
    return arr;
}

std::vector<long long> readVecLong(Int n){
    std::vector<long long> arr(n);
    for (Int i = 0; i < n; i++){
        long long a;
        std::cin >> a;
        arr[i] = a;
    }
    return arr;
}

std::vector<std::tuple<Int,Int>> readVecTup2(Int n){
    std::vector<std::tuple<Int,Int>> arr(n);
    for (Int i = 0; i < n; i++){
        Int a, b;
        std::cin >> a;
        std::cin >> b;
        arr[i] = {a,b};
    }
    return arr;
}

std::vector<std::tuple<Int,Int,Int>> readVecTup3(Int n){
    std::vector<std::tuple<Int,Int,Int>> arr(n);
    for (Int i = 0; i < n; i++){
        Int a, b, c;
        std::cin >> a;
        std::cin >> b;
        std::cin >> c;
        arr[i] = {a,b,c};
    }
    return arr;
}
std::vector<std::tuple<Int,Int,Int,Int>> readVecTup4(Int n){
    std::vector<std::tuple<Int,Int,Int,Int>> arr(n);
    for (Int i = 0; i < n; i++){
        Int a, b, c,d;
        std::cin >> a;
        std::cin >> b;
        std::cin >> c;
        std::cin >> d;
        arr[i] = {a,b,c,d};
    }
    return arr;
}

void printNewLine(){std::cout << "\n";}
template<typename T, typename... Ts>
std::tuple<Ts...> tail(std::tuple<T,Ts...> as){
    return std::apply([](T &a, Ts... b){return std::make_tuple(b...);},as);
}

template<typename T>
void print(T a){std::cout << std::setprecision(19) << a;}
template<typename... T>
void println(T... a);

template<typename T, typename Ts>
void print(std::tuple<T,Ts> a){
    print(std::get<0>(a));
    print(" ");
    print(std::get<1>(a));
}

template<typename T, typename... Ts>
void print(std::tuple<T,Ts...> a){
    print(std::get<0>(a));
    print(" ");
    print(tail(a));
}

template<typename T>
void print(const std::vector<T> &arr, Int width = -1){
    if (width == -1) width = arr.size();
    for (Int i = 0; i < (Int)arr.size(); i++){
        print(arr[i]);
        if (i+1 == (Int)arr.size()) continue;
        if ((i%width) == width-1) printNewLine();
        if ((i%width) != width-1) print(" ");
    }    
}
template<typename... T>
void println(T... a){print(a...);printNewLine();}



template<typename T>
void sorted(std::vector<T> &arr){
    std::sort(arr.begin(),arr.end(),[](const T &a, const T &b){ return a < b; });
}


template<typename T>
std::set<T> toSet(const std::vector<T> &vec){
    std::set<T> c;
    for (T a : vec){ c.emplace(a);}
    return c;
}

template<typename T, typename N>
std::vector<N> map(const std::vector<T> &a, const std::function<N(T)> &transform){
    std::vector<N> b(a.size());
    Int i = 0;
    for (T x : a){
        b[i] = transform(x);
        i++;
    }
    return b;
}

template<typename N, typename M, typename R>
std::vector<R> zip(const std::vector<N> &a, const  std::vector<M> &b, const std::function<R(N,M)> &transform){
    Int n = std::min(a.size(),b.size());
    std::vector<R> res(n);
    for (Int i = 0; i < n; i++){
        res[i] = transform(a[i],b[i]);
    }
    return res;
}

template<typename T, typename N>
std::vector<N> convertVec(const std::vector<T> &a){
    std::vector<N> b(a.size());
    Int i = 0;
    for (T x : a){
        b[i] = x;
        i++;
    }
    return b;
}
template<typename T>
std::vector<T> reverse(const std::vector<T> &a){
    std::vector<T> b(a.size());
    Int i = 0;
    for (T x : a){
        b[a.size()-1-i] = x;
        i++;
    }
    return b;
}



template<typename T, typename R>
R reduce(const std::vector<T> &arr, R init, const std::function<R(const R,const T)> &transform){
    R current = init;
    for (Int idx = 0; idx < (Int)arr.size(); idx++){
        current = transform(current,arr[idx]);
    };
    return current;
}
template<typename T>
T max(const std::vector<T> &arr){return reduce(arr, arr[0], static_cast<std::function<T(const T,const T)>>(static_cast<const T& (*)(const T&, const T&)>(std::max<T>)));}
template<typename T>
T min(const std::vector<T> &arr){return reduce(arr, arr[0], static_cast<std::function<T(const T,const T)>>(static_cast<const T& (*)(const T&, const T&)>(std::min<T>)));}

template<typename T>
std::function<T(const T,const T)> add(){return [](T a,T b){return a+b;};}
template<typename T>
std::function<T(const T,const T)> modAdd(T mod){return [mod](T a,T b){return (((a+b)%mod)+mod)%mod;};}
template<typename T, typename R>
std::function<R(const R,const T)> mulAdd(R mul){return [mul](R a,T b){return a+b*mul;};}
template<typename T>
T sum(const std::vector<T> &arr){return reduce(arr, 0, add<T>());}
template<typename T>
T avg(const std::vector<T> &arr){return reduce(arr, 0.0, mulAdd<double,T>(1.0/arr.size()));}

template<typename T>
T binarySearch(T min, T max, const std::function<bool(T)> &leq){
    if (min == max) return max;
    T guess = (min+max)/2;
    if (leq(guess)){
        return binarySearch(min,guess,leq);
    }else{
        return binarySearch(guess+1,max,leq);
    }
}

template<typename T>
T binarySearchDecending(T min, T max, const std::function<bool(T)> &leq){
    std::function<bool(T)> gt = [leq](T x){
        return !leq(x);
    };
    return binarySearch(min,max+1, gt)-1;
}

template<typename T>
std::vector<T> cloneVec(std::vector<T> a){return a;}

template<typename T>
std::vector<T> padding(const std::vector<T> &initial, T num, Int start, Int end){
    std::vector<T> arr(start+initial.size()+end);
    for (Int i = 0; i < start; i++){
        arr[i] = num;
    }
    for (Int i = 0; i < (Int)initial.size(); i++){
        arr[start+i] = initial[i];
    }
    for (Int i = 0; i < end; i++){
        arr[start+initial.size()+i] = num;
    }
    return arr;
}

template<typename T>
Int findMax(Int start, Int end, const std::function<T(Int)> &maxHeuristic){
    T initialGuess = maxHeuristic(start);
    Int index = start;
    for (Int i = start; i <= end; i++){
        if (initialGuess < maxHeuristic(i)){
            initialGuess = maxHeuristic(i);
            index = i;
        }
    }
    return index;
}

template<typename T>
void operateOnMax(Int start, Int end, const std::function<T(Int)> &maxHeuristic, const std::function<void(Int)> &found){
    found(findMax(start,end,maxHeuristic));
}

template<typename T>
void stackOperate(std::vector<T> init, const std::function<std::vector<T>(T)> operate){
    std::stack<T> stack;
    for (auto x : init){
        stack.push(x);
    }
    while (!stack.empty()){
        T next = stack.top();
        stack.pop();
        std::vector<T> nextSteps = operate(next);
        for (T a : nextSteps){
            stack.push(a);
        }
    }
}

template<typename T>
void queueOperate(std::vector<T> init, const std::function<std::vector<T>(T)> operate){
    std::queue<T> que;
    for (auto x : init){
        que.push(x);
    }
    while (!que.empty()){
        T next = que.front();
        que.pop();
        std::vector<T> nextSteps = operate(next);
        for (T a : nextSteps){
            que.push(a);
        }
    }
}

template<typename T>
void priorityQueueOperate(std::vector<T> init, const std::function<std::vector<T>(T)> operate){
    std::priority_queue<T> que;
    for (auto x : init){
        que.push(x);
    }
    while (!que.empty()){
        T next = que.top();
        que.pop();
        std::vector<T> nextSteps = operate(next);
        for (T a : nextSteps){
            que.push(a);
        }
    }
}

template<typename T>
void recursiveVectorSplit(Int start, Int end, Int distl, Int distr, const std::function<T(Int)> &maxHeuristic, const std::function<void(Int)> &found){
    if (start == end){
        return found(start);
    }else if (end < start){
        return;
    }
    std::tuple<Int,Int> range = {start,end};
    std::function<std::vector<std::tuple<Int,Int>>(std::tuple<Int,Int>)> splitter = [distl, distr,&maxHeuristic,&found](std::tuple<Int,Int> range){
        auto [start,end] = range;
        Int idx = findMax(start, end, maxHeuristic);
        found(idx);
        std::vector<std::tuple<Int,Int>> next;
        if (idx+distl+1 <= end){
            next.push_back({idx+distl+1,end});
        }
        if (start <= idx-distr-1){
            next.push_back({start, idx-distr-1});
        }
        return next;
    };
    stackOperate({range},splitter);
}


template<typename T>
std::vector<T> dynamicallyComputableList(Int n, std::function<T(std::vector<T>&,Int)> next){
    std::vector<T> arr(n);
    for (Int i = 0; i < n; i++){
        arr[i] = next(arr,i);
    }
    return arr;
}
template<typename T>
Int indexOfBinarySearch(const std::vector<T> &arr, const T &find){
    return binarySearch((Int)0,(Int)arr.size()-1,(std::function<bool(Int)>)[&arr, &find](Int guess){return find <= arr[guess];});
}

template<typename T>
std::unordered_map<T, Int> indexCompress(const std::vector<T> &arr){
    std::unordered_map<T, Int> compressed;
    Int i = 0;
    for (T x : arr){
        if (compressed.find(x) != compressed.end()) compressed[x] = i++;
    }
    return compressed;
}   

template<typename... Ts>
std::tuple<std::vector<std::tuple<Int,Ts...>>, std::vector<Int>> edgeIndexing(int n, std::vector<std::tuple<Int,Ts...>> edges){
    sorted(edges);
    std::vector<Int> edgeIndex(n+1);
    {
        Int j = 0;
        for (Int i = 0; i < (Int)edges.size(); i++) {
            auto a = std::get<0>(edges[i]);
            while (j <= a){
                edgeIndex[j++] = i;
            }
        }
        while (j < n){
            edgeIndex[j++] = edges.size();
        }
        edgeIndex[n] = edges.size(); 
    }
    return {edges,edgeIndex};
}

template<typename T>
std::tuple<std::vector<T>, std::vector<Int>> shortestPath(std::vector<std::tuple<Int, Int, T>> &edgesMessy, Int start){
    std::vector<std::tuple<Int, Int, T>> edges = edgesMessy;
    for (auto e : edgesMessy){
        auto [a,b,t] = e;
        edges.push_back({b,a,t});
    }
    shortestPathDirected(edges, start);
}

template<typename T>
std::tuple<std::vector<T>, std::vector<Int>> shortestPathDirected(const Int n, std::vector<std::tuple<Int, Int, T>> &edges, Int start){
    //assumes all paths are between 0 <= c < n
    //assumes 0 indexing
    std::vector<Int> path(n);
    std::vector<bool> edgeUpdated(n);
    std::vector<T> distance(n);
    for (Int i = 0; i < n; i++) path[i] = i;
    for (Int i = 0; i < n; i++) edgeUpdated[i] = true;
    edgeUpdated[start] = false;
    for (Int i = 0; i < n; i++) distance[i] = std::numeric_limits<T>::max()/4;
    distance[start] = 0;
    sorted(edges);
    std::vector<Int> edgeIndex(n+1);
    {
        Int j = 0;
        for (Int i = 0; i < (Int)edges.size(); i++) {
            auto& [a,b,w] = edges[i];
            while (j <= a){
                edgeIndex[j++] = i;
            }
        }
        while (j < n){
            edgeIndex[j++] = edges.size();
        }
        edgeIndex[n] = edges.size(); 
    }
    std::function<std::vector<std::tuple<Int,Int>>(std::tuple<Int,Int>)> func = [&distance,&edgeIndex,&edges,&path,&edgeUpdated](std::tuple<Int,Int> propagateW){
        auto& [weight,propagate] = propagateW;
        std::vector<std::tuple<Int,Int>> next;
        if (distance[propagate] != -weight) return next;
        if (edgeUpdated[propagate]) return next;
        for (Int i = edgeIndex[propagate]; i < edgeIndex[propagate+1]; i++){
            auto& [a, b, w] = edges[i];
            if (distance[a]+w < distance[b]){
                next.push_back({-(distance[a]+w),b});
                distance[b] = distance[a]+w;
                path[b] = a;
                edgeUpdated[b] = false;
            }
        }
        edgeUpdated[propagate] = true;
        return next;
    };
    priorityQueueOperate((std::vector<std::tuple<Int,Int>>){{0,start}},func);
    //for (Int i = 0; i < n; i++) {
    //    bool changed = false;
    //    for (auto& e : edges) {
    //        auto& [a, b, w] = e;
    //        if (!changed) {changed = (distance[b] != std::min(distance[b], distance[a]+w));}
    //        if (distance[a]+w < distance[b]){
    //            distance[b] = distance[a]+w;
    //            path[b] = a;
    //        }
    //    }
    //    if (!changed) break;
    //    if (changed && i == n){
    //        return std::tuple((std::vector<T>){},(std::vector<Int>){});
    //    }
    //}
    return std::tuple(distance,path);
}
std::vector<Int> pathFromPathMap(std::vector<Int> &map, Int goal){
    std::stack<Int> fullPath;
    fullPath.push(goal);
    while (map[fullPath.top()] != fullPath.top()){
        fullPath.push(map[fullPath.top()]);
    }
    std::vector<Int> p;
    while (!fullPath.empty()){
        p.push_back(fullPath.top());
        fullPath.pop();
    }
    return p;
}





template<typename T>
std::vector<T> distanceTable(const std::vector<T> &init, const  std::function<T(T,T)> &distance){
    Int n = init.size();
    std::vector<T> table(n*n);
    for (Int i = 0; i < n; i++){
        table[i+n*i] = init[i];
        for (Int j = i-1; j >= 0; j--){
            table[j+n*i] = distance(init[j],table[j+1+n*i]);
        }
        for (Int j = i+1; j < n; j++){
            table[j+n*i] = distance(table[j-1+n*i],init[j]);
        }
    }
    return table;
}

void particles(){
    auto [n] = readInt1();
    auto arr = readVecLong(n);
    std::vector<long long> table = distanceTable(arr,add<long long>());
    print(table,n);
    //println(cost[n*n-1]);
}
template<typename T>
std::vector<T> range(const std::vector<T> &a, Int from, Int to){
    std::vector<T> b;
    for (Int i = from; i < to; i++){
        b.push_back(a[i]);
    }
    return b;
}

template<typename T>
bool forall(const std::vector<T> &a, std::function<bool(T)> &test){
    bool res = true;
    for (auto v : a){
        res = res & test(v);
    }
    return res;
}
/*
std::vector<Int> dynamicUpdater(std::vector<std::tuple<Int,Int>> get, std::function<Int(std::unordered_map<std::tuple<Int,Int>, Int>&,std::tuple<Int,Int>)> eval, std::function<std::vector<std::tuple<Int,Int>>(std::tuple<Int,Int>)> needs){
    std::map<std::tuple<Int,Int>, Int> results;
    std::stack<std::tuple<Int,Int>> toDo;
    for (auto val : get){
        toDo.push(val);
    }
    std::function<bool(std::tuple<Int,Int>)> has = [&results](std::tuple<Int,Int> &value){
        return results.find(value) != results.end();
    };
    while (toDo.size() != 0){
        auto next = toDo.top();
        toDo.pop();
        if (results.find(next) != results.end()){

        } else if (forall(needs(next))){
            results[next] = eval(results,next);
        } else {
            for (auto val : needs(next)){
                toDo.push(val);
            }
        }
    }
    std::function<Int(std::tuple<Int,Int>)> resultMap = [&results](std::tuple<Int,Int> &value){
        return results[value];
    };
    return map(get,resultMap);
}*/
/*

template<typename T, typename R>
std::vector<R> dynamicUpdater(std::vector<T> get, std::function<R(std::unordered_map<T, R>&,T)> eval, std::function<std::vector<T>(T)> needs){
    std::map<T, R> results;
    std::stack<T> toDo;
    for (auto val : get){
        toDo.push(val);
    }
    std::function<bool(T)> has = [&results](T &value){
        return results.find(value) != results.end();
    };
    while (toDo.size() != 0){
        auto next = toDo.top();
        toDo.pop();
        if (results.find(next) != results.end()){

        } else if (forall(needs(next))){
            results[next] = eval(results,next);
        } else {
            for (auto val : needs(next)){
                toDo.push(val);
            }
        }
    }
    std::function<R(T)> resultMap = [&results](T &value){
        return results[value];
    };
    return map(get,resultMap);
}
*/
std::vector<Int> indexSeq(Int n){
    std::vector<Int> arr;
    for (Int i = 0; i < n; i++){
        arr.push_back(i);
    }
}

void shortestRoutes(){
    auto [n,m] = readInt2();
    auto arr = readVecTup3(m);
    auto [paths,route] = shortestPathDirected(n+1,arr,1);
    println(range(paths,1,paths.size()));
}

void courseOrdering(){
    auto [n,m] = readInt2();
    auto arr = readVecTup2(m);
    auto [edges,index] = edgeIndexing(n+1,arr);
    std::vector<Int> count(n+1,0);
    std::vector<bool> visited(n+1,false);
    for (auto q : edges){
        auto [a,b] = q;
        count[b] += 1;
    }
    std::queue<Int> next;
    for (Int i = 1; i <= n; i++){
        if (count[i] == 0) next.push(i);
    }
    std::vector<Int> list;
    while (!next.empty()){
        auto a = next.front();
        next.pop();
        if (visited[a]) continue;
        list.push_back(a);
        visited[a] = true;
        for (Int r = index[a]; r < index[a+1]; r++){
            Int b = std::get<1>(edges[r]);
            if ( 0 == --count[b]) next.push(b); 
        }
    }
    if (list.size() == n){
        println(list);
    } else {
        println("IMPOSSIBLE");
    }
}

int main() {
    //shortestRoutes();
    courseOrdering();
    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
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 4 6 1 2 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