106402024-04-07 15:39:50VargusTestnevelés óracpp17Time limit exceeded 16/501.069s82936 KiB
#include <iostream>
#include <queue>
#include <algorithm>
#define ll long long

using namespace std;

struct adat
{
    bool lat, kilep;
    ll ido = 0;
    vector <ll> sz, ut;
};
vector <adat> x;

bool kor = false;
deque <ll> q;
void melyseg(ll csp)
{
    x[csp].lat = true;
    for (auto& e : x[csp].sz)
    {
        x[e].ido += x[csp].ido + 1;
        if (x[e].lat && !x[e].kilep)
        {
            kor = true;
        }
        if (!x[e].lat)
        {
            melyseg(e);
        }
    }
    q.push_front(csp);
    x[csp].kilep = true;
}

int main()
{
    ll n, k;
    cin >> n >> k;
    x.resize(n + 1);
    for (ll i = 1; i <= k; ++i)
    {
        ll cs1, cs2;
        cin >> cs1 >> cs2;
        x[cs1].sz.push_back(cs2);
        x[cs2].ut.push_back(cs1);
    }
    ll db = 0;
    for (ll i = 1; i <= n; ++i)
    {
        if (!x[i].ut.size())
        {
            ++db;
            melyseg(i);
            if (kor)
            {
                cout << "0";
                return 0;
            }
        }
    }
    if (db == 1)
    {
        cout << "1\n";
        for (ll j = 0; j <= q.size() - 1; ++j)
            cout << q[j] << " ";
        return 0;
    }

    else if (db == 0)
    {
        cout << "0";
        return 0;
    }
    else if (db >= 2)
        cout << "2\n";
    {
        for (ll i = 0; i < q.size() - 1; ++i)
        {
            for (ll j = i + 1; j < q.size(); ++j)
            {
                if (x[q[i]].ido >= x[q[j]].ido)
                    swap(q[i], q[j]);
            }
        }
        for (ll j = 0; j <= q.size() - 1; ++j)
            cout << q[j] << " ";
        for (ll i = 1; i <= q.size() - 1; ++i)
        {
            if (x[q[i]].ido == x[q[i - 1]].ido)
            {
                swap(q[i], q[i - 1]);
                cout << "\n";
                for (ll j = 0; j <= q.size() - 1; ++j)
                    cout << q[j] << " ";
                break;
            }
        }
    }

}
/*
5 3
2 1
3 2
1 5

5 9
3 1
3 5
3 4
4 2
3 2
1 4
1 2
1 5
5 4

*/
SubtaskSumTestVerdictTimeMemory
base16/50
1Accepted0/03ms1816 KiB
2Accepted0/03ms2012 KiB
3Time limit exceeded0/01.054s17452 KiB
4Wrong answer0/23ms4740 KiB
5Accepted3/33ms4956 KiB
6Wrong answer0/33ms5072 KiB
7Accepted3/32ms5048 KiB
8Accepted1/13ms5056 KiB
9Accepted3/33ms5184 KiB
10Wrong answer0/34ms5532 KiB
11Wrong answer0/34ms5592 KiB
12Accepted1/14ms5608 KiB
13Accepted2/24ms5900 KiB
14Wrong answer0/37ms5840 KiB
15Accepted1/1196ms27180 KiB
16Time limit exceeded0/31.069s24540 KiB
17Time limit exceeded0/51.059s22896 KiB
18Time limit exceeded0/11.044s33364 KiB
19Accepted2/2193ms34552 KiB
20Wrong answer0/3228ms79268 KiB
21Wrong answer0/4229ms81104 KiB
22Wrong answer0/4226ms82936 KiB