181982025-10-13 16:53:20algoproTestnevelés óracpp17Time limit exceeded 41/501.1s21936 KiB
// UUID: c33e5532-d075-4369-bc1f-b50a0dc75a7e
#include <bits/stdc++.h>
using namespace std;

void melysegi(int i,vector<vector<int>> &matrix,vector<bool> &volt, vector<int> &sorrend, bool &ok) {
	for (int j = 0;j < matrix[i].size();j++) {
		if (!volt[matrix[i][j] - 1]) {
			volt[matrix[i][j] - 1] = true;
			melysegi(matrix[i][j] - 1,matrix,volt, sorrend, ok);
			sorrend.push_back(matrix[i][j]);
		}
		else {
			if (find(sorrend.begin(), sorrend.end(), matrix[i][j]) != sorrend.end()) {
			}
			else {
				cout << 0;
				ok = false;
			}
		}
	}
}
int main() {
	bool ok;ok = true;
	int n, k; cin >> n >> k;
	int temp,temp2;temp = 0,temp2 = 0;
	vector<vector<int>> matrix(n,vector<int>(0,0));
	for (int i = 0;i < k;i++) {
		cin >> temp >> temp2;
		matrix[temp - 1].push_back(temp2);
	}
	vector<int> sorrend(0,0);
	vector<bool> volt(n,false);
	for (int i = 0;i < n;i++) {
		if (volt[i] == false) {
			volt[i] = true;
			melysegi(i, matrix, volt, sorrend, ok);
			sorrend.push_back(i + 1);
		}
	}
	for (int i = 0;i < n - 1;i++) {
		if (!(find(matrix[sorrend[i + 1] - 1].begin(), matrix[sorrend[i + 1] - 1].end(), sorrend[i]) != matrix[sorrend[i + 1] - 1].end())) {
			cout << 2 << endl;
			for (int j = 0;j < n;j++) { 
				cout << sorrend[n - j - 1] << " ";
			}
			temp = sorrend[i + 1];
			sorrend[i + 1] = sorrend[i];
			sorrend[i] = temp;
			ok = false;
			cout << endl;
			break;
		}
	}
	if (ok) {
		cout << 1 << endl;
	}
	for (int i = 0;i < n;i++) {
		cout << sorrend[n - i - 1] << " ";
	}
}
SubtaskSumTestVerdictTimeMemory
base41/50
1Accepted0/01ms508 KiB
2Accepted0/01ms316 KiB
3Time limit exceeded0/01.09s5840 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms316 KiB
8Wrong answer0/11ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/33ms340 KiB
11Accepted3/33ms316 KiB
12Wrong answer0/13ms316 KiB
13Accepted2/23ms316 KiB
14Accepted3/32ms316 KiB
15Time limit exceeded0/11.1s4084 KiB
16Time limit exceeded0/31.1s8132 KiB
17Accepted5/557ms8560 KiB
18Time limit exceeded0/11.088s9420 KiB
19Time limit exceeded0/21.077s4148 KiB
20Accepted3/3226ms16900 KiB
21Accepted4/4237ms21936 KiB
22Accepted4/4194ms18864 KiB