179142025-09-22 18:41:42algoproTestnevelés óracpp17Elfogadva 50/50128ms18584 KiB
// UUID: 3882144c-c3a1-4a79-85d6-0cdb19ca6437
// Topologikus sorrend

#include <bits/stdc++.h>
using namespace std;

// declarations
// variables
using ll = long long;
using ld = long double;  // or double, if TL is tight
using str = string;      // yay python!

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define f first
#define s second

#define tcT template <class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT > using V = vector<T>;
tcT, size_t SZ > using AR = array<T, SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
using v2ds = V<V<str>>;
using v2di = V<vi>;
using v2dc = V<V<char>>;

// functions
#define loop(i,a,b) for(int i = (a); i < (b); i++)
#define loopbw(i,a,b) for(int i = (a) - 1; i >= (b); i--)
#define each(a, x) for (auto &a : x)
#define imeach(a,x) for(const auto &a : x)

#define sz(x) int(size(x))
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()

// ----------------------------------------------------------------------

vb volt;
vb fin;
v2di graf;
vi res;

bool impossible = false;

void dfs(int u){

	if(fin[u]) {
		return;
	}

	volt[u] = true;

	each(v,graf[u]){
		if(!volt[v]){
			dfs(v);
		}
		else{
			impossible = true;
			return;
		}
	}

	volt[u] = false;
	res.pb(u);
	fin[u] = true;
}

void solve() {
 
	int n, k;
	cin >> n >> k;

	graf.resize(n+1);
	volt.resize(n+1);
	fin.resize(n+1);

	loop(i,0,k){
		int a,b; cin >> a >> b;
		graf[b].pb(a);
	}

	loop(i,1,n+1){
		if(!volt[i]){
			dfs(i);
		}
	}

	if(impossible) {
		cout << "0";
		return;
	}

	bool nemjooo = false;

	for(int i = 0; i < res.size()-1; i++){

		bool joo = false;
		each(x, graf[res[i+1]]){
			if(x == res[i]){
				joo = true;
				break;
			}
		}

		if(joo) continue;

		if(!joo){
			nemjooo = true;
			cout << "2\n";

			loop(j,0,res.size()){
				if(j == i){
					cout << res[j] << " " << res[j+1] << " ";
					j++;
				}
				else cout << res[j] << " ";
			}
			cout << '\n';
			loop(j,0,res.size()){
				if(j == i){
					cout << res[j+1] << " " << res[j] << " ";
					j++;
				}
				else cout << res[j] << " ";
			}
			break;
		}

	}
	if(!nemjooo){
		cout << "1\n";
		each(x,res){
			cout << x << " ";
		}
	}
	

	// ide ez ele
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
    cout.tie(nullptr);
 
	int t = 1; //cin >> t;
	while(t--) solve();
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms508 KiB
2Elfogadva0/01ms316 KiB
3Elfogadva0/0108ms6836 KiB
4Elfogadva2/21ms320 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva3/31ms316 KiB
8Elfogadva1/11ms316 KiB
9Elfogadva3/31ms316 KiB
10Elfogadva3/32ms316 KiB
11Elfogadva3/32ms316 KiB
12Elfogadva1/12ms316 KiB
13Elfogadva2/22ms388 KiB
14Elfogadva3/32ms384 KiB
15Elfogadva1/181ms4384 KiB
16Elfogadva3/3101ms8252 KiB
17Elfogadva5/541ms8616 KiB
18Elfogadva1/1128ms12016 KiB
19Elfogadva2/274ms4528 KiB
20Elfogadva3/3118ms17072 KiB
21Elfogadva4/4125ms18584 KiB
22Elfogadva4/4126ms15904 KiB