|
| 1 | +package class123; |
| 2 | + |
| 3 | +// 树上聚会后送k个人的总时间最少 |
| 4 | +// 测试链接 : https://www.luogu.com.cn/problem/P6419 |
| 5 | +// 提交以下的code,提交时请把类名改成"Main",可以通过所有用例 |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.io.OutputStreamWriter; |
| 11 | +import java.io.PrintWriter; |
| 12 | +import java.io.StreamTokenizer; |
| 13 | +import java.util.Arrays; |
| 14 | + |
| 15 | +public class Code05_Kamp { |
| 16 | + |
| 17 | + public static int MAXN = 500001; |
| 18 | + |
| 19 | + public static int n; |
| 20 | + |
| 21 | + public static int k; |
| 22 | + |
| 23 | + public static int[] people = new int[MAXN]; |
| 24 | + |
| 25 | + public static int[] head = new int[MAXN]; |
| 26 | + |
| 27 | + public static int[] next = new int[MAXN << 1]; |
| 28 | + |
| 29 | + public static int[] to = new int[MAXN << 1]; |
| 30 | + |
| 31 | + public static int[] weight = new int[MAXN << 1]; |
| 32 | + |
| 33 | + public static int cnt; |
| 34 | + |
| 35 | + public static int[] size = new int[MAXN]; |
| 36 | + |
| 37 | + public static long[] incost = new long[MAXN]; |
| 38 | + |
| 39 | + public static long[] outcost = new long[MAXN]; |
| 40 | + |
| 41 | + public static long[] infirst = new long[MAXN]; |
| 42 | + |
| 43 | + public static long[] insecond = new long[MAXN]; |
| 44 | + |
| 45 | + public static long[] outfirst = new long[MAXN]; |
| 46 | + |
| 47 | + public static void build() { |
| 48 | + cnt = 1; |
| 49 | + Arrays.fill(people, 1, n + 1, 0); |
| 50 | + Arrays.fill(head, 1, n + 1, 0); |
| 51 | + Arrays.fill(incost, 1, n + 1, 0); |
| 52 | + Arrays.fill(outcost, 1, n + 1, 0); |
| 53 | + Arrays.fill(infirst, 1, n + 1, 0); |
| 54 | + Arrays.fill(insecond, 1, n + 1, 0); |
| 55 | + Arrays.fill(outfirst, 1, n + 1, 0); |
| 56 | + } |
| 57 | + |
| 58 | + public static void addEdge(int u, int v, int w) { |
| 59 | + next[cnt] = head[u]; |
| 60 | + to[cnt] = v; |
| 61 | + weight[cnt] = w; |
| 62 | + head[u] = cnt++; |
| 63 | + } |
| 64 | + |
| 65 | + public static void dfs(int u, int f) { |
| 66 | + size[u] = people[u]; |
| 67 | + for (int e = head[u], v, w; e != 0; e = next[e]) { |
| 68 | + v = to[e]; |
| 69 | + w = weight[e]; |
| 70 | + if (v != f) { |
| 71 | + dfs(v, u); |
| 72 | + size[u] += size[v]; |
| 73 | + if (size[v] > 0) { |
| 74 | + incost[u] += incost[v] + (long) w * 2; |
| 75 | + if (infirst[u] < infirst[v] + w) { |
| 76 | + insecond[u] = infirst[u]; |
| 77 | + infirst[u] = infirst[v] + w; |
| 78 | + } else if (insecond[u] < infirst[v] + w) { |
| 79 | + insecond[u] = infirst[v] + w; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static void dp(int u, int f) { |
| 87 | + for (int e = head[u], v, w; e != 0; e = next[e]) { |
| 88 | + v = to[e]; |
| 89 | + w = weight[e]; |
| 90 | + if (v != f) { |
| 91 | + if (k - size[v] > 0) { |
| 92 | + outcost[v] = outcost[u] + (incost[u] - incost[v]); |
| 93 | + if (size[v] == 0) { |
| 94 | + outcost[v] += (long) w * 2; |
| 95 | + } |
| 96 | + if (infirst[v] + w == infirst[u]) { |
| 97 | + outfirst[v] = Math.max(outfirst[u], insecond[u]) + w; |
| 98 | + } else { |
| 99 | + outfirst[v] = Math.max(outfirst[u], infirst[u]) + w; |
| 100 | + } |
| 101 | + } |
| 102 | + dp(v, u); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public static void main(String[] args) throws IOException { |
| 108 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 109 | + StreamTokenizer in = new StreamTokenizer(br); |
| 110 | + PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); |
| 111 | + in.nextToken(); |
| 112 | + n = (int) in.nval; |
| 113 | + build(); |
| 114 | + in.nextToken(); |
| 115 | + k = (int) in.nval; |
| 116 | + for (int i = 1, u, v, w; i < n; i++) { |
| 117 | + in.nextToken(); |
| 118 | + u = (int) in.nval; |
| 119 | + in.nextToken(); |
| 120 | + v = (int) in.nval; |
| 121 | + in.nextToken(); |
| 122 | + w = (int) in.nval; |
| 123 | + addEdge(u, v, w); |
| 124 | + addEdge(v, u, w); |
| 125 | + } |
| 126 | + for (int i = 1, u; i <= k; i++) { |
| 127 | + in.nextToken(); |
| 128 | + u = (int) in.nval; |
| 129 | + people[u]++; |
| 130 | + } |
| 131 | + dfs(1, 0); |
| 132 | + dp(1, 0); |
| 133 | + for (int i = 1; i <= n; i++) { |
| 134 | + out.println(incost[i] + outcost[i] - Math.max(outfirst[i], infirst[i])); |
| 135 | + } |
| 136 | + out.flush(); |
| 137 | + out.close(); |
| 138 | + br.close(); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments