44082023-03-27 17:39:27horvathabelTestnevelés óracpp17Accepted 50/50252ms112732 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> g[2000001];
int main() {
	int n, m;
	cin>>n>>m;
	vector<int> c;
	c.assign(n+1, 0);
	for (int i=0; i<m;i++){
		int x,y;
		cin>>x>>y;
		c[y]++; 
		g[x].push_back(y);
	}
	queue<int> q;
	for (int i=1;i<=n;i++){
		if (c[i]==0) q.push(i);
	}
	int cnt=0; 
	vector<int> ans;
	pair<int, int> ans2;
	bool van=false, volte=false; 
	while (!q.empty()){
		cnt++;
		if (q.size()>1 && !volte){
			ans2.first=q.front();
			van=true; 
			volte=true;
		}
		int v=q.front();
		ans.push_back(v);
		q.pop();
		if (van){
			ans2.second=q.front(); 
			van=false; 
		}
		for (int edge:g[v]){
			c[edge]-=1;
			if (c[edge]==0) q.push(edge);
		} 
	}
	if (cnt!=n){
		cout<<0<<endl;
		return 0; 
	}
	if (!volte){
		cout<<1<<endl;
		for (int i:ans) cout<<i<<" "; 
	}
	else{
		cout<<2<<endl;
		vector<int> mego; 
		for (int i=0; i<n;i++){
			cout<<ans[i]<<" ";
			if (ans[i]==ans2.first) mego.push_back(ans2.second);
			else{
				if (ans[i]==ans2.second) mego.push_back(ans2.first);
				else mego.push_back(ans[i]);
			}
		}
		cout<<endl;
		for (int i:mego) cout<<i<<" ";
	}
	
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/035ms95700 KiB
2Accepted0/045ms95952 KiB
3Accepted0/0211ms104040 KiB
4Accepted2/246ms96168 KiB
5Accepted3/337ms96420 KiB
6Accepted3/335ms96412 KiB
7Accepted3/335ms96632 KiB
8Accepted1/135ms96840 KiB
9Accepted3/345ms97060 KiB
10Accepted3/339ms97224 KiB
11Accepted3/337ms97444 KiB
12Accepted1/146ms97736 KiB
13Accepted2/246ms97948 KiB
14Accepted3/337ms97904 KiB
15Accepted1/1206ms103216 KiB
16Accepted3/3168ms109304 KiB
17Accepted5/5100ms105316 KiB
18Accepted1/1252ms112720 KiB
19Accepted2/2185ms103556 KiB
20Accepted3/3219ms112716 KiB
21Accepted4/4204ms112696 KiB
22Accepted4/4222ms112732 KiB