179722025-09-24 18:08:02algoproTestnevelés óracpp17Accepted 50/50234ms20424 KiB
// UUID: 6c72c73a-c072-45ac-9d18-f82e79b02635
#include <bits/stdc++.h>
using namespace std;

vector<int> c;
vector<int> t;
bool z = true;

void topo(int i, vector<vector<int>> &a) {
	c[i] = 1;
	for (auto j : a[i]) {
		if (c[j] == 1) z = false;
		if (!c[j]) topo(j, a);
	}
	c[i] = 2;
	t.push_back(i);
}

int main() {
	int n, k; cin >> n >> k;
    vector<vector<int>> a (n+1);
	for (int i = 0; i < k; i++) {
		int x, y; cin >> x >> y;
		a[x].push_back(y);
	}
	c.resize(n+1);
	for (int i = 1; i <= n; i++) {
		if (!c[i]) {
			topo(i, a);
		}
		if (!z) {
			cout << 0 << endl;
			return 0;
		}
	}
	z = false;
	int w = 0;
	for (int i = n-1; i > 0; i--) {
		int f = t[i];
		int m = t[i-1];
		bool d = false;
		for (auto j : a[f]) {
			if (j == m) d = true;
		}
		if (!d) {
			z = true;
			w = i;
		}
	}
	if (!z) {
		cout << 1 << endl;
		for (int i = n-1; i >= 0; i--) {
			cout << t[i] << " ";
		}
	}
	else {
		cout << 2 << endl;
		for (int i = n-1; i >= 0; i--) {
			cout << t[i] << " ";
		}
		cout << endl;
		swap(t[w], t[w-1]);
		for (int i = n-1; i >= 0; i--) {
			cout << t[i] << " ";
		}
	}
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted0/0190ms7128 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms508 KiB
8Accepted1/11ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/33ms416 KiB
11Accepted3/33ms316 KiB
12Accepted1/12ms316 KiB
13Accepted2/23ms316 KiB
14Accepted3/32ms556 KiB
15Accepted1/1158ms4440 KiB
16Accepted3/3149ms10000 KiB
17Accepted5/559ms9196 KiB
18Accepted1/1234ms13224 KiB
19Accepted2/2157ms4644 KiB
20Accepted3/3190ms16376 KiB
21Accepted4/4223ms20424 KiB
22Accepted4/4202ms17832 KiB