179692025-09-24 17:59:18algoproTestnevelés óracpp17Accepted 50/50127ms13352 KiB
// UUID: 0c4c0248-212b-47fc-b167-407a3898e804
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k; cin >> n >> k;
	vector<vector<int>> vec(n);
	vector<int> beel(n);
	for(int i=0; i<k; i++)
	{
		int a,b; cin >> a >> b;a--;b--;
		vec[a].push_back(b);
		beel[b]++;
	}
	queue<int> q;
	for(int i=0; i<n; i++)
		if(beel[i]==0)
			q.push(i);
	vector<int> ki;
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		ki.push_back(x);
		for(auto& z:vec[x])
		{
			beel[z]--;
			if(beel[z]==0)
				q.push(z);
		}
	}
	for(auto& z:beel)
	{
		if(z!=0)
		{
			cout << 0;
			return 0;
		}
	}
	for(int i=0; i<ki.size()-1; i++)
	{
		bool b=true;
		for(auto& z:vec[ki[i]])
		{
			if(z==ki[i+1])
			{
				b=false;
				break;
			}
		}
		if(b)
		{
			cout << "2\n";
			for(int j=0; j<ki.size(); j++)
				cout << ki[j]+1 << " ";
			cout << "\n";
			for(int j=0; j<ki.size(); j++)
			{
				if(j==i)
				{
					cout << ki[j+1]+1 << " " << ki[j]+1 << " ";
					j++;
				}
				else
					cout << ki[j]+1 << " ";
			}
			return 0;
		}
	}
	cout << "1\n";
	for(auto& z:ki)
		cout << z+1 << " ";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms344 KiB
3Accepted0/098ms7340 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms316 KiB
8Accepted1/11ms364 KiB
9Accepted3/31ms316 KiB
10Accepted3/32ms316 KiB
11Accepted3/32ms316 KiB
12Accepted1/12ms316 KiB
13Accepted2/22ms316 KiB
14Accepted3/32ms316 KiB
15Accepted1/171ms4532 KiB
16Accepted3/383ms9892 KiB
17Accepted5/543ms10248 KiB
18Accepted1/1125ms13352 KiB
19Accepted2/270ms4684 KiB
20Accepted3/3127ms11700 KiB
21Accepted4/4108ms11704 KiB
22Accepted4/4100ms11804 KiB