Go to diff to previous submission
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Fr { int n; int main; int a; int b; int v; String line; while((line = br.readLine()) != null){ lis = line.split(" "); int[][] mat = new int[n+1][n+1]; for (int i = 0; i < n-1; i++) { line = br.readLine(); lis = line.split(" "); mat[a][b] = v; mat[b][a] = v; } int sumV = 0; for (int i = 1; i < n+1; i++) { if (i != main && mat[main][i] != 0){ sumV += getValueS(mat, i, main); } } } br.close(); } static int getValueS(int[][] mat, int s, int r){ int sumV = 0; for (int i = 1; i < mat.length; i++) { if (i != r && mat[i][s] != 0){ sumV += getValueS(mat, i ,s); } } if (sumV != 0 && sumV < mat[r][s]){ return sumV; } return mat[r][s]; } }
--- 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(); }