fork download
  1. #include<iostream>
  2. using namespace std;
  3. #define MAX 8500
  4. int temp;
  5. struct node{
  6. int parent;
  7. int child;
  8. int wartosc;
  9. bool uzyta;
  10. };
  11. struct node2{
  12. node2 *parent;
  13. node2 *left;
  14. node2 *right;
  15. int wartosc;
  16. int co;
  17. bool uzyta;
  18. };
  19. void zeruj(node2 *a){
  20. a->co = NULL;
  21. a->left = NULL;
  22. a->parent = NULL;
  23. a->right = NULL;
  24. a->wartosc = NULL;
  25. }
  26. void szukajdziecka(node2 *w, node t[], int n){
  27. int znalazl = false;
  28. int znalazl2 = false;
  29. for (int i = 0; i < n; i++){
  30. if (t[i].parent == w->co){
  31. znalazl = true;
  32. if (t[i].uzyta == false){
  33. if (w->left == NULL){
  34. w->left = new node2;
  35. zeruj(w->left);
  36. w->left->parent = w;
  37. w->left->wartosc = t[i].wartosc;
  38. w->left->co = t[i].child;
  39. t[i].uzyta = true;
  40. szukajdziecka(w->left, t, n);
  41. }
  42. else if (w->right == NULL){
  43. w->right = new node2;
  44. zeruj(w->right);
  45. w->right->parent = w;
  46. w->right->wartosc = t[i].wartosc;
  47. w->right->co = t[i].child;
  48. t[i].uzyta = true;
  49. szukajdziecka(w->right, t, n);
  50. }
  51. }
  52. }
  53. if (t[i].child == w->co){
  54. znalazl2 = true;
  55. if (t[i].uzyta == false){
  56. if (w->left == NULL){
  57. w->left = new node2;
  58. zeruj(w->left);
  59. w->left->parent = w;
  60. w->left->wartosc = t[i].wartosc;
  61. w->left->co = t[i].parent;
  62. t[i].uzyta = true;
  63. szukajdziecka(w->left, t, n);
  64. }
  65. else if (w->right == NULL){
  66. w->right = new node2;
  67. zeruj(w->right);
  68. w->right->parent = w;
  69. w->right->wartosc = t[i].wartosc;
  70. w->right->co = t[i].parent;
  71. t[i].uzyta = true;
  72. szukajdziecka(w->right, t, n);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. int liczymy(node2 *w, int n){
  79. if (n > 0){
  80. if (w->left != NULL && w->right != NULL){
  81. int pom1, pom2;
  82. if ((pom1 = liczymy(w->left, n - 1)) >= (pom2 = liczymy(w->right, n - 1))){
  83. return pom1 + w->wartosc;
  84. }
  85. else {
  86. return pom2 + w->wartosc;
  87. }
  88. }else if (w->left != NULL){
  89. return liczymy(w->left, n - 1) + w->wartosc;
  90. }else if (w->right != NULL){
  91. return liczymy(w->right, n - 1) + w->wartosc;
  92. }else if (w->left == NULL && w->right == NULL){
  93. return w->wartosc;
  94. }
  95. else return 0;
  96. }
  97. else return 0;
  98. }
  99. int main(){
  100. node t[MAX];
  101. node2 *root=new node2;
  102. zeruj(root);
  103. int n, ruch,pom=1;
  104. cin >> n >> ruch;
  105. for (int i = 0; i < n; i++){
  106. cin >> t[i].parent >> t[i].child >> t[i].wartosc;
  107. t[i].uzyta = false;
  108. }
  109. for (int i = 0; i < n; i++){
  110. if (t[i].parent == 1 || t[i].child == 1){
  111. t[i].uzyta = true;
  112. root->co = t[i].child;
  113. root->wartosc = t[i].wartosc;
  114. szukajdziecka(root, t, n);
  115. }
  116. }
  117. cout<<liczymy(root, n);
  118. return 0;
  119. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty