Source code for submission s881

Main.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package fr;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author kuric10
  13.  */
  14. public class Main {
  15.  
  16. /**
  17.   * @param args the command line arguments
  18.   */
  19. private static int hlavna, poc;
  20. private static ArrayList<int[]>[] pole;
  21. private static boolean[] pouzite;
  22.  
  23. public static void main(String[] args) {
  24. // TODO code application logic here
  25. String pom[];
  26. String s;
  27. int a, b;
  28. Scanner input = new Scanner(System.in);
  29. int[] pomPole;
  30. int kde;
  31. int pomi;
  32. while (input.hasNextLine()) {
  33. s = input.nextLine();
  34. pom = s.split(" ");
  35. hlavna = Integer.parseInt(pom[1]) - 1;
  36. poc = Integer.parseInt(pom[0]);
  37. pole = new ArrayList[poc];
  38. pouzite = new boolean[poc];
  39. for (int i = 0; i < poc; i++) {
  40. pole[i] = new ArrayList<>();
  41. }
  42. for (int i = 0; i < poc - 1; i++) {
  43. pomPole = new int[2];
  44. s = input.nextLine();
  45. pom = s.split(" ");
  46. a = Integer.parseInt(pom[1]) - 1;
  47. b = Integer.parseInt(pom[2]);
  48. pomPole[0] = a;
  49. pomPole[1] = b;
  50. kde = Integer.parseInt(pom[0]) - 1;
  51. /*if(pomPole[0]<kde){
  52.   pomi=kde;
  53.   kde=pomPole[0];
  54.   pomPole[0]=pomi;
  55.   }*/
  56. pole[kde].add(pomPole);
  57. pomPole = new int[2];
  58. pomPole[0] = kde;
  59. pomPole[1] = b;
  60. pole[a].add(pomPole);
  61. }
  62. // vypis();
  63. System.out.println(rek(hlavna, 9999999));
  64. }
  65.  
  66. }
  67.  
  68. private static int rek(int ja, int i) {
  69. //System.out.println(ja);
  70. pouzite[ja] = true;
  71. /*
  72.   if (pole[ja].isEmpty()) {
  73.   System.out.println(ja + " " + i);
  74.   return i;
  75.   }*/
  76. int sucet = 0;
  77. for (int[] is : pole[ja]) {
  78. // System.out.println(ja);
  79. if (pouzite[is[0]]) {
  80.  
  81. continue;
  82. }
  83. sucet += rek(is[0], is[1]);
  84. }
  85. if(sucet==0)return i;
  86. // System.out.println(ja + " " + Math.min(i, sucet));
  87. return Math.min(i, sucet);
  88. }
  89.  
  90. public static void vypis() {
  91. for (ArrayList<int[]> arrayList : pole) {
  92. for (int[] is : arrayList) {
  93. System.out.println(is[0]+" "+is[1]);
  94. }
  95. }
  96. }
  97. }
  98.