Advertisement
OMEGAHEAD_MonkoX

Untitled

Mar 15th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <fstream>
  10. #include <stack>
  11. #include <map>
  12. #include <cstring>
  13. #include <cmath>
  14. #include <set>
  15. #include <iterator>
  16. #include <cmath>
  17. #include <locale>
  18. #include <queue>
  19. #include<deque>
  20. #include <stack>
  21.  
  22.  
  23. using namespace std;
  24.  
  25.  
  26.  
  27. const int MOD = 1e9+7;
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33. int N = 0, a = 0, m = 0,k = 0 ,t = 0, mi =-1;
  34. cin >> N;
  35. vector<stack<int> > s(N);
  36. for(auto i = 0; i < N; ++i)
  37. {
  38. while(!s[i].empty())
  39. {
  40. s[i].pop();
  41. }
  42. }
  43. if (N == 0)
  44. return 0;
  45. vector<pair<int, int> > answ;
  46. for(auto i = 0; i < N; ++i)
  47. {
  48. cin >> m;
  49. if (m != 0)
  50. {
  51. for(auto j = 0; j < m; ++j)
  52. {
  53. cin >> a;
  54. if(a == i + 1)
  55. k++;
  56. s[i].push(a);
  57. t++;
  58. }
  59. }
  60. }
  61. if(k == t || N == 1)
  62. return 0;
  63. t = 0;
  64. k = 0;
  65. if (N == 2)
  66. {
  67. while(!s[1].empty())
  68. {
  69. s[0].push(s[1].top());
  70. s[1].pop();
  71. answ.push_back(make_pair(2, 1));
  72. }
  73. while (!s[0].empty())
  74. {
  75. t++;
  76. a = s[0].top();
  77. s[0].pop();
  78. s[1].push(a);
  79. if (a != 1)
  80. answ.push_back(make_pair(1, 2));
  81. if (t == 1)
  82. {
  83. mi = a;
  84. }
  85. // cout << mi << " fgsasg " << a << endl;
  86. if (t > 1)
  87. {
  88. if (a > mi)
  89. {
  90. cout << 0;
  91. return 0;
  92. }
  93. if (mi > a)
  94. mi = a;
  95. }
  96. }
  97. }
  98.  
  99. if (N > 2)
  100. {
  101. for (int i = 1; i < s.size() ; ++i)
  102. {
  103. while(!s[i].empty())
  104. {
  105. a = s[i].top();
  106. s[i].pop();
  107. s[0].push(a);
  108. answ.push_back(make_pair(i + 1, 1));
  109. //cout << s[0].top() << endl;
  110. }
  111. }
  112. while(!s[0].empty())
  113. {
  114. a = s[0].top();
  115. s[0].pop();
  116. if (a == 1)
  117. {
  118. s[N - 1].push(a);
  119. answ.push_back(make_pair(1, N));
  120. }
  121. else{
  122. s[a - 1].push(a);
  123. answ.push_back(make_pair(1, a));
  124. }
  125. }
  126. while(!s[N - 1].empty())
  127. {
  128. a = s[N - 1].top();
  129. s[N - 1].pop();
  130. if (a == 1){
  131. s[0].push(a);
  132. answ.push_back(make_pair(N, 1));
  133. }
  134. else{
  135. s[N - 2].push(a);
  136. // cout << a << " " << endl;
  137. answ.push_back(make_pair(N, N - 1));
  138. }
  139. if (s[N - 1].size() == 0)
  140. break;
  141. }
  142. if (s[N - 2].size() > 0)
  143. a = s[N - 2].top();
  144. while(a == N)
  145. {
  146. s[N - 2].pop();
  147. s[N - 1].push(a);
  148. //cout << a << " dasdoajod" << endl;
  149. answ.push_back(make_pair(N - 1, N));
  150. if (s[N - 2].size() == 0)
  151. break;
  152. a = s[N - 2].top();
  153. }
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. for(int i = 0; i < answ.size(); ++i)
  163. {
  164. cout << answ[i].first << " " << answ[i].second << endl;
  165. }
  166.  
  167.  
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement