Source code for submission s1055

Go to diff to previous submission

Main.java

  1.  
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  *
  8.  * @author cteam049
  9.  */
  10. public class Main {
  11.  
  12. static class Node {
  13. Map<Integer, Node> sucs = new HashMap<Integer, Main.Node>();
  14. int value;
  15. int force;
  16. int count;
  17. Node parent;
  18.  
  19. public Node(int value) {
  20. this.value = value;
  21. }
  22. }
  23.  
  24. private static boolean appendNode(Node node, int i1, int i2, int i3) {
  25. if (node.value == i1) {
  26. Node n = new Node(i2);
  27. n.force = i3;
  28. n.parent = node;
  29. node.sucs.put(i2, n);
  30.  
  31. return true;
  32. } else if (node.value == i2) {
  33. Node n = new Node(i1);
  34. n.force = i3;
  35. n.parent = node;
  36. node.sucs.put(i1, n);
  37.  
  38. return true;
  39. } else if (node.sucs.containsKey(i1)) {
  40. return appendNode(node.sucs.get(i1), i1, i2, i3);
  41. } else if (node.sucs.containsKey(i2)) {
  42. return appendNode(node.sucs.get(i2), i1, i2, i3);
  43. } else {
  44. for (Node tmp : node.sucs.values()) {
  45. if (appendNode(tmp, i1, i2, i3)) return true;
  46. }
  47. }
  48. return false;
  49. }
  50.  
  51.  
  52. private static void preorder(Node node) {
  53. System.out.println(node.value);
  54. for (Node n : node.sucs.values()) {
  55. preorder(n);
  56. }
  57.  
  58. }
  59.  
  60. private static int solve(Node node) {
  61. if (node.sucs.isEmpty()) {
  62. return node.force;
  63. }
  64. int sum = 0;
  65. for (Node n : node.sucs.values()) {
  66. sum += solve(n);
  67. }
  68. if (sum < node.force) {
  69. return sum;
  70. }
  71. return node.force;
  72. }
  73.  
  74. private static int solveIterative(Node node) {
  75. outer:
  76. while (node.parent != null) {
  77. // System.out.println("Uzel " + node.value);
  78. if (node.sucs.isEmpty()) {
  79. node.count = node.force;
  80. } else {
  81. for (Node n : node.sucs.values()) {
  82. // System.out.println("Potomek " + n.value);
  83. if (n.count == 0) {
  84. node = n;
  85. continue outer;
  86. }
  87. }
  88. for (Node n : node.sucs.values()) {
  89. node.count += n.count;
  90. }
  91. if (node.count > node.force) node.count = node.force;
  92. }
  93. node = node.parent;
  94. }
  95. // System.out.println("Uzel " + node.value);
  96. return node.sucs.get(0).count;
  97. }
  98.  
  99. public static void main(String[] args) {
  100. Scanner sc = new Scanner(System.in);
  101. String line = null;
  102. int n, c;
  103. while (sc.hasNext()) {
  104. n = sc.nextInt();
  105. c = sc.nextInt();
  106. Node n1 = new Node(c);
  107. n1.force = Integer.MAX_VALUE;
  108. n1.parent = null;
  109.  
  110. for (int i=0 ; i<n-1 ; i++) {
  111. int i1 = sc.nextInt();
  112. int i2 = sc.nextInt();
  113. int i3 = sc.nextInt();
  114. appendNode(n1, i1, i2, i3);
  115. }
  116.  
  117. Node parent = new Node(Integer.MAX_VALUE);
  118. parent.sucs.put(0, n1);
  119. parent.force = Integer.MAX_VALUE;
  120. n1.parent = parent;
  121. System.out.println(solveIterative(n1));
  122. // preorder(n1);
  123. // System.out.println(solve(n1));
  124. }
  125. }
  126.  
  127. }
  128.  

Diff to submission s945

Main.java

--- c5.s945.cteam049.fr.java.0.Main.java
+++ c5.s1055.cteam049.fr.java.0.Main.java
@@ -14,4 +14,6 @@
         int value;
         int force;
+        int count;
+        Node parent;
         
         public Node(int value) {
@@ -24,10 +26,14 @@
             Node n = new Node(i2);
             n.force = i3;
+            n.parent = node;
             node.sucs.put(i2, n);
+            
             return true;
         } else if (node.value == i2) {
             Node n = new Node(i1);
             n.force = i3;
+            n.parent = node;
             node.sucs.put(i1, n);
+            
             return true;
         } else if (node.sucs.containsKey(i1)) {
@@ -66,4 +72,29 @@
     }
     
+    private static int solveIterative(Node node) {
+        outer:
+        while (node.parent != null) {
+//            System.out.println("Uzel " + node.value);
+            if (node.sucs.isEmpty()) {
+                node.count = node.force;
+            } else {
+                for (Node n : node.sucs.values()) {
+//                    System.out.println("Potomek " + n.value);
+                    if (n.count == 0) {
+                        node = n;
+                        continue outer;
+                    }
+                }
+                for (Node n : node.sucs.values()) {
+                    node.count += n.count;
+                }
+                if (node.count > node.force) node.count = node.force;
+            }
+            node = node.parent;
+        }
+//        System.out.println("Uzel " + node.value);
+        return node.sucs.get(0).count;
+    }
+    
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
@@ -75,4 +106,5 @@
             Node n1 = new Node(c);
             n1.force = Integer.MAX_VALUE;
+            n1.parent = null;
             
             for (int i=0 ; i<n-1 ; i++) {
@@ -83,6 +115,11 @@
             }
             
+            Node parent = new Node(Integer.MAX_VALUE);
+            parent.sucs.put(0, n1);
+            parent.force = Integer.MAX_VALUE;
+            n1.parent = parent;
+            System.out.println(solveIterative(n1));
 //            preorder(n1);
-            System.out.println(solve(n1));
+//            System.out.println(solve(n1));
         }
     }