89942024-02-11 00:23:53xxxTestnevelés óracpp17Wrong answer 36/50226ms26288 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();
        }
	}

	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;
}

SubtaskSumTestVerdictTimeMemory
base36/50
1Accepted0/06ms11036 KiB
2Accepted0/06ms11284 KiB
3Wrong answer0/0184ms18672 KiB
4Accepted2/26ms11708 KiB
5Accepted3/36ms11860 KiB
6Wrong answer0/36ms12076 KiB
7Accepted3/36ms12156 KiB
8Accepted1/16ms12028 KiB
9Accepted3/36ms12156 KiB
10Wrong answer0/38ms12388 KiB
11Wrong answer0/38ms12472 KiB
12Accepted1/17ms12472 KiB
13Accepted2/27ms12560 KiB
14Accepted3/37ms12476 KiB
15Accepted1/1160ms17912 KiB
16Accepted3/3136ms22636 KiB
17Accepted5/559ms16408 KiB
18Wrong answer0/1226ms25132 KiB
19Accepted2/2162ms18616 KiB
20Accepted3/3189ms26084 KiB
21Accepted4/4180ms26240 KiB
22Wrong answer0/4178ms26288 KiB