47552023-03-31 11:05:39Erik_GepardTestnevelés óracpp17Runtime error 27/504ms4428 KiB
#include <bits/stdc++.h>
using namespace std;

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

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
base27/50
1Accepted0/03ms2244 KiB
2Accepted0/03ms2404 KiB
3Runtime error0/03ms2716 KiB
4Accepted2/23ms2828 KiB
5Accepted3/33ms3040 KiB
6Accepted3/33ms3296 KiB
7Accepted3/33ms3468 KiB
8Accepted1/13ms3680 KiB
9Accepted3/33ms3900 KiB
10Accepted3/34ms4012 KiB
11Accepted3/34ms4176 KiB
12Accepted1/14ms4024 KiB
13Accepted2/24ms4020 KiB
14Accepted3/33ms4024 KiB
15Runtime error0/13ms4196 KiB
16Runtime error0/33ms4428 KiB
17Runtime error0/53ms4304 KiB
18Runtime error0/13ms4340 KiB
19Runtime error0/23ms4428 KiB
20Runtime error0/33ms4368 KiB
21Runtime error0/43ms4400 KiB
22Runtime error0/43ms4400 KiB