179842025-09-24 18:22:40algoproTestnevelés óracpp17Accepted 50/50231ms19964 KiB
// UUID: 9a884aed-f60d-4a0c-a6bc-75ea601c61da
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> graph;
vector<bool> been;
vector<int> solution;

void dfs(int node){
	been[node]=true;
	for(int next:graph[node]){
		if(!been[next]) dfs(next);
	}
	solution.push_back(node);
}


int main() {
	int N,K;
	cin >> N >> K;
	graph.resize(N+1);

	for(int i=0; i<K; i++){
		int a,b;
		cin >> a >> b;
		graph[a].push_back(b);
	}

	been.resize(N+1,false);

	for(int i=1; i<=N; i++){
		if(!been[i]) dfs(i);
	}

	been.assign(N+1,false);
	bool check;
	bool AtLeastTwo=false;
	int SwapIndex=-1;
	int i=0;
	reverse(solution.begin(),solution.end());
	
	for(int place:solution){
		check=false;
		been[place]=true;
		for(int u:graph[place]){
			if(been[u]) {cout << 0; return 0;}
			if(i < N && u == solution[i+1]) {check=true;}
		}
		if(!check && i<N-1){AtLeastTwo=true; SwapIndex=i;}
		i++;
	}
	if(AtLeastTwo) cout << "2\n";
	else cout << "1\n";
	
	for(int n:solution) cout << n << " ";
	if (AtLeastTwo) {
		cout << "\n";
		swap(solution[SwapIndex], solution[SwapIndex + 1]);
		for(int n:solution) cout << n << " ";
	}
return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted0/0202ms6716 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms316 KiB
8Accepted1/11ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/33ms316 KiB
11Accepted3/33ms472 KiB
12Accepted1/13ms316 KiB
13Accepted2/23ms432 KiB
14Accepted3/32ms316 KiB
15Accepted1/1159ms4140 KiB
16Accepted3/3143ms9532 KiB
17Accepted5/557ms8616 KiB
18Accepted1/1231ms12320 KiB
19Accepted2/2159ms4528 KiB
20Accepted3/3203ms15672 KiB
21Accepted4/4202ms19964 KiB
22Accepted4/4196ms17328 KiB