fork download
  1. //84104971101048411497 - Can you guess what does this mean?
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. typedef complex<double> point;
  8. #define mapii map<int, int>
  9. #define debug(a) cout << #a << ": " << a << endl
  10. #define debuga1(a, l, r) fto(i, l, r) cout << a[i] << " "; cout << endl
  11. #define fdto(i, r, l) for(int i = (r); i >= (l); --i)
  12. #define fto(i, l, r) for(int i = (l); i <= (r); ++i)
  13. #define forit(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)
  14. #define forrit(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)
  15. #define ii pair<int, int>
  16. #define iii pair<int, ii>
  17. #define ff first
  18. #define ss second
  19. #define mp make_pair
  20. #define pb push_back
  21. #define X real()
  22. #define Y imag()
  23. #define maxN 1000005
  24. #define oo 2000000007
  25.  
  26. const double PI = acos(-1.0);
  27.  
  28. double fRand(double fMin, double fMax)
  29. {
  30. double f = (double)rand() / RAND_MAX;
  31. return fMin + f * (fMax - fMin);
  32. }
  33.  
  34. template <class T>
  35. T min(T a, T b, T c) {
  36. return min(a, min(b, c));
  37. }
  38.  
  39. template <class T>
  40. T max(T a, T b, T c) {
  41. return max(a, max(b, c));
  42. }
  43.  
  44. int readInt () {
  45. bool minus = false;
  46. int result = 0;
  47. char ch;
  48. ch = getchar();
  49. while (true) {
  50. if (ch == '-') break;
  51. if (ch >= '0' && ch <= '9') break;
  52. ch = getchar();
  53. }
  54. if (ch == '-') minus = true; else result = ch-'0';
  55. while (true) {
  56. ch = getchar();
  57. if (ch < '0' || ch > '9') break;
  58. result = result*10 + (ch - '0');
  59. }
  60. if (minus)
  61. return -result;
  62. else
  63. return result;
  64. }
  65.  
  66. bool cmp(string &s, string &t) {
  67. if (s.length() < t.length()) return true;
  68. if (s.length() > t.length()) return false;
  69. return (s < t);
  70. }
  71.  
  72. string toBin(int n) {
  73. string s;
  74. if (n == 0) s += "0";
  75. while (n > 0) {
  76. s += n%2 + '0';
  77. n /= 2;
  78. }
  79. reverse(s.begin(), s.end());
  80. return s;
  81. }
  82.  
  83. int n, k;
  84. vector<int> ke[maxN];
  85. string *d[3*maxN];
  86.  
  87. void Calc(int u) {
  88. if (u <= n) {
  89. fto(i, 0, 1) Calc(ke[u][i]);
  90. if (cmp(*d[ke[u][0]], *d[ke[u][1]])) d[u] = d[ke[u][1]];
  91. else d[u] = d[ke[u][0]];
  92. *d[u] += '0';
  93. }
  94. }
  95.  
  96. int main () {
  97. #ifndef ONLINE_JUDGE
  98. freopen("input.txt", "r", stdin);
  99. //freopen("output.txt", "w", stdout);
  100. #endif // ONLINE_JUDGE
  101.  
  102. n = readInt(); k = n;
  103. fto(i, 1, n) {
  104. d[i] = new string();
  105. fto(j, 1, 2) {
  106. int x = readInt();
  107. if (x > 0) ke[i].pb(x);
  108. else {
  109. d[++k] = new string();
  110. *d[k] = toBin(-x);
  111. ke[i].pb(k);
  112. }
  113. }
  114. }
  115.  
  116. Calc(1);
  117.  
  118. string ans = *d[1];
  119. fto(i, 0, ans.length()-1) putchar(ans[i]);
  120.  
  121. return 0;
  122. }
  123.  
Success #stdin #stdout 0s 62112KB
stdin
4
2 3
-9 4
-2 -13
-1 -7
stdout
111000