This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
//#define int long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=500005;
int n,m;
vector<int> vis(N,-1),out(N,0);
vector<int> adj[N];
int32_t main() {
LCBorz;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;
out[a]++;
adj[b].push_back(a);
}
vector<int> ans1(N,-1);
queue<int> q;
for(int i=1;i<=n;i++){
if(out[i]==0){
ans1[i]=1;
q.push(i);
}
}
auto run=[&]()->void {
while(q.size()){
int p=q.front();
q.pop();
ans1[p]=1;
for(int j:adj[p]){
if(ans1[j]==1){
cout<<-1<<endl;
exit(0);
}
if(ans1[j]==-1){
for(int j1:adj[j]){
out[j1]--;
if(out[j1]==0){
q.push(j1);
}
}
}
ans1[j]=0;
}
}
};
run();
for(int i=1;i<=n;i++){
if(ans1[i]==-1){
q.push(i);
run();
}
}
vector<int> ans;
for(int i=1;i<=n;i++){
if(ans1[i]==1){
ans.push_back(i);
}
}
cout<<ans.size()<<endl;
for(int j:ans){
cout<<j<<' ';
}
cout<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |