import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import java.util.StringTokenizer; public class fio { public static void main(String[] args) throws IOException { fio solver = new fio(); solver.init(); solver.solve(); } private void init() { } private static class Node { Node left; Node right; Node parent; int g; long ord = -1; int idx; int elems; public Node(int g) { super(); this.g = g; elems = 1; } int getLCALength() { return (elems - elems/2) + 3 * (elems/2); } } private static class Pair implements Comparable<Pair> { int c; int d; long lca = -1; int ord; public Pair(int c, int d, int ord) { super(); if (c < d) { this.c = c; this.d = d; } else { this.c = d; this.d = c; } this.ord = ord; } @Override public int compareTo(Pair o) { //im wiekszy lca tym wczesniej w kolekcji if (this.lca == o.lca) { return this.ord - o.ord; } else { return (this.lca < o.lca) ? 1 : -1; } } } final static int ORDINAL_GROUP = 1000000; final static int H = 20; long[] lcaTree = new long[1 << (H + 1)]; void initTree(long[] lca) { // System.out.println(1 << H); // System.out.println("LCA length: " + lca.length); // System.out.println("LCATREE length: " + lcaTree.length); // System.out.println((lca.length - 1) | (1 << H)); // for (int i = 0; i < lca.length; i++) lcaTree[i | (1 << H)] = lca[i]; for (int i = (1 << H) - 1; i > 0; i--) lcaTree[i] = Math.min(lcaTree[i << 1], lcaTree[(i << 1) + 1]); } private long findLca(int from, int to) { from |= 1 << H; to |= 1 << H; long r = Math.min(lcaTree[from], lcaTree[to]); while ((from >> 1) != (to >> 1)) { if ((from & 1) == 0) r = Math.min(r, lcaTree[from + 1]); if ((to & 1) == 1) r = Math.min(r, lcaTree[to - 1]); from >>= 1; to >>= 1; } return r; } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); Node[] phials = new Node[n]; for (int i = 0; i < n; i++) { phials[i] = new Node(in.nextInt()); } buildGraph(phials, in, m); Pair[] pairs = new Pair[k]; for (int i = 0; i < pairs.length; i++) { pairs[i] = new Pair(in.nextInt() - 1, in.nextInt() - 1, i); } //wezly wewnetrzne wystapia 3-krotnie, liscie 1-krotnie long[] lca = new long[n + 3 * m]; int idx = 0; //zbuduj tablice, long ord = -ORDINAL_GROUP; for (int i = 0; i < n; i++) { if (phials[i].ord == -1) { ord += ORDINAL_GROUP; Node root = findRoot(phials[i]); countLca(root, ord, idx, lca); idx += root.getLCALength(); } } initTree(lca); for(Pair p : pairs) { if (phials[p.c].ord/ORDINAL_GROUP == phials[p.d].ord/ORDINAL_GROUP) { p.lca = findLca(phials[p.c].idx, phials[p.d].idx); } } Arrays.sort(pairs); long r = 0; for (Pair p : pairs) if (p.lca > -1) { int residue = Math.min(phials[p.c].g, phials[p.d].g); phials[p.c].g -= residue; phials[p.d].g -= residue; r += residue << 1; } out.println(r); out.flush(); out.close(); } private void countLca(Node root, long ord, int idx, long[] lca) { final class Item { Node node; int idx; long ord; public Item(Node node, long ord, int idx) { super(); this.node = node; this.idx = idx; this.ord = ord; } } LinkedList<Item> queue = new LinkedList<Item>(); queue.add(new Item(root, ord,idx)); while (!queue.isEmpty()) { Item item = queue.poll(); item.node.ord = item.ord; //int elems = 0; lca[item.idx] = item.node.ord; item.node.idx = item.idx; if (item.node.left != null) { queue.add(new Item(item.node.left, item.ord + 1, item.idx + 1)); lca[item.idx + 1 + item.node.left.getLCALength()] = item.node.ord; //elems= item.node.left.elems; queue.add(new Item(item.node.right, item.ord + 1 + item.node.left.elems, item.idx + 1 + item.node.left.getLCALength() + 1)); lca[item.idx + item.node.getLCALength() - 1] = item.node.ord; } //lca[item.idx + elems] = item.node.ord; //item.node.idx = item.idx + elems; } } private Node findRoot(Node node) { while (node.parent != null) { node = node.parent; } return node; } private void buildGraph(Node[] phials, Reader in, int m) throws IOException { Node[] nodes = new Node[phials.length]; for (int i = 0; i < nodes.length; i++) nodes[i] = phials[i]; for (int i = 0; i < m; i++) { int a = in.nextInt() - 1; int b = in.nextInt() - 1; Node n = new Node(-1); if (a < b) { n.left = nodes[a]; n.right = nodes[b]; } else { n.left = nodes[b]; n.right = nodes[a]; } nodes[a].parent = n; nodes[b].parent = n; nodes[b] = n; n.elems = 1 + n.left.elems + n.right.elems; } } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import java.util.StringTokenizer; public class fio { public static void main(String[] args) throws IOException { fio solver = new fio(); solver.init(); solver.solve(); } private void init() { } private static class Node { Node left; Node right; Node parent; int g; long ord = -1; int idx; int elems; public Node(int g) { super(); this.g = g; elems = 1; } int getLCALength() { return (elems - elems/2) + 3 * (elems/2); } } private static class Pair implements Comparable<Pair> { int c; int d; long lca = -1; int ord; public Pair(int c, int d, int ord) { super(); if (c < d) { this.c = c; this.d = d; } else { this.c = d; this.d = c; } this.ord = ord; } @Override public int compareTo(Pair o) { //im wiekszy lca tym wczesniej w kolekcji if (this.lca == o.lca) { return this.ord - o.ord; } else { return (this.lca < o.lca) ? 1 : -1; } } } final static int ORDINAL_GROUP = 1000000; final static int H = 20; long[] lcaTree = new long[1 << (H + 1)]; void initTree(long[] lca) { // System.out.println(1 << H); // System.out.println("LCA length: " + lca.length); // System.out.println("LCATREE length: " + lcaTree.length); // System.out.println((lca.length - 1) | (1 << H)); // for (int i = 0; i < lca.length; i++) lcaTree[i | (1 << H)] = lca[i]; for (int i = (1 << H) - 1; i > 0; i--) lcaTree[i] = Math.min(lcaTree[i << 1], lcaTree[(i << 1) + 1]); } private long findLca(int from, int to) { from |= 1 << H; to |= 1 << H; long r = Math.min(lcaTree[from], lcaTree[to]); while ((from >> 1) != (to >> 1)) { if ((from & 1) == 0) r = Math.min(r, lcaTree[from + 1]); if ((to & 1) == 1) r = Math.min(r, lcaTree[to - 1]); from >>= 1; to >>= 1; } return r; } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); Node[] phials = new Node[n]; for (int i = 0; i < n; i++) { phials[i] = new Node(in.nextInt()); } buildGraph(phials, in, m); Pair[] pairs = new Pair[k]; for (int i = 0; i < pairs.length; i++) { pairs[i] = new Pair(in.nextInt() - 1, in.nextInt() - 1, i); } //wezly wewnetrzne wystapia 3-krotnie, liscie 1-krotnie long[] lca = new long[n + 3 * m]; int idx = 0; //zbuduj tablice, long ord = -ORDINAL_GROUP; for (int i = 0; i < n; i++) { if (phials[i].ord == -1) { ord += ORDINAL_GROUP; Node root = findRoot(phials[i]); countLca(root, ord, idx, lca); idx += root.getLCALength(); } } initTree(lca); for(Pair p : pairs) { if (phials[p.c].ord/ORDINAL_GROUP == phials[p.d].ord/ORDINAL_GROUP) { p.lca = findLca(phials[p.c].idx, phials[p.d].idx); } } Arrays.sort(pairs); long r = 0; for (Pair p : pairs) if (p.lca > -1) { int residue = Math.min(phials[p.c].g, phials[p.d].g); phials[p.c].g -= residue; phials[p.d].g -= residue; r += residue << 1; } out.println(r); out.flush(); out.close(); } private void countLca(Node root, long ord, int idx, long[] lca) { final class Item { Node node; int idx; long ord; public Item(Node node, long ord, int idx) { super(); this.node = node; this.idx = idx; this.ord = ord; } } LinkedList<Item> queue = new LinkedList<Item>(); queue.add(new Item(root, ord,idx)); while (!queue.isEmpty()) { Item item = queue.poll(); item.node.ord = item.ord; //int elems = 0; lca[item.idx] = item.node.ord; item.node.idx = item.idx; if (item.node.left != null) { queue.add(new Item(item.node.left, item.ord + 1, item.idx + 1)); lca[item.idx + 1 + item.node.left.getLCALength()] = item.node.ord; //elems= item.node.left.elems; queue.add(new Item(item.node.right, item.ord + 1 + item.node.left.elems, item.idx + 1 + item.node.left.getLCALength() + 1)); lca[item.idx + item.node.getLCALength() - 1] = item.node.ord; } //lca[item.idx + elems] = item.node.ord; //item.node.idx = item.idx + elems; } } private Node findRoot(Node node) { while (node.parent != null) { node = node.parent; } return node; } private void buildGraph(Node[] phials, Reader in, int m) throws IOException { Node[] nodes = new Node[phials.length]; for (int i = 0; i < nodes.length; i++) nodes[i] = phials[i]; for (int i = 0; i < m; i++) { int a = in.nextInt() - 1; int b = in.nextInt() - 1; Node n = new Node(-1); if (a < b) { n.left = nodes[a]; n.right = nodes[b]; } else { n.left = nodes[b]; n.right = nodes[a]; } nodes[a].parent = n; nodes[b].parent = n; nodes[b] = n; n.elems = 1 + n.left.elems + n.right.elems; } } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } } |