95642024-02-23 09:44:22csaron71Testnevelés óracpp17Accepted 50/50228ms27536 KiB
#include <bits/stdc++.h>
#include <queue>
using namespace std;

int main() {
	int n, k;
	cin >> n >> k;

	vector<int> elotte(n, 0);
	vector<vector<int> > utana(n, vector<int>());
	for (int i=0; i<k; i++) {
		int x, y;
		cin >> x >> y;
		x--;
		y--;
		elotte[y]++;
		utana[x].push_back(y);
	}

	queue<int> nullak;
	for (int i=0; i<n; i++) {
		if (elotte[i]==0) {
			nullak.push(i);
		}
	}

	vector<int> vege;
	bool tobb=false;
	int elso, masodik;
	while (nullak.size()>0) {
		int epp=nullak.front();
		nullak.pop();
		if (nullak.size()>0) {
			tobb=true;
			elso=epp;
			masodik=nullak.front();
		}
		vege.push_back(epp);
		for (int sz : utana[epp]) {
			elotte[sz]--;
			if (elotte[sz]==0) {
				nullak.push(sz);
			}
		}
	}

	if (vege.size()!=n) {
		cout << "0\n";
	}
	else if (tobb==false) {
		cout << "1\n";
		for (int sz : vege) {
			cout << sz+1 << " ";
		}
	}
	else {
		cout << "2\n";
		for (int sz : vege) {
			cout << sz+1 << " ";
		}
		cout << "\n";
		for (int i=0; i<n; i++) {
			if (vege[i]==elso) {
				cout << masodik+1 << " " << elso+1 << " ";
				i++;
			}
			else {
				cout << vege[i]+1 << " ";
			}
		}
	}
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1812 KiB
2Accepted0/03ms2024 KiB
3Accepted0/0184ms16356 KiB
4Accepted2/23ms4740 KiB
5Accepted3/33ms4828 KiB
6Accepted3/33ms4860 KiB
7Accepted3/33ms4960 KiB
8Accepted1/13ms4936 KiB
9Accepted3/33ms4948 KiB
10Accepted3/34ms5424 KiB
11Accepted3/34ms5504 KiB
12Accepted1/14ms5632 KiB
13Accepted2/24ms5760 KiB
14Accepted3/33ms5672 KiB
15Accepted1/1155ms13680 KiB
16Accepted3/3138ms22072 KiB
17Accepted5/561ms20712 KiB
18Accepted1/1228ms27536 KiB
19Accepted2/2151ms13980 KiB
20Accepted3/3179ms25784 KiB
21Accepted4/4182ms25772 KiB
22Accepted4/4179ms25892 KiB