import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class roz { BufferedReader rd; roz() throws IOException { rd = new BufferedReader(new InputStreamReader(System.in)); compute(); } private void compute() throws IOException { int[] a = intarr(); int n = a[0]; int m = a[1]; List[] e = new List[n]; for(int i=0;i<e.length;i++) { e[i] = new ArrayList(1); } for(int i=0;i<n-1;i++) { a = intarr(); int u = a[0]-1; int v = a[1]-1; e[u].add(v); e[v].add(u); } final double[] lo = new double[n]; final double[] hi = new double[n]; int[] done = new int[n]; for(int i=0;i<m;i++) { lo[i] = pint(); hi[i] = lo[i]; } Set<Integer> ok = new HashSet<>(); if(m == n-1) { ok.add(m); } else { Set<Integer> nk = new HashSet<>(); for(int i=0;i<m;i++) { for(Integer v: (List<Integer>)e[i]) { if(v >= m) { if(!ok.contains(v) && !nk.contains(v)) { int cnt = 0; int cnt2 = 0; for(Integer u: (List<Integer>)e[v]) { if(u>=m) { cnt++; } else { cnt2++; } } done[v] = cnt2; if(cnt == 1) { ok.add(v); } else { nk.add(v); } } } } } } boolean[] p = new boolean[n]; for(int i=0;i<m;i++) { p[i] = true; } Comparator<Integer> byR = new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { return Double.compare(hi[a] + lo[a],hi[b] + lo[b]); } }; Queue<Integer> q = new ArrayDeque<>(ok); Integer v = 0; while(!q.isEmpty()) { v = q.poll(); List<Integer> ne = new ArrayList<>(); for(Integer u: (List<Integer>)e[v]) { if(p[u]) { ne.add(u); } else { done[u]++; if(e[u].size() - done[u] == 1) { q.add(u); } } } Integer[] arr = ne.toArray(new Integer[ne.size()]); Arrays.sort(arr, byR); if(arr.length%2==1) { int mid = arr.length/2; lo[v] = lo[arr[mid]]; hi[v] = hi[arr[mid]]; } else { int x = arr.length/2-1; int y = arr.length/2; double[] c = md(lo[arr[x]],hi[arr[x]],lo[arr[y]],hi[arr[y]]); lo[v] = c[0]; hi[v] = c[1]; } p[v] = true; } long minSum = Long.MAX_VALUE; Integer start = v; for(int i=0;i<2;i++) { double[] fn = new double[n]; fn[start] = i==0?lo[start]:hi[start]; p = new boolean[n]; q = new ArrayDeque<>(); q.add(start); double sum = 0; while(!q.isEmpty()) { v = q.poll(); p[v] = true; for(Integer u: (List<Integer>)e[v]) { if(!p[u]) { p[u] = true; if(fn[v] < lo[u]) { sum += lo[u] - fn[v]; fn[u] = lo[u]; } else if(fn[v] > hi[u]) { sum += fn[v] - hi[u]; fn[u] = hi[u]; } else { fn[u] = fn[v]; } q.add(u); } } } long cur = Math.round(Math.floor(sum+0.01)); minSum = Math.min(minSum, cur); } out(minSum); } private double[] md(double x1, double x2, double y1, double y2) { double[] res; if(x2 >= y1 && y2 >= x1) { if(x1 <= y1) { if(x2 >= y2) { res = new double[] { y1, y2 }; } else { res = new double[] { y1, x2 }; } } else { if(x2 < y2) { res = new double[] { x1, x2 }; } else { res = new double[] { x1, y2 }; } } } else if(x1 > y2) { res = new double[] { y2, x1 }; } else { res = new double[] { x2, y1 }; } return res; } private int pint() throws IOException { return pint(rd.readLine()); } private int pint(String s) { return Integer.parseInt(s); } private int[] intarr() throws IOException { return intarr(rd.readLine()); } private int[] intarr(String s) { String[] q = split(s); int n = q.length; int[] a = new int[n]; for(int i=0;i<n;i++) { a[i] = Integer.parseInt(q[i]); } return a; } public String[] split(String s) { if(s == null) { return new String[0]; } int n = s.length(); int start = -1; int end = 0; int sp = 0; boolean lastWhitespace = true; for(int i=0;i<n;i++) { char c = s.charAt(i); if(isWhitespace(c)) { lastWhitespace = true; } else { if(lastWhitespace) { sp++; } if(start == -1) { start = i; } end = i; lastWhitespace = false; } } if(start == -1) { return new String[0]; } String[] res = new String[sp]; int last = start; int x = 0; lastWhitespace = true; for(int i=start;i<=end;i++) { char c = s.charAt(i); boolean w = isWhitespace(c); if(w && !lastWhitespace) { res[x++] = s.substring(last,i); } else if(!w && lastWhitespace) { last = i; } lastWhitespace = w; } res[x] = s.substring(last,end+1); return res; } private boolean isWhitespace(char c) { return c==' ' || c=='\t'; } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new roz(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class roz { BufferedReader rd; roz() throws IOException { rd = new BufferedReader(new InputStreamReader(System.in)); compute(); } private void compute() throws IOException { int[] a = intarr(); int n = a[0]; int m = a[1]; List[] e = new List[n]; for(int i=0;i<e.length;i++) { e[i] = new ArrayList(1); } for(int i=0;i<n-1;i++) { a = intarr(); int u = a[0]-1; int v = a[1]-1; e[u].add(v); e[v].add(u); } final double[] lo = new double[n]; final double[] hi = new double[n]; int[] done = new int[n]; for(int i=0;i<m;i++) { lo[i] = pint(); hi[i] = lo[i]; } Set<Integer> ok = new HashSet<>(); if(m == n-1) { ok.add(m); } else { Set<Integer> nk = new HashSet<>(); for(int i=0;i<m;i++) { for(Integer v: (List<Integer>)e[i]) { if(v >= m) { if(!ok.contains(v) && !nk.contains(v)) { int cnt = 0; int cnt2 = 0; for(Integer u: (List<Integer>)e[v]) { if(u>=m) { cnt++; } else { cnt2++; } } done[v] = cnt2; if(cnt == 1) { ok.add(v); } else { nk.add(v); } } } } } } boolean[] p = new boolean[n]; for(int i=0;i<m;i++) { p[i] = true; } Comparator<Integer> byR = new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { return Double.compare(hi[a] + lo[a],hi[b] + lo[b]); } }; Queue<Integer> q = new ArrayDeque<>(ok); Integer v = 0; while(!q.isEmpty()) { v = q.poll(); List<Integer> ne = new ArrayList<>(); for(Integer u: (List<Integer>)e[v]) { if(p[u]) { ne.add(u); } else { done[u]++; if(e[u].size() - done[u] == 1) { q.add(u); } } } Integer[] arr = ne.toArray(new Integer[ne.size()]); Arrays.sort(arr, byR); if(arr.length%2==1) { int mid = arr.length/2; lo[v] = lo[arr[mid]]; hi[v] = hi[arr[mid]]; } else { int x = arr.length/2-1; int y = arr.length/2; double[] c = md(lo[arr[x]],hi[arr[x]],lo[arr[y]],hi[arr[y]]); lo[v] = c[0]; hi[v] = c[1]; } p[v] = true; } long minSum = Long.MAX_VALUE; Integer start = v; for(int i=0;i<2;i++) { double[] fn = new double[n]; fn[start] = i==0?lo[start]:hi[start]; p = new boolean[n]; q = new ArrayDeque<>(); q.add(start); double sum = 0; while(!q.isEmpty()) { v = q.poll(); p[v] = true; for(Integer u: (List<Integer>)e[v]) { if(!p[u]) { p[u] = true; if(fn[v] < lo[u]) { sum += lo[u] - fn[v]; fn[u] = lo[u]; } else if(fn[v] > hi[u]) { sum += fn[v] - hi[u]; fn[u] = hi[u]; } else { fn[u] = fn[v]; } q.add(u); } } } long cur = Math.round(Math.floor(sum+0.01)); minSum = Math.min(minSum, cur); } out(minSum); } private double[] md(double x1, double x2, double y1, double y2) { double[] res; if(x2 >= y1 && y2 >= x1) { if(x1 <= y1) { if(x2 >= y2) { res = new double[] { y1, y2 }; } else { res = new double[] { y1, x2 }; } } else { if(x2 < y2) { res = new double[] { x1, x2 }; } else { res = new double[] { x1, y2 }; } } } else if(x1 > y2) { res = new double[] { y2, x1 }; } else { res = new double[] { x2, y1 }; } return res; } private int pint() throws IOException { return pint(rd.readLine()); } private int pint(String s) { return Integer.parseInt(s); } private int[] intarr() throws IOException { return intarr(rd.readLine()); } private int[] intarr(String s) { String[] q = split(s); int n = q.length; int[] a = new int[n]; for(int i=0;i<n;i++) { a[i] = Integer.parseInt(q[i]); } return a; } public String[] split(String s) { if(s == null) { return new String[0]; } int n = s.length(); int start = -1; int end = 0; int sp = 0; boolean lastWhitespace = true; for(int i=0;i<n;i++) { char c = s.charAt(i); if(isWhitespace(c)) { lastWhitespace = true; } else { if(lastWhitespace) { sp++; } if(start == -1) { start = i; } end = i; lastWhitespace = false; } } if(start == -1) { return new String[0]; } String[] res = new String[sp]; int last = start; int x = 0; lastWhitespace = true; for(int i=start;i<=end;i++) { char c = s.charAt(i); boolean w = isWhitespace(c); if(w && !lastWhitespace) { res[x++] = s.substring(last,i); } else if(!w && lastWhitespace) { last = i; } lastWhitespace = w; } res[x] = s.substring(last,end+1); return res; } private boolean isWhitespace(char c) { return c==' ' || c=='\t'; } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new roz(); } } |