import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.Random; import java.util.Stack; import javax.management.RuntimeErrorException; import static java.lang.Math.*; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStreamReader; public class roz { static class Seg{ int left;int right; Seg(int a, int b) { left = a; right=b; } Seg(int x) { this(x,x); } long dist(int x) { if(x<left)return left -x; if(x>right)return x-right; return 0; } @Override public String toString() { return "["+left+", "+right+"]"; } } static int[] searchEqBrut(Seg[]A) { int minVal = Integer.MAX_VALUE; int maxVal = Integer.MIN_VALUE; for(Seg x:A){ minVal = min(minVal, x.left); maxVal = max(maxVal, x.right); } long bestSum = Long.MAX_VALUE; for(int x = minVal;x<=maxVal;x++) { long sum = dist(x,A); bestSum = min(sum,bestSum); } int[] res = new int[]{-1,-1}; for(int x = minVal;x<=maxVal;x++) { long sum = dist(x,A); if(sum==bestSum) { if(res[0]==-1)res[0]=x; res[1]=x; } } return res; } static long dist(int x, Seg[]A) { long sum = 0; for(Seg y:A)sum+=y.dist(x); return sum; } static class ev implements Comparable<ev>{ int p; boolean open; ev(int x, boolean b){ p=x;open =b; } @Override public int compareTo(ev e) { if(p!=e.p) { return p-e.p; } else { if(open == e.open)return 0; return open?-1:1; } } } static int[] searchSmart(Seg[]A) { int[]res = new int[]{-1,-1}; ev[]E = new ev[2*A.length]; for(int i=0;i<A.length;i++) { E[2*i]= new ev(A[i].left, true); E[2*i+1]= new ev(A[i].right, false); } Arrays.sort(E); int onLeft =0; int onRight = A.length; for(ev e:E) { if(onLeft == onRight) res[1]=e.p; if(e.open) { onRight--; } else { onLeft++; } if(onLeft==onRight)res[0]=e.p; } return res; } static Seg[] toSeg(int[]A) { Seg[]r = new Seg[A.length]; for(int i=0;i<r.length;i++)r[i]=new Seg(A[i]); return r; } public static void test(){ for(int t=0;;t++){ Random R = new Random(t); int n = R.nextInt(200); Seg[]A = new Seg[n]; for(int i=0;i<n;i++){ int a = R.nextInt(2000); int b = R.nextInt(2000); A[i]=new Seg(min(a,b), max(a,b)); } int[]r1 = searchEqBrut(A); int[]r2 = searchSmart(A); if(r1[0]!=r2[0] || r1[1]!=r2[1]){ throw new RuntimeException("error "+t); } else { System.out.println("OK "+t); } } } static class Node { int idx; Seg seg = null; Node(int i){idx=i;} long cost =0; HashSet<Node>edges= new HashSet<>(); ArrayList<Node>children= new ArrayList<>(); @Override public boolean equals(Object obj) { return ((Node)obj).idx == idx; } void connectUp() { Node p = edges.iterator().next(); p.children.add(this); // TODO: faster p.edges.remove(this); } Seg getSeg() { if(seg!=null)return seg; Seg[]A = new Seg[children.size()]; // TODO: iter? for(int i=0;i<A.length;i++){ A[i]=children.get(i).getSeg(); } int[] bounds = searchSmart(A); seg = new Seg(bounds[0], bounds[1]); cost = dist(bounds[0], A); for(Node n:children)cost+=n.cost; return seg; } @Override public int hashCode() { return idx; } } static long doit(BufferedReader br) throws Exception{ long stTime = System.currentTimeMillis(); String[]s = br.readLine().split(" "); int n = Integer.parseInt(s[0]);int m = Integer.parseInt(s[1]); Node[]G = new Node[n+1]; for(int i=1;i<=n;i++)G[i]= new Node(i); for(int i=0;i<n-1;i++) { s = br.readLine().split(" "); int a = Integer.parseInt(s[0]);int b = Integer.parseInt(s[1]); G[a].edges.add(G[b]); G[b].edges.add(G[a]); } for(int i=1;i<=m;i++){ int x = Integer.parseInt(br.readLine()); G[i].seg = new Seg(x,x); } if (n==2 && m==n) { return abs(G[1].seg.left - G[2].seg.left); } LinkedList<Node>toConnect = new LinkedList<>(); for(int i=1;i<=m;i++) { toConnect.add(G[i]); } while(toConnect.isEmpty() ==false) { Node nn = toConnect.removeFirst(); if(nn.edges.size()==0)continue; nn.connectUp(); nn.getSeg(); Node p = nn.edges.iterator().next(); if(p.seg==null && p.edges.size()==1) { toConnect.add(p); } } Node root = null; for(int i=1;i<=n;i++) { if(G[i].edges.size()>1)throw new RuntimeException("Not connected"); if(G[i].edges.size()==0){ if(root ==null)root = G[i]; else throw new RuntimeException("Two roots??"); } } Seg finalSeg = root.getSeg(); System.err.println(System.currentTimeMillis()-stTime); return root.cost; } public static void genLine() { System.out.println("500000 1"); for(int i=1;i<500000;i++)System.out.println(i+" "+(i+1)); System.out.println(42); } public static void genCater() { System.out.println("500000 499999"); for(int i=1;i<500000;i++)System.out.println(i+" 500000"); for(int i=1;i<500000;i++)System.out.println(i); } static void testKamil() throws Exception{ for(int i=1;i<=60;i++) { String n = (i<10?"0":"")+i; BufferedReader br = new BufferedReader(new FileReader("kamil/roz"+n+".in")); BufferedReader brout = new BufferedReader(new FileReader("kamil/roz"+n+".out")); long resKam = Long.parseLong(brout.readLine()); long res = doit(br); if(res==resKam)System.out.println("OK"); else System.out.println("ERRRORR "+i); } } public static void main(String[] args) throws Exception { //testKamil(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //new BufferedReader(new FileReader("cater.in")); System.out.println(doit(br)); //genCater(); } }
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 242 243 244 245 246 247 248 249 250 251 252 | import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.Random; import java.util.Stack; import javax.management.RuntimeErrorException; import static java.lang.Math.*; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStreamReader; public class roz { static class Seg{ int left;int right; Seg(int a, int b) { left = a; right=b; } Seg(int x) { this(x,x); } long dist(int x) { if(x<left)return left -x; if(x>right)return x-right; return 0; } @Override public String toString() { return "["+left+", "+right+"]"; } } static int[] searchEqBrut(Seg[]A) { int minVal = Integer.MAX_VALUE; int maxVal = Integer.MIN_VALUE; for(Seg x:A){ minVal = min(minVal, x.left); maxVal = max(maxVal, x.right); } long bestSum = Long.MAX_VALUE; for(int x = minVal;x<=maxVal;x++) { long sum = dist(x,A); bestSum = min(sum,bestSum); } int[] res = new int[]{-1,-1}; for(int x = minVal;x<=maxVal;x++) { long sum = dist(x,A); if(sum==bestSum) { if(res[0]==-1)res[0]=x; res[1]=x; } } return res; } static long dist(int x, Seg[]A) { long sum = 0; for(Seg y:A)sum+=y.dist(x); return sum; } static class ev implements Comparable<ev>{ int p; boolean open; ev(int x, boolean b){ p=x;open =b; } @Override public int compareTo(ev e) { if(p!=e.p) { return p-e.p; } else { if(open == e.open)return 0; return open?-1:1; } } } static int[] searchSmart(Seg[]A) { int[]res = new int[]{-1,-1}; ev[]E = new ev[2*A.length]; for(int i=0;i<A.length;i++) { E[2*i]= new ev(A[i].left, true); E[2*i+1]= new ev(A[i].right, false); } Arrays.sort(E); int onLeft =0; int onRight = A.length; for(ev e:E) { if(onLeft == onRight) res[1]=e.p; if(e.open) { onRight--; } else { onLeft++; } if(onLeft==onRight)res[0]=e.p; } return res; } static Seg[] toSeg(int[]A) { Seg[]r = new Seg[A.length]; for(int i=0;i<r.length;i++)r[i]=new Seg(A[i]); return r; } public static void test(){ for(int t=0;;t++){ Random R = new Random(t); int n = R.nextInt(200); Seg[]A = new Seg[n]; for(int i=0;i<n;i++){ int a = R.nextInt(2000); int b = R.nextInt(2000); A[i]=new Seg(min(a,b), max(a,b)); } int[]r1 = searchEqBrut(A); int[]r2 = searchSmart(A); if(r1[0]!=r2[0] || r1[1]!=r2[1]){ throw new RuntimeException("error "+t); } else { System.out.println("OK "+t); } } } static class Node { int idx; Seg seg = null; Node(int i){idx=i;} long cost =0; HashSet<Node>edges= new HashSet<>(); ArrayList<Node>children= new ArrayList<>(); @Override public boolean equals(Object obj) { return ((Node)obj).idx == idx; } void connectUp() { Node p = edges.iterator().next(); p.children.add(this); // TODO: faster p.edges.remove(this); } Seg getSeg() { if(seg!=null)return seg; Seg[]A = new Seg[children.size()]; // TODO: iter? for(int i=0;i<A.length;i++){ A[i]=children.get(i).getSeg(); } int[] bounds = searchSmart(A); seg = new Seg(bounds[0], bounds[1]); cost = dist(bounds[0], A); for(Node n:children)cost+=n.cost; return seg; } @Override public int hashCode() { return idx; } } static long doit(BufferedReader br) throws Exception{ long stTime = System.currentTimeMillis(); String[]s = br.readLine().split(" "); int n = Integer.parseInt(s[0]);int m = Integer.parseInt(s[1]); Node[]G = new Node[n+1]; for(int i=1;i<=n;i++)G[i]= new Node(i); for(int i=0;i<n-1;i++) { s = br.readLine().split(" "); int a = Integer.parseInt(s[0]);int b = Integer.parseInt(s[1]); G[a].edges.add(G[b]); G[b].edges.add(G[a]); } for(int i=1;i<=m;i++){ int x = Integer.parseInt(br.readLine()); G[i].seg = new Seg(x,x); } if (n==2 && m==n) { return abs(G[1].seg.left - G[2].seg.left); } LinkedList<Node>toConnect = new LinkedList<>(); for(int i=1;i<=m;i++) { toConnect.add(G[i]); } while(toConnect.isEmpty() ==false) { Node nn = toConnect.removeFirst(); if(nn.edges.size()==0)continue; nn.connectUp(); nn.getSeg(); Node p = nn.edges.iterator().next(); if(p.seg==null && p.edges.size()==1) { toConnect.add(p); } } Node root = null; for(int i=1;i<=n;i++) { if(G[i].edges.size()>1)throw new RuntimeException("Not connected"); if(G[i].edges.size()==0){ if(root ==null)root = G[i]; else throw new RuntimeException("Two roots??"); } } Seg finalSeg = root.getSeg(); System.err.println(System.currentTimeMillis()-stTime); return root.cost; } public static void genLine() { System.out.println("500000 1"); for(int i=1;i<500000;i++)System.out.println(i+" "+(i+1)); System.out.println(42); } public static void genCater() { System.out.println("500000 499999"); for(int i=1;i<500000;i++)System.out.println(i+" 500000"); for(int i=1;i<500000;i++)System.out.println(i); } static void testKamil() throws Exception{ for(int i=1;i<=60;i++) { String n = (i<10?"0":"")+i; BufferedReader br = new BufferedReader(new FileReader("kamil/roz"+n+".in")); BufferedReader brout = new BufferedReader(new FileReader("kamil/roz"+n+".out")); long resKam = Long.parseLong(brout.readLine()); long res = doit(br); if(res==resKam)System.out.println("OK"); else System.out.println("ERRRORR "+i); } } public static void main(String[] args) throws Exception { //testKamil(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //new BufferedReader(new FileReader("cater.in")); System.out.println(doit(br)); //genCater(); } } |