#include<bits/stdc++.h>
using namespace std;
vector<int> graf[5000009];
int col[5000009];
int tak[5000009];
int cyk=0;
vector<int> wyn;
void DFS(int u, vector<int> g)
{
col[u]=1;
g.push_back(u);
for(int i=0; i<graf[u].size(); i++)
{
int v=graf[u][i];
if(col[v]==0)
{
DFS(v,g);
}
if(col[v]==1)
{
cyk++;
int a=0;
while(g[a]!=v) a++;
for(int i=a; i<g.size(); i++)
{
int b=g[i];
tak[b]++;
}
}
}
col[u]=2;
return;
}
int main()
{
int n,m,a,b,c=0;
scanf("%d%d", &n, &m);
for(int i=1; i<=m; i++)
{
scanf("%d%d", &a, &b);
graf[a].push_back(b);
}
vector<int> g;
for(int i=1; i<=n; i++) if(col[i]==0) DFS(i, g);
if(cyk==0)
{
printf("NIE");
return 0;
}
for(int i=1; i<=n; i++)
{
if(tak[i]==cyk)
{
c++;
wyn.push_back(i);
}
}
printf("%d\n", c);
sort(wyn.begin(), wyn.end());
for(int i=0; i<wyn.size(); i++) printf("%d ", wyn[i]);
return 0;
}
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 | #include<bits/stdc++.h> using namespace std; vector<int> graf[5000009]; int col[5000009]; int tak[5000009]; int cyk=0; vector<int> wyn; void DFS(int u, vector<int> g) { col[u]=1; g.push_back(u); for(int i=0; i<graf[u].size(); i++) { int v=graf[u][i]; if(col[v]==0) { DFS(v,g); } if(col[v]==1) { cyk++; int a=0; while(g[a]!=v) a++; for(int i=a; i<g.size(); i++) { int b=g[i]; tak[b]++; } } } col[u]=2; return; } int main() { int n,m,a,b,c=0; scanf("%d%d", &n, &m); for(int i=1; i<=m; i++) { scanf("%d%d", &a, &b); graf[a].push_back(b); } vector<int> g; for(int i=1; i<=n; i++) if(col[i]==0) DFS(i, g); if(cyk==0) { printf("NIE"); return 0; } for(int i=1; i<=n; i++) { if(tak[i]==cyk) { c++; wyn.push_back(i); } } printf("%d\n", c); sort(wyn.begin(), wyn.end()); for(int i=0; i<wyn.size(); i++) printf("%d ", wyn[i]); return 0; } |
English