179952025-09-24 21:44:10algoproTestnevelés óracpp17Elfogadva 50/50142ms25772 KiB
// UUID: c07db662-6cda-434e-bd6f-23d02f7cded7
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n, m;
vector <vector <int>> g(3e5);
vector <bool> be(3e5, false);
vector <bool> ki(3e5, false);
vector <int> t;

void dfs(int a) {
	if (be[a] && !ki[a]) { cout << 0; exit(0); }
	if (be[a]) return;
	be[a] = true;
	for (int b : g[a]) dfs(b);
	ki[a] = true;
	t.push_back(a);
}

bool edge(int a, int b) {
	for (int x : g[a]) if (x == b) return true;
	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	int a, b;
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		g[a].push_back(b);
	}

	for (int i = 1; i <= n; i++) dfs(i);
	reverse(t.begin(), t.end());

	a = -1;
	for (int i = 0; i < n - 1; i++) if (!edge(t[i], t[i + 1])) a = i;

	cout << (a > -1 ? 2 : 1) << "\n";
	for (int x : t) cout << x << " ";
	if (a == -1) return 0;
	swap(t[a], t[a + 1]);
	cout << "\n";
	for (int x : t) cout << x << " ";
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/07ms7480 KiB
2Elfogadva0/08ms7476 KiB
3Elfogadva0/0104ms11688 KiB
4Elfogadva2/28ms7480 KiB
5Elfogadva3/38ms7304 KiB
6Elfogadva3/38ms7476 KiB
7Elfogadva3/36ms7476 KiB
8Elfogadva1/18ms7476 KiB
9Elfogadva3/37ms7476 KiB
10Elfogadva3/38ms7476 KiB
11Elfogadva3/38ms7388 KiB
12Elfogadva1/18ms7488 KiB
13Elfogadva2/28ms7580 KiB
14Elfogadva3/38ms7396 KiB
15Elfogadva1/175ms10112 KiB
16Elfogadva3/396ms13800 KiB
17Elfogadva5/543ms10924 KiB
18Elfogadva1/1119ms14756 KiB
19Elfogadva2/278ms10164 KiB
20Elfogadva3/3123ms20396 KiB
21Elfogadva4/4142ms25772 KiB
22Elfogadva4/4115ms22444 KiB