|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static int N; |
| 57 | + static long K; |
| 58 | + static List<int[]>[] graph; |
| 59 | + static long[] v; |
| 60 | + static PriorityQueue<Long>[] s; |
| 61 | + static int ans = 0; |
| 62 | + |
| 63 | + public static void main(String[] args) throws Exception { |
| 64 | + |
| 65 | + io = new IOController(); |
| 66 | + |
| 67 | + init(); |
| 68 | + solve(); |
| 69 | + |
| 70 | + io.close(); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + static void init() throws Exception { |
| 75 | + |
| 76 | + N = io.nextInt(); |
| 77 | + K = io.nextLong(); |
| 78 | + graph = new ArrayList[N+1]; |
| 79 | + v = new long[N+1]; |
| 80 | + s = new PriorityQueue[N+1]; |
| 81 | + for(int i=1;i<=N;i++) { |
| 82 | + graph[i] = new ArrayList<>(); |
| 83 | + s[i] = new PriorityQueue<>((a,b) -> Long.compare(b,a)); |
| 84 | + s[i].add(0L); |
| 85 | + } |
| 86 | + for(int i=1;i<N;i++) { |
| 87 | + int a = io.nextInt(); |
| 88 | + int b = io.nextInt(); |
| 89 | + int c = io.nextInt(); |
| 90 | + graph[a].add(new int[]{b,c}); |
| 91 | + graph[b].add(new int[]{a,c}); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + static void solve() throws Exception { |
| 97 | + |
| 98 | + dfs(1,0); |
| 99 | + io.write(ans + "\n"); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + static void dfs(int n, int p) { |
| 104 | + for(int[] e:graph[n]) if(e[0] != p) { |
| 105 | + int i = e[0], x = e[1]; |
| 106 | + dfs(i,n); |
| 107 | + if(s[i].size() > s[n].size()) { |
| 108 | + v[i] += x; |
| 109 | + for(long j:s[n]) s[i].add(j+v[n]-v[i]); |
| 110 | + s[n].clear(); |
| 111 | + s[n] = s[i]; |
| 112 | + v[n] = v[i]; |
| 113 | + } |
| 114 | + else { |
| 115 | + for(long j:s[i]) s[n].add(j+x+v[i]-v[n]); |
| 116 | + } |
| 117 | + } |
| 118 | + while(!s[n].isEmpty() && s[n].peek() + v[n] > K) s[n].poll(); |
| 119 | + ans = Math.max(ans, s[n].size()); |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | +``` |
0 commit comments