180992025-09-29 17:29:21algoproTestnevelés óracpp17Wrong answer 35/50330ms32132 KiB
// UUID: fd5e6ab8-2d79-4a3f-817b-435c7c218b77
#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])) {
				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
base35/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted0/0330ms21164 KiB
4Accepted2/21ms316 KiB
5Wrong answer0/31ms316 KiB
6Wrong answer0/31ms336 KiB
7Wrong answer0/31ms508 KiB
8Accepted1/11ms348 KiB
9Wrong answer0/31ms316 KiB
10Accepted3/33ms564 KiB
11Accepted3/33ms564 KiB
12Accepted1/13ms756 KiB
13Accepted2/23ms564 KiB
14Accepted3/32ms564 KiB
15Accepted1/1291ms16216 KiB
16Wrong answer0/3168ms20604 KiB
17Accepted5/552ms19368 KiB
18Accepted1/1323ms32132 KiB
19Accepted2/2246ms16364 KiB
20Accepted3/3241ms25772 KiB
21Accepted4/4303ms25788 KiB
22Accepted4/4301ms25804 KiB