181012025-09-29 18:30:14algoproTestnevelés óracpp17Accepted 50/50319ms32036 KiB
// UUID: ef89d4aa-f839-4cae-9176-29cb416f6f6a
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> gr;
vector<set<int>> grg;

int main() {
	int n, k;
	cin >> n >> k;
	gr.resize(n + 1);
	grg.resize(n + 1);
	vector<int> befok(n + 1);
	befok[0] = 3;
	for (int i = 1; i <= k; i++) {
		int a, b;
		cin >> a >> b;
		gr[a].push_back(b);
		grg[a].insert(b);
		befok[b]++;
	}
	queue<int> q;
	vector<int> ans;
	for (int i = 1; i <= n; i++) {
		if (!befok[i]) {
			q.push(i);
			ans.push_back(i);
		}
	}
	while (!q.empty()) {
		int a = q.front();
		q.pop();
		for (int i : gr[a]) {
			if (befok[i] == 1) {
				q.push(i);
				ans.push_back(i);
			}
			befok[i]--;
		}
	}
	if (ans.size() == n) {
		int x = -1;
		for (int i = 0; i < n - 1; i++) {
			if (grg[ans[i]].find(ans[i + 1])==grg[ans[i]].end()) {
				x = i;
			}
		}
		if (x != -1) {
			cout << "2\n";
			for (int i = 0; i < n; i++) {
				cout << ans[i] << ' ';
			}
			cout << '\n';
			for (int i = 0; i < n; i++) {
				if (i == x) {
					cout << ans[i + 1] << ' ';
				}
				else if (i == x + 1) {
					cout << ans[i - 1] << ' ';
				}
				else {
					cout << ans[i] << ' ';
				}
			}
		}
		else {
			cout << "1\n";
			for (int i = 0; i < n; i++) {
				cout << ans[i] << ' ';
			}
		}
	}
	else {
		cout << 0;
	}
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted0/0319ms21164 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms500 KiB
7Accepted3/31ms316 KiB
8Accepted1/11ms316 KiB
9Accepted3/31ms508 KiB
10Accepted3/33ms568 KiB
11Accepted3/33ms564 KiB
12Accepted1/13ms564 KiB
13Accepted2/23ms564 KiB
14Accepted3/32ms564 KiB
15Accepted1/1238ms16228 KiB
16Accepted3/3216ms21192 KiB
17Accepted5/552ms19476 KiB
18Accepted1/1317ms32036 KiB
19Accepted2/2232ms16308 KiB
20Accepted3/3284ms25772 KiB
21Accepted4/4284ms25772 KiB
22Accepted4/4223ms25772 KiB