Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5. public static class Edge {
  6. public int j, x;
  7. public Edge(int j, int x) {
  8. this.j = j;
  9. this.x = x;
  10. }
  11. }
  12. public static PrintWriter out;
  13. public static ArrayList<Edge>[] adjList;
  14. public static int n, q;
  15. public static int MAX = 103;
  16. public static int[][] dp = new int[MAX][MAX];
  17.  
  18. public static void main(String[] args) {
  19. MyScanner sc = new MyScanner();
  20. out = new PrintWriter(new BufferedOutputStream(System.out));
  21. n = sc.nextInt();
  22. adjList = (ArrayList<Edge>[])new ArrayList[n + 1];
  23.  
  24. for (int i = 0; i < n + 1; i++)
  25. adjList[i] = new ArrayList<Edge>();
  26.  
  27. q = sc.nextInt();
  28. for (int l = 0; l < n - 1; l++) {
  29. int i = sc.nextInt();
  30. int j = sc.nextInt();
  31. int x = sc.nextInt();
  32. adjList[i].add(new Edge(j, x));
  33. adjList[j].add(new Edge(i, x));
  34. }
  35. for (int i = 0; i < MAX; i++)
  36. Arrays.fill(dp[i], -1);
  37.  
  38. out.println(dfs(1, q));
  39.  
  40. out.close();
  41. }
  42.  
  43. public static int dfs(int i, int q) {
  44. if (i >= MAX || i < 0 || q >= MAX || q < 0) return Integer.MIN_VALUE;
  45. if (dp[i][q] != -1) return dp[i][q];
  46. if (q == 0) return dp[i][q] = 0;
  47.  
  48. if (adjList[i].size() == 0) { // Just a leaf node
  49. return Integer.MIN_VALUE;
  50. }
  51.  
  52. Edge left = adjList[i].get(0);
  53. int case1 = left.x + dfs(left.j, q - 1);
  54. if (adjList[i].size() == 1) { // Has just a left/right child
  55. return dp[i][q] = case1;
  56. }
  57.  
  58. Edge right = adjList[i].get(1);
  59. int case2 = right.x + dfs(right.j, q - 1);
  60.  
  61. // If we've gotten here, we have both children nodes, so we can check all 3 cases
  62. int case3 = Math.max(
  63. left.x + dfs(left.j, q - 2),
  64. right.x + dfs(right.j, q - 2));
  65.  
  66. return dp[i][q] = Math.max(case1, Math.max(case2, case3));
  67. }
  68.  
  69.  
  70. public static class MyScanner {
  71. BufferedReader br;
  72. StringTokenizer st;
  73.  
  74. public MyScanner() {
  75. br = new BufferedReader(new InputStreamReader(System.in));
  76. }
  77.  
  78. public boolean hasNext() {
  79. try {
  80. boolean a = br.ready();
  81. return a;
  82. } catch (IOException e) {
  83. return false;
  84. }
  85. }
  86.  
  87. public String next() {
  88. while (st == null || !st.hasMoreElements()) {
  89. try {
  90. st = new StringTokenizer(br.readLine());
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. return st.nextToken();
  96. }
  97.  
  98. public int nextInt() {
  99. return Integer.parseInt(next());
  100. }
  101.  
  102. public long nextLong() {
  103. return Long.parseLong(next());
  104. }
  105.  
  106. public double nextDouble() {
  107. return Double.parseDouble(next());
  108. }
  109.  
  110. public char nextChar() {
  111. return next().charAt(0);
  112. }
  113.  
  114. public String nextLine() {
  115. String str = "";
  116. try {
  117. str = br.readLine();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. return str;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement