180372025-09-25 17:47:31algoproTestnevelés óracpp17Time limit exceeded 27/501.1s12596 KiB
// UUID: bb1a4162-c6cf-4810-a3c9-1650d193ef13
#include <bits/stdc++.h>
using namespace std;

bool nemLehet = false;
vector<vector<int>> graf;
vector<int> be;
vector<int> ki;
vector<int> topol;

void bejar(int csucs){
	be.push_back(csucs);
	for(int i = 0; i < graf[csucs].size(); i++){
		if(count(be.begin(), be.end(), graf[csucs][i]) == 0 && count(ki.begin(), ki.end(), graf[csucs][i]) == 0){
			bejar(graf[csucs][i]);
		}
		else if(count(be.begin(), be.end(), graf[csucs][i]) == 1 && count(ki.begin(), ki.end(), graf[csucs][i]) == 0){
			nemLehet = true;
		}
	}
	ki.push_back(csucs);
	topol.push_back(csucs);
}

int main() {
	int n, k;
	cin >> n >> k;
	graf.resize(n+1);
	for(int i = 0; i < k; i++){
		int a, b;
		cin >> a >> b;
		graf[a].push_back(b);
	}
	for(int i = 1; i <= n; i++){
		if(count(be.begin(), be.end(), i) == 0){
			bejar(i);
		}
	}
	if(nemLehet){
		cout << 0;
		return 0;
	}
	reverse(topol.begin(), topol.end());
	int csere = -1;
	for(int i = 0; i < n-1; i++){
		if(count(graf[topol[i]].begin(), graf[topol[i]].end(), topol[i+1]) == 0){
			csere = i;
		}
	}
	if(csere == -1){
		cout << 1 << endl;
		for(int i = 0; i < n; i++){
			cout << topol[i] << " ";
		}
	}
	else{
		cout << 2 << endl;
		for(int i = 0; i < n; i++){
			cout << topol[i] << " ";
		}
		cout << endl;
		for(int i = 0; i < n; i++){
			if(i == csere){
				cout << topol[i+1] << " " << topol[i] << " ";
			}
			else if(i != csere+1){
				cout << topol[i] << " ";
			}
		}
	}
}
SubtaskSumTestVerdictTimeMemory
base27/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Time limit exceeded0/01.075s5428 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms396 KiB
7Accepted3/31ms316 KiB
8Accepted1/11ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/37ms316 KiB
11Accepted3/38ms316 KiB
12Accepted1/18ms316 KiB
13Accepted2/28ms316 KiB
14Accepted3/34ms464 KiB
15Time limit exceeded0/11.1s3892 KiB
16Time limit exceeded0/31.1s8248 KiB
17Time limit exceeded0/51.1s5876 KiB
18Time limit exceeded0/11.077s9136 KiB
19Time limit exceeded0/21.075s4112 KiB
20Time limit exceeded0/31.1s12596 KiB
21Time limit exceeded0/41.1s12468 KiB
22Time limit exceeded0/41.088s12452 KiB