179962025-09-24 21:44:49zsomborTestnevelés óracpp17Accepted 50/50143ms25608 KiB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n, m;
vector <vector <int>> g(3e5);
vector <bool> be(3e5, false);
vector <bool> ki(3e5, false);
vector <int> t;

void dfs(int a) {
	if (be[a] && !ki[a]) { cout << 0; exit(0); }
	if (be[a]) return;
	be[a] = true;
	for (int b : g[a]) dfs(b);
	ki[a] = true;
	t.push_back(a);
}

bool edge(int a, int b) {
	for (int x : g[a]) if (x == b) return true;
	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	int a, b;
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		g[a].push_back(b);
	}

	for (int i = 1; i <= n; i++) dfs(i);
	reverse(t.begin(), t.end());

	a = -1;
	for (int i = 0; i < n - 1; i++) if (!edge(t[i], t[i + 1])) a = i;

	cout << (a > -1 ? 2 : 1) << "\n";
	for (int x : t) cout << x << " ";
	if (a == -1) return 0;
	swap(t[a], t[a + 1]);
	cout << "\n";
	for (int x : t) cout << x << " ";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/07ms7476 KiB
2Accepted0/07ms7476 KiB
3Accepted0/0115ms11492 KiB
4Accepted2/28ms7476 KiB
5Accepted3/38ms7716 KiB
6Accepted3/37ms7412 KiB
7Accepted3/36ms7532 KiB
8Accepted1/17ms7476 KiB
9Accepted3/37ms7476 KiB
10Accepted3/38ms7668 KiB
11Accepted3/37ms7468 KiB
12Accepted1/17ms7432 KiB
13Accepted2/28ms7476 KiB
14Accepted3/37ms7476 KiB
15Accepted1/174ms10308 KiB
16Accepted3/393ms14008 KiB
17Accepted5/543ms10920 KiB
18Accepted1/1118ms14756 KiB
19Accepted2/279ms10164 KiB
20Accepted3/3120ms20432 KiB
21Accepted4/4143ms25608 KiB
22Accepted4/4115ms22472 KiB