89922024-02-11 00:11:23xxxTestnevelés óracpp17Wrong answer 36/50224ms54916 KiB
#include <bits/stdc++.h>
using namespace std;

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

vector<vector<int> > adj(mxN);
int in[mxN];
bool vis[mxN], rev[mxN];
bool ex;


void dfs (int u) {
	if (ex) {
		return;
	}
	vis[u] = true;
	rev[u] = true;

	for(int x : adj[u]) {
		if (rev[x]) {
			cout << "0\n";
			ex = true;
			return;
		} else if (!vis[x]) {
			dfs(x);
		}
	}

	rev[u] = false;

}


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]++;
	}
	dfs(1);
	if (ex) {
        return 0;
	}

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

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

	while(!q.empty()) {
        int v = q.front();
        q.pop();
        int cnt = 0;
        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 (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/07ms11028 KiB
2Accepted0/06ms11288 KiB
3Wrong answer0/0188ms21208 KiB
4Accepted2/26ms13908 KiB
5Accepted3/37ms14156 KiB
6Wrong answer0/37ms14308 KiB
7Accepted3/37ms14528 KiB
8Accepted1/17ms14740 KiB
9Accepted3/36ms14836 KiB
10Wrong answer0/37ms14928 KiB
11Wrong answer0/38ms15268 KiB
12Accepted1/18ms15516 KiB
13Accepted2/28ms15556 KiB
14Accepted3/37ms15652 KiB
15Accepted1/1173ms23764 KiB
16Accepted3/3148ms30468 KiB
17Accepted5/559ms23676 KiB
18Wrong answer0/1224ms34672 KiB
19Accepted2/2172ms30668 KiB
20Accepted3/3206ms46740 KiB
21Accepted4/4201ms54916 KiB
22Wrong answer0/4200ms52980 KiB