fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct node{
  6. int n;
  7. struct node* next;
  8. } node;
  9. int * g[10005];
  10. int t;
  11. int visited[10005];
  12.  
  13. int col[10005];
  14. int fin[10005];
  15. int cur;
  16. int flag;
  17.  
  18. int nd_count[10005];
  19. void explore(int n){
  20.  
  21. visited[n] = 1;
  22. col[n] = 1;
  23. int i;
  24. for( i = nd_count[n] -1; i >= 0; i--){
  25. if (!visited[g[n][i]])
  26. explore(g[n][i]);
  27. if (col[g[n][i]] == 1){
  28. // cout << "herE" << endl;
  29. flag = 1;
  30. }
  31. if (flag)
  32. return;
  33. }
  34. col[n] = 2;
  35.  
  36.  
  37. fin[cur--] = n;
  38. return;
  39. }
  40. int n;
  41. void dfs(){
  42. cur = n;
  43. int i;
  44. for(i = n ; i > 0; i--){
  45. if (!visited[i])
  46. explore(i);
  47. if (flag)
  48. return;
  49. }
  50. return;
  51. }
  52. int current_idx[10005];
  53. int li_cnt[10005];
  54. node* li[10005];
  55. node comp[1000006];
  56.  
  57.  
  58. int main()
  59. {
  60. int m;
  61. scanf("%d %d", &n, &m);
  62. int i;
  63. for( i = 1; i <= m; i++){
  64. int x,y;
  65. scanf("%d %d", &x, &y);
  66. comp[i].n = x;
  67. comp[i].next = li[y];
  68. li[y] = &comp[i];
  69.  
  70. nd_count[x]++;
  71. }
  72. for( i = 1; i <= n; i++)
  73. g[i] = malloc(sizeof(int)*nd_count[i]);;
  74.  
  75. for( i = 1; i <= n; i++){
  76. node * pt = li[i];
  77. while (pt != 0){
  78. g[pt->n][current_idx[pt->n]++] = i;
  79. pt = pt->next;
  80. }
  81.  
  82.  
  83. }
  84.  
  85.  
  86. dfs();
  87. if (flag == 1){
  88. printf("Sandro fails.\n");
  89. return 0;
  90. }
  91.  
  92. for( i = 1; i < n; i++)
  93. printf("%d ", fin[i]);
  94. printf("%d\n", fin[n]);
  95. return 0;
  96. }
  97.  
  98.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty