Source code for submission s986

Go to diff to previous submission

Fr.java

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Fr {
  8.  
  9. public static void main(String[] args) throws NumberFormatException, IOException {
  10.  
  11. int n;
  12. int main;
  13.  
  14. int a;
  15. int b;
  16. int v;
  17.  
  18. String line;
  19. String[] lis;
  20. while((line = br.readLine()) != null){
  21. lis = line.split(" ");
  22. n = Integer.parseInt(lis[0]);
  23. main = Integer.parseInt(lis[1]);
  24.  
  25.  
  26. int[][] mat = new int[n+1][n+1];
  27.  
  28. for (int i = 0; i < n-1; i++) {
  29. line = br.readLine();
  30. lis = line.split(" ");
  31.  
  32. a = Integer.parseInt(lis[0]);
  33. b = Integer.parseInt(lis[1]);
  34. v = Integer.parseInt(lis[2]);
  35.  
  36. mat[a][b] = v;
  37. mat[b][a] = v;
  38. }
  39.  
  40. int sumV = 0;
  41. for (int i = 1; i < n+1; i++) {
  42. if (i != main && mat[main][i] != 0){
  43. sumV += getValueS(mat, i, main);
  44. }
  45. }
  46. System.out.println(sumV);
  47.  
  48. }
  49. br.close();
  50. }
  51.  
  52.  
  53. static int getValueS(int[][] mat, int s, int r){
  54. int sumV = 0;
  55. for (int i = 1; i < mat.length; i++) {
  56. if (i != r && mat[i][s] != 0){
  57. sumV += getValueS(mat, i ,s);
  58. }
  59. }
  60.  
  61. if (sumV != 0 && sumV < mat[r][s]){
  62. return sumV;
  63. }
  64. return mat[r][s];
  65. }
  66.  
  67. }

Diff to submission s954

Fr.java

--- c5.s954.cteam055.fr.java.0.Fr.java
+++ c5.s986.cteam055.fr.java.0.Fr.java
@@ -1,2 +1,5 @@
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.Scanner;
 
@@ -4,6 +7,6 @@
 public class Fr {
 
-        public static void main(String[] args) {
-                Scanner sc = new Scanner(System.in);
+        public static void main(String[] args) throws NumberFormatException, IOException {
+                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 
                 int n;
@@ -14,10 +17,10 @@
                 int v;
 
-                while(sc.hasNextInt()){ 
-                        a = 0;
-                        b = 0;
-                        
-                        n = sc.nextInt();
-                        main = sc.nextInt();
+                String line;
+                String[] lis;
+                while((line = br.readLine()) != null){  
+                        lis = line.split(" ");
+                        n = Integer.parseInt(lis[0]);
+                        main = Integer.parseInt(lis[1]);
 
                         
@@ -25,7 +28,10 @@
                         
                         for (int i = 0; i < n-1; i++) {
-                                a = sc.nextInt();
-                                b = sc.nextInt();
-                                v = sc.nextInt();
+                                line = br.readLine();
+                                lis = line.split(" ");
+                                
+                                a = Integer.parseInt(lis[0]);
+                                b = Integer.parseInt(lis[1]);
+                                v = Integer.parseInt(lis[2]);
                                 
                                 mat[a][b] = v; 
@@ -42,5 +48,5 @@
         
                 }
-                sc.close();
+                br.close();
         }