Advertisement
PedalaVasile

Untitled

Oct 2nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("retea1.in");
  6. ofstream fout("retea1.out");
  7.  
  8. vector < int > l[305];
  9. vector < int > g[305];
  10. vector < int > rez;
  11.  
  12. stack < int > v;
  13. stack < int > stk;
  14.  
  15. bitset < 305 > viz;
  16.  
  17. int a[305][305];
  18. int n;
  19.  
  20. void DFS(int nod)
  21. {
  22. viz[nod] = 1;
  23.  
  24. for (int el : l[nod])
  25. if (!viz[el])
  26. DFS(el);
  27.  
  28. v.push(nod);
  29. }
  30.  
  31. void DFST(int nod)
  32. {
  33. stk.push(nod);
  34. viz[nod] = 1;
  35. int c = 1;
  36.  
  37. while (!stk.empty())
  38. {
  39. nod = stk.top();
  40. stk.pop();
  41.  
  42. for (int el : g[nod])
  43. {
  44. if (!viz[el])
  45. {
  46. if (c == 1)
  47. rez.push_back(nod);
  48.  
  49. c++;
  50. rez.push_back(el);
  51. viz[el] = 1;
  52. stk.push(el);
  53. }
  54. }
  55. }
  56.  
  57. cout << c << ' ' << a[nod][nod] << ' ' << nod;
  58.  
  59. if (c == 1 && a[nod][nod])
  60. rez.push_back(nod);
  61. }
  62.  
  63. int main()
  64. {
  65. fin >> n;
  66.  
  67. rez.reserve(n);
  68.  
  69. for (int i = 1; i <= n; i++)
  70. {
  71. for (int j = 1; j <= n; j++)
  72. {
  73. int t;
  74.  
  75. fin >> t;
  76.  
  77. if (t)
  78. {
  79. l[i].push_back(j);
  80. g[j].push_back(i);
  81. }
  82. }
  83. }
  84.  
  85. for (int i = 1; i <= n; i++)
  86. {
  87. if (!viz[i])
  88. DFS(i);
  89. }
  90.  
  91. viz.reset();
  92.  
  93. while (!v.empty())
  94. {
  95. cout << v.top() << ' ';
  96. if (!viz[v.top()])
  97. {
  98. DFST(v.top());
  99. }
  100.  
  101. v.pop();
  102. }
  103.  
  104. for (int el : rez)
  105. fout << el << ' ' ;
  106.  
  107.  
  108. fin.close();
  109. fout.close();
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement