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<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
using namespace std;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define PB push_back
const int MAXN = 500010;
int n, m, cn;
bool vis[MAXN], del[MAXN], ans[MAXN], allow[MAXN];
vector<int> g[MAXN], gi[MAXN], order, lst;
void dfs1(int v){
vis[v]=true;
for(int to:g[v]) if(!vis[to] && !del[to]) dfs1(to);
order.PB(v);
}
void dfs2(int v){
vis[v]=true;
for(int to:gi[v]) if(!vis[to] && !del[to]) dfs2(to);
lst.PB(v);
}
void dfs3(int v, bool flag){
del[v]=vis[v]=true;
ans[v]=flag;
for(int to:gi[v]) if(!del[to]){
if(flag) del[to]=true;
if(!vis[to] && allow[to]) dfs3(to, !flag);
}
}
int main(){
scanf("%d %d", &n, &m);
forn(i, m){
int a, b; scanf("%d %d", &a, &b); --a, --b;
g[a].PB(b), gi[b].PB(a);
}
while(true){
order.clear();
lst.clear();
forn(i, n) vis[i]=false;
forn(i, n) if(!vis[i] && !del[i]) dfs1(i);
if(order.empty()) break;
reverse(order.begin(), order.end());
forn(i, n) vis[i]=false;
for(int el:order) if(!vis[el]) lst.clear(), dfs2(el);
int v = lst.back();
assert(!del[v]);
forn(i, n) vis[i]=allow[i]=false;
for(int el:lst) allow[el]=true;
dfs3(v, true);
}
forn(i, n) cn+=ans[i];
printf("%d\n", cn);
forn(i, n) if(ans[i]) printf("%d ", i+1);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:38:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | int a, b; scanf("%d %d", &a, &b); --a, --b;
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |