89952024-02-11 00:27:42xxxTestnevelés óracpp17Elfogadva 50/50219ms25916 KiB
#include <bits/stdc++.h>
using namespace std;

const int mxN = 2 * (1e5 + 1);

vector<vector<int> > adj(mxN);
int in[mxN];


int main() {
	int n, m;

	cin >> n >> m;
	for(int i = 0; i < m; i++) {
		int x, y;
		cin >> x >> y;
		adj[x].push_back(y);
		in[y]++;
	}


	vector<int> top;
	queue<int> q;

	for(int i = 1; i <= n; i++) {
		if (in[i] == 0) {
			top.push_back(i);
			q.push(i);
		}
	}
	int ind, cntv = 0;
	bool flag = false;
	if(top.size() > 1) {
        flag = true;
        ind = 1;
	}

	while(!q.empty()) {
        int v = q.front();
        q.pop();
        int cnt = 0;
        cntv++;
        for(int u : adj[v]) {
            in[u]--;
            if (in[u] == 0) {
                q.push(u);
                top.push_back(u);
                cnt++;
            }
        }
        if (cnt >= 2) {
            flag = true;
            ind = top.size()-1;
        }
	}

	if (cntv < n) {
        cout << "0\n";
        return 0;
	}

    if (flag) {
        cout << "2\n";
        for(int x : top) {
            cout << x << ' ';
        }
        cout << endl;
        swap(top[ind], top[ind-1]);
        for(int x : top) {
            cout << x << ' ';
        }
        cout << endl;
    } else {
        cout << "1\n";
        for(int x : top) {
            cout << x << ' ';
        }
        cout << endl;
    }
	return 0;
}

RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/06ms11032 KiB
2Elfogadva0/06ms11296 KiB
3Elfogadva0/0179ms18424 KiB
4Elfogadva2/26ms11296 KiB
5Elfogadva3/36ms11552 KiB
6Elfogadva3/36ms11748 KiB
7Elfogadva3/36ms11964 KiB
8Elfogadva1/17ms12172 KiB
9Elfogadva3/36ms12432 KiB
10Elfogadva3/38ms12616 KiB
11Elfogadva3/38ms12600 KiB
12Elfogadva1/17ms12608 KiB
13Elfogadva2/27ms12580 KiB
14Elfogadva3/38ms12668 KiB
15Elfogadva1/1158ms18144 KiB
16Elfogadva3/3135ms22812 KiB
17Elfogadva5/559ms16140 KiB
18Elfogadva1/1219ms24544 KiB
19Elfogadva2/2160ms18496 KiB
20Elfogadva3/3172ms25792 KiB
21Elfogadva4/4181ms25916 KiB
22Elfogadva4/4180ms25912 KiB