Source code for submission s919

Fr.java

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Fr {
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. while(sc.hasNextInt()){
  12. int n = sc.nextInt();
  13. int c = sc.nextInt();
  14. Node[] node = new Node[n + 1];
  15. ArrayList<Edge> edge = new ArrayList<Edge>();
  16. for(int i = 0; i < n - 1; i++){
  17. int u = sc.nextInt();
  18. int v = sc.nextInt();
  19. int w = sc.nextInt();
  20. Edge e = new Edge();
  21. if(node[u] == null){
  22. node[u] = new Node();
  23. }
  24. if(node[v] == null){
  25. node[v] = new Node();
  26. }
  27. e.a = node[v];
  28. e.b = node[u];
  29. e.cena = w;
  30. node[u].hrany.add(e);
  31. node[v].hrany.add(e);
  32.  
  33. }
  34. System.out.println(node[c].initiate());
  35. }
  36.  
  37. }
  38.  
  39. }
  40.  
  41. class Node {
  42. LinkedList<Edge> hrany;
  43.  
  44. public Node()
  45. {
  46. hrany = new LinkedList<Edge>();
  47. }
  48.  
  49. int initiate(){
  50. int res = 0;
  51. for(Edge e: hrany){
  52. if(e.a == this){
  53. res += e.b.compute(e);
  54. } else {
  55. res += e.a.compute(e);
  56. }
  57. }
  58. return res;
  59. }
  60.  
  61. int compute(Edge edge){
  62. if (hrany.size() == 1)
  63. return edge.cena;
  64. int res = 0;
  65. for(Edge e: hrany){
  66. if(e != edge)
  67. {
  68. if (this == e.a){
  69. res += e.b.compute(e);
  70. } else {
  71. res += e.a.compute(e);
  72. }
  73. }
  74. }
  75. return Math.min(res, edge.cena);
  76. }
  77. }
  78.  
  79. class Edge {
  80. int cena;
  81. Node a, b;
  82. }