fork download
  1. #include<bits/stdc++.h>
  2. #define f1(i, n) for(ll i=1;i<=n;++i)
  3. #define f0(i, n) for(ll i=0;i<n;++i)
  4. #define ull unsigned long long
  5. #define ll long long
  6. #define rev(a) reverse(a.begin(),a.end())
  7. #define all(x) x.begin(),x.end()
  8. #define so(A, n) sort(A+1, A+n+1)
  9. using namespace std;
  10. const int maxn = 200010;
  11. const int N = 2e5 + 5;
  12. void bai1() {
  13. ll n;
  14. cin >> n;
  15. cout << (3 + (2 * n + 1)) * n / 2;
  16. }
  17. void bai3() {
  18. int n;
  19. cin >> n;
  20. int A[n + 5];
  21. f1(i, n) {
  22. cin >> A[i];
  23. }
  24. int i = 1, j = 1, res = 0;
  25. bool status = true;
  26. while (j <= n) {
  27. if (A[j] > 0) {
  28. if (!status) {
  29. i = j;
  30. status = true;
  31. }
  32. else {
  33. res = max(res, j - i + 1);
  34. }
  35. }
  36. else if (A[j] < 0) {
  37. if (status) {
  38. i = j;
  39. status = false;
  40. }
  41. else {
  42. res = max(res, j - i + 1);
  43. }
  44. }
  45. else {
  46. ++j;
  47. i = j;
  48. if (A[i] < 0) status = false;
  49. else status = true;
  50. }
  51. ++j;
  52. }
  53. cout << res;
  54. }
  55. map<int, int> Left, Right;
  56. int res[N];
  57. pair<int, int> A[N];
  58. void XepHang() {
  59. int n;
  60. cin >> n;
  61. f1(i, n) {
  62. cin >> A[i].first >> A[i].second;
  63. Right[A[i].first] = A[i].second;
  64. Left[A[i].second] = A[i].first;
  65. }
  66. sort(A + 1, A + n + 1);
  67. int missLeft = 0, missRight = 0;
  68. map<int, int> check;
  69. f1(i, n) {
  70. check[A[i].first] = 1;
  71. }
  72. f1(i, n) {
  73. if (check[i] == 0) missLeft = i;
  74. }
  75. check.clear();
  76. f1(i, n) {
  77. check[A[i].second] = 1;
  78. }
  79. f1(i, n) {
  80. if (check[i] == 0) missRight = i;
  81. }
  82. if (Left[missLeft] != 0) {
  83. res[n] = missLeft;
  84. res[1] = missRight;
  85. }
  86. else {
  87. if (Right[missRight] == 0) {
  88. res[n] = missRight;
  89. res[1] = missLeft;
  90. }
  91. else {
  92. res[1] = missRight;
  93. res[n] = missLeft;
  94. }
  95. }
  96.  
  97. // cout << res[1] << " " << res[n];
  98.  
  99. f1(i, n) {
  100. if (A[i].first == 0) {
  101. res[2] = A[i].second;
  102. }
  103. if (A[i].second == 0) {
  104. res[n - 1] = A[i].first;
  105. }
  106. }
  107.  
  108. for (int i = 3; i <= n - 2; ++i) {
  109. if (res[i] == 0) {
  110. if (Right[res[i - 2]] != 0) res[i] = Right[res[i - 2]];
  111. else if (res[i + 2] != 0 && Left[res[i + 2]] != 0) res[i] = Left[res[i + 2]];
  112. }
  113. }
  114. f1(i, n) {
  115. cout << res[i] << " ";
  116. }
  117. }
  118. int main()
  119. {
  120. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  121. XepHang();
  122. return 0;
  123. }
  124.  
  125. //[3, 1, 7, 5, 2, 6, 4]
  126.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty