47562023-03-31 11:06:19Erik_GepardTestnevelés óracpp17Accepted 50/50238ms38800 KiB
#include <bits/stdc++.h>
using namespace std;

vector <int> Graf[200011];
vector<bool> bejart;
vector<int> ans;
int m, n;
int eleres[200011];

void dfs(int v) {
  bejart[v] = true;
  for (int u : Graf[v]) {
    if (!bejart[u])
      dfs(u);
  }
  ans.push_back(v);
}

void topological_sort() {
  bejart.assign(n+1, false);
  ans.clear();
  for (int i = n; i > 0; i--) {
    if (!bejart[i])
      dfs(i);
    }
  reverse(ans.begin(), ans.end());
  for(int i=0; i<ans.size(); i++) {
    eleres[ans[i]]=i;
  }
}

int main() {
	cin>>n>>m;
  for (int i=1; i<=m; i++) {
    int a, b;
    cin>>a>>b;
    Graf[a].push_back(b);
  }
  topological_sort();
  for(int i=1; i<=n; i++) {
    for(int sz: Graf[i]) {
      if(eleres[i]>eleres[sz]) {
        cout<<0<<"\n";
        return 0;
      }
    }
  }
  int csere=-1;
  for(int i=1; i<n; i++){
    int a=ans[i-1], b=ans[i], lehet=1;
    for(int x : Graf[a]){
        if(x==b){
            lehet=0;
        }
    }
    if(lehet){
        csere=i-1;
    }
  }
  if(csere==-1){
    cout<<1<<"\n";
    for (int i=0; i<ans.size(); i++) {
        cout<<ans[i]<<" ";
    }
  }
  else{
    cout<<2<<"\n";
    for (int i=0; i<ans.size(); i++) {
        cout<<ans[i]<<" ";
    }
    cout<<"\n";
    cerr<<csere;
    for (int i=0; i<ans.size(); i++) {
        if(i==csere){
            cout<<ans[i+1];
        }
        else if(i==csere+1){
            cout<<ans[i-1];
        }
        else{
            cout<<ans[i];
        }
        cout<<" ";
    }
    cout<<"\n";
  }
  return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms11304 KiB
2Accepted0/07ms11580 KiB
3Accepted0/0186ms18380 KiB
4Accepted2/26ms11956 KiB
5Accepted3/36ms12196 KiB
6Accepted3/36ms12464 KiB
7Accepted3/37ms12620 KiB
8Accepted1/16ms12824 KiB
9Accepted3/36ms13108 KiB
10Accepted3/38ms13164 KiB
11Accepted3/38ms13268 KiB
12Accepted1/18ms13300 KiB
13Accepted2/28ms13384 KiB
14Accepted3/37ms13476 KiB
15Accepted1/1163ms18404 KiB
16Accepted3/3149ms23580 KiB
17Accepted5/561ms16632 KiB
18Accepted1/1238ms24092 KiB
19Accepted2/2159ms18940 KiB
20Accepted3/3184ms33168 KiB
21Accepted4/4187ms32588 KiB
22Accepted4/4194ms38800 KiB