fork download
  1. #include <bits/stdc++.h>
  2. #define fst ios_base::sync_with_stdio(0); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  3. #define PI acos(-1.0)
  4. #define rd freopen("ip.txt","r",stdin)
  5. #define wr freopen("op.txt", "w", stdout)
  6. #define wis(x) cerr << #x << " is " << x << endl;
  7. #define mem(array,int) memset(array,int,sizeof(array))
  8. #define lld long long int
  9. #define nl cout<<endl;
  10. #define mx 200009
  11. #define MOD 1000000007
  12. #define pii pair<int,int>
  13. #define INF 1000009
  14. using namespace std;
  15.  
  16. //const int mx = 1e5+5;
  17.  
  18.  
  19.  
  20.  
  21. vector<int>G[mx],city;
  22.  
  23. int color[mx] ,vis[mx],par[mx],dist[mx],N,E;
  24.  
  25.  
  26.  
  27. void DFS(int n){
  28.  
  29.  
  30. color[n] = 1;
  31. vis[n]++;
  32.  
  33. for (int i=0; i<G[n].size(); i++) {
  34. int x = G[n][i];
  35.  
  36. if (color[x] == 0) {
  37. DFS(x);
  38. }
  39. }
  40. // color[n] = 2;
  41. //cout<<n<<" ";
  42.  
  43. }
  44.  
  45.  
  46.  
  47. int main()
  48. {
  49.  
  50. // rd;
  51. //wr;
  52. fst;
  53.  
  54.  
  55.  
  56. while(cin>>N>>E)
  57. {
  58.  
  59. for(int i=0;i<E;i++)
  60. {
  61. int u,v;
  62. cin >> u >> v;
  63. G[u].push_back(v);
  64. // G[v].push_back(u);
  65. }
  66.  
  67. for (int i = 1; i <= N; ++i)
  68. {
  69. DFS(i);
  70. mem(color,0);
  71. // nl;
  72. }
  73.  
  74. // int lim = sizeof(vis)/sizeof(*vis);
  75. for (int i = 1; i <= N ; ++i)
  76. {
  77. if (vis[i] == N)
  78. {
  79. city.push_back(i);
  80. }
  81. // printf("%d == %d\n",i,vis[i] );
  82. }
  83. int lim = city.size();
  84. //cout<<lim<<endl;
  85.  
  86. printf("%d\n",lim );
  87. for (int i = 0; i < lim; ++i)
  88. {
  89. // cout<<city[i]<<" ";
  90. printf("%d ",city[i] );
  91. }
  92. nl;
  93. mem(vis,0);
  94. mem(color,0);
  95. for (int i = 0; i < mx; ++i)
  96. {
  97. G[i].clear();
  98. }
  99.  
  100.  
  101. }
  102.  
  103.  
  104. return 0;
  105. }
Success #stdin #stdout 0s 23048KB
stdin
Standard input is empty
stdout
Standard output is empty