89952024-02-11 00:27:42xxxTestnevelés óracpp17Accepted 50/50219ms25916 KiB
#include <bits/stdc++.h>
using namespace std;

const int mxN = 2 * (1e5 + 1);

vector<vector<int> > adj(mxN);
int in[mxN];


int main() {
	int n, m;

	cin >> n >> m;
	for(int i = 0; i < m; i++) {
		int x, y;
		cin >> x >> y;
		adj[x].push_back(y);
		in[y]++;
	}


	vector<int> top;
	queue<int> q;

	for(int i = 1; i <= n; i++) {
		if (in[i] == 0) {
			top.push_back(i);
			q.push(i);
		}
	}
	int ind, cntv = 0;
	bool flag = false;
	if(top.size() > 1) {
        flag = true;
        ind = 1;
	}

	while(!q.empty()) {
        int v = q.front();
        q.pop();
        int cnt = 0;
        cntv++;
        for(int u : adj[v]) {
            in[u]--;
            if (in[u] == 0) {
                q.push(u);
                top.push_back(u);
                cnt++;
            }
        }
        if (cnt >= 2) {
            flag = true;
            ind = top.size()-1;
        }
	}

	if (cntv < n) {
        cout << "0\n";
        return 0;
	}

    if (flag) {
        cout << "2\n";
        for(int x : top) {
            cout << x << ' ';
        }
        cout << endl;
        swap(top[ind], top[ind-1]);
        for(int x : top) {
            cout << x << ' ';
        }
        cout << endl;
    } else {
        cout << "1\n";
        for(int x : top) {
            cout << x << ' ';
        }
        cout << endl;
    }
	return 0;
}

SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms11032 KiB
2Accepted0/06ms11296 KiB
3Accepted0/0179ms18424 KiB
4Accepted2/26ms11296 KiB
5Accepted3/36ms11552 KiB
6Accepted3/36ms11748 KiB
7Accepted3/36ms11964 KiB
8Accepted1/17ms12172 KiB
9Accepted3/36ms12432 KiB
10Accepted3/38ms12616 KiB
11Accepted3/38ms12600 KiB
12Accepted1/17ms12608 KiB
13Accepted2/27ms12580 KiB
14Accepted3/38ms12668 KiB
15Accepted1/1158ms18144 KiB
16Accepted3/3135ms22812 KiB
17Accepted5/559ms16140 KiB
18Accepted1/1219ms24544 KiB
19Accepted2/2160ms18496 KiB
20Accepted3/3172ms25792 KiB
21Accepted4/4181ms25916 KiB
22Accepted4/4180ms25912 KiB