Source code for submission s903

Go to diff to previous submission

Fr.java

  1.  
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.Scanner;
  5.  
  6. /*
  7.  * To change this template, choose Tools | Templates
  8.  * and open the template in the editor.
  9.  */
  10.  
  11.  
  12. /**
  13.  *
  14.  * @author cteam92
  15.  */
  16. public class Fr {
  17. static ArrayList <LinkedList<toEdge>> graph;
  18. static int c;
  19.  
  20. /**
  21.   * @param args the command line arguments
  22.   */
  23. public static void main(String[] args) {
  24. Scanner sc = new Scanner(System.in);
  25. while (true) {
  26. int n;
  27. int u, v, w;
  28.  
  29. if (!sc.hasNext()) break;
  30.  
  31. n = sc.nextInt();
  32. c = sc.nextInt();
  33.  
  34. graph = new ArrayList <LinkedList<toEdge>>();
  35. for (int i = 0; i < n; i++) {
  36. graph.add(new LinkedList<toEdge>());
  37. }
  38.  
  39. for (int i = 0; i < n - 1; i++) {
  40. u = sc.nextInt();
  41. v = sc.nextInt();
  42. w = sc.nextInt();
  43. sc.nextLine();
  44.  
  45. LinkedList<toEdge> vertex;
  46.  
  47. //System.out.println(u);
  48. //System.out.println(graph.);
  49. /*vertex = graph.get(u - 1);
  50.   toEdge V2 = new toEdge(v - 1, w);
  51.   vertex.add(V2);
  52.   graph.set(u - 1, vertex);*/
  53. graph.get(u-1).add(new toEdge(v-1, w));
  54. graph.get(v-1).add(new toEdge(u-1, w));
  55. }
  56.  
  57. //visited = new boolean[n];
  58. int sum = minOfEdges(c - 1, -1);
  59. System.out.println(sum);
  60. }
  61. }
  62.  
  63.  
  64. public static int minOfEdges(int node, int parent) {
  65. int sum = 0;
  66.  
  67. //System.out.println(node + " " + parent);
  68.  
  69. if (node != c -1 && graph.get(node).size() == 1)
  70. return Integer.MAX_VALUE;
  71. for (toEdge e: graph.get(node)) {
  72. if (e.V != parent) {
  73. //System.out.println(e.value + ";" + minOfEdges(e.V, node));
  74. sum += Math.min(e.value, minOfEdges(e.V, node));
  75. }
  76. }
  77.  
  78. return sum;
  79. }
  80.  
  81. public static class toEdge {
  82. int V;
  83. int value;
  84.  
  85. public toEdge(int V, int value) {
  86. this.V = V;
  87. this.value = value;
  88. }
  89.  
  90. @Override
  91. public String toString() {
  92. return "toEdge{" + "V=" + V + ", value=" + value + '}';
  93. }
  94.  
  95.  
  96. }
  97. }
  98.  

Diff to submission s889

Fr.java

--- c5.s889.cteam092.fr.java.0.Fr.java
+++ c5.s903.cteam092.fr.java.0.Fr.java
@@ -16,4 +16,5 @@
 public class Fr {
     static ArrayList <LinkedList<toEdge>> graph;
+    static int c;
 
     /**
@@ -23,5 +24,5 @@
         Scanner sc = new Scanner(System.in);
         while (true) {            
-            int n, c;
+            int n;
             int u, v, w;
             
@@ -66,5 +67,5 @@
         //System.out.println(node + " " + parent);
         
-        if (graph.get(node).size() == 1)
+        if (node != c -1 && graph.get(node).size() == 1)
             return Integer.MAX_VALUE;
         for (toEdge e: graph.get(node)) {