#include<bits/stdc++.h>
using namespace std;
vector<int> kraw[500001];
int odw[500001], low[500001], low2[500001], ma[500001];
int zle[500001], zle2[500001], ord[500001], stos[500001];
vector<int> wyn;
int ile=0, num=0, cz=3, g=0;
void dfs(int x){
odw[x]=2;
g++;
stos[g]=x;
num++;
ord[x]=num;
ma[x]=ord[x];
// printf("%d ", x);
// for(int i=1; i<=g; i++)
// printf("%d ", stos[i]);
// printf("\n");
for(vector<int> ::iterator it=kraw[x].begin(); it!=kraw[x].end(); it++){
if(odw[*it]==0){
dfs(*it);
ma[x]=min(ma[x], ma[*it]);
low[x]+=low[*it];
low[x]+=low2[*it];
}
else{
if(odw[*it]==2)
{
ma[x]=min(ma[x], ord[*it]);
ile++;
low[x]++;
low2[*it]--;
}
if(odw[*it]==cz)
{
int pocz=1, kon=g;
while(pocz<kon){
int s=(pocz+kon+1)/2;
if(ord[stos[s]]<ord[*it])
pocz=s;
else
kon=s-1;
}
// if(*it==8)
// printf("?%d %d\n", x, pocz);
if(ord[stos[pocz]]>=ma[*it])
{
zle2[*it]++;
zle[x]++;
zle[stos[pocz]]-=2;
}
ma[x]=min(ma[x], ma[*it]);
}
}
}
g--;
odw[x]=cz;
return;
}
void dfs2(int x)
{
odw[x]=1;
for(vector<int> ::iterator it=kraw[x].begin(); it!=kraw[x].end(); it++){
if(odw[*it]==0){
dfs2(*it);
zle[x]+=zle[*it]+zle2[*it];
}
}
return;
}
int main()
{
int n,m;
scanf("%d%d", &n, &m);
for(int i=1; i<=m; i++)
{
int a,b;
scanf("%d%d", &a, &b);
kraw[a].push_back(b);
}
for(int i=1; i<=n; i++)
if(odw[i]==0)
{
g=0;
dfs(i);
cz++;
}
for(int i=1; i<=n; i++)
odw[i]=0;
for(int i=1; i<=n; i++)
if(odw[i]==0)
dfs2(i);
// for(int i=1; i<=n; i++)
// printf("%d: %d %d %d\n", i, low[i], zle[i], ma[i]);
if(ile==0)
printf("NIE\n");
else
{
for(int i=1; i<=n; i++)
if(low[i]==ile && zle[i]==0)
wyn.push_back(i);
printf("%d\n", int(wyn.size()));
for(int i=0; i<wyn.size(); i++)
printf("%d ", wyn[i]);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include<bits/stdc++.h> using namespace std; vector<int> kraw[500001]; int odw[500001], low[500001], low2[500001], ma[500001]; int zle[500001], zle2[500001], ord[500001], stos[500001]; vector<int> wyn; int ile=0, num=0, cz=3, g=0; void dfs(int x){ odw[x]=2; g++; stos[g]=x; num++; ord[x]=num; ma[x]=ord[x]; // printf("%d ", x); // for(int i=1; i<=g; i++) // printf("%d ", stos[i]); // printf("\n"); for(vector<int> ::iterator it=kraw[x].begin(); it!=kraw[x].end(); it++){ if(odw[*it]==0){ dfs(*it); ma[x]=min(ma[x], ma[*it]); low[x]+=low[*it]; low[x]+=low2[*it]; } else{ if(odw[*it]==2) { ma[x]=min(ma[x], ord[*it]); ile++; low[x]++; low2[*it]--; } if(odw[*it]==cz) { int pocz=1, kon=g; while(pocz<kon){ int s=(pocz+kon+1)/2; if(ord[stos[s]]<ord[*it]) pocz=s; else kon=s-1; } // if(*it==8) // printf("?%d %d\n", x, pocz); if(ord[stos[pocz]]>=ma[*it]) { zle2[*it]++; zle[x]++; zle[stos[pocz]]-=2; } ma[x]=min(ma[x], ma[*it]); } } } g--; odw[x]=cz; return; } void dfs2(int x) { odw[x]=1; for(vector<int> ::iterator it=kraw[x].begin(); it!=kraw[x].end(); it++){ if(odw[*it]==0){ dfs2(*it); zle[x]+=zle[*it]+zle2[*it]; } } return; } int main() { int n,m; scanf("%d%d", &n, &m); for(int i=1; i<=m; i++) { int a,b; scanf("%d%d", &a, &b); kraw[a].push_back(b); } for(int i=1; i<=n; i++) if(odw[i]==0) { g=0; dfs(i); cz++; } for(int i=1; i<=n; i++) odw[i]=0; for(int i=1; i<=n; i++) if(odw[i]==0) dfs2(i); // for(int i=1; i<=n; i++) // printf("%d: %d %d %d\n", i, low[i], zle[i], ma[i]); if(ile==0) printf("NIE\n"); else { for(int i=1; i<=n; i++) if(low[i]==ile && zle[i]==0) wyn.push_back(i); printf("%d\n", int(wyn.size())); for(int i=0; i<wyn.size(); i++) printf("%d ", wyn[i]); } } |
English