44072023-03-27 17:39:09horvathabelTestnevelés óracpp17Wrong answer 48/50280ms112648 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<<-1<<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
base48/50
1Accepted0/035ms95668 KiB
2Accepted0/045ms95920 KiB
3Accepted0/0224ms104364 KiB
4Accepted2/243ms96448 KiB
5Accepted3/345ms96560 KiB
6Accepted3/343ms96788 KiB
7Accepted3/343ms97032 KiB
8Wrong answer0/145ms96896 KiB
9Accepted3/343ms96860 KiB
10Accepted3/346ms97292 KiB
11Accepted3/346ms97380 KiB
12Wrong answer0/146ms97252 KiB
13Accepted2/246ms97252 KiB
14Accepted3/345ms97252 KiB
15Accepted1/1197ms102708 KiB
16Accepted3/3184ms108832 KiB
17Accepted5/598ms104712 KiB
18Accepted1/1280ms111732 KiB
19Accepted2/2187ms102920 KiB
20Accepted3/3215ms112416 KiB
21Accepted4/4228ms112508 KiB
22Accepted4/4207ms112648 KiB