import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; public class fio { static Reaction[] tozero; static class Reaction implements Comparable<Reaction> { int first; int second; int affects; int priority; Reaction(int first, int second, int priority) { this.first = first; this.second = second; this.priority = priority; } Reaction sibling; Reaction next, previous; RList list; boolean removed = false; void remove() { if (this == list.head) list.head = next; if (this == list.tail) list.tail = previous; if (next != null) next.previous = previous; if (previous != null) previous.next = next; list.size--; if (this == tozero[this.first]) tozero[this.first] = next; } @Override public int compareTo(Reaction o) { return priority - o.priority; } } static class RList { int size = 0; Reaction head = null; Reaction tail = null; void add(Reaction r) { r.list = this; if (size == 0) { head = r; tail = r; tozero[r.first] = r; } else { r.previous = tail; tail.next = r; tail = r; } size++; } void append(RList a) { if (a.size > 0) { if (size == 0) { head = a.head; } else { tail.next = a.head; a.head.previous = tail; } tail = a.tail; size += a.size; } } } public static void main(String[] args) { long osad = 0; int n = R.i(); int m = R.i(); int k = R.i(); int[] g = new int[n + 1]; int[] whereis = new int[n + 1]; RList[] t = new RList[n + 1]; for (int i = 1; i <= n; i++) { g[i] = R.i(); whereis[i] = i; t[i] = new RList(); } int[] f = new int[2 * m]; for (int i = 0; i < m; i++) { f[2 * i] = R.i(); f[2 * i + 1] = R.i(); } int c, d; Reaction r1, r2; tozero = new Reaction[n + 1]; for (int i = 0; i < k; i++) { c = R.i(); d = R.i(); r1 = new Reaction(c, d, i); r2 = new Reaction(d, c, i); r1.affects = d; r2.affects = c; r1.sibling = r2; r2.sibling = r1; t[c].add(r1); t[d].add(r2); } int a, b, dest; int aa, bb; int o; Reaction r, delr; RList ar, br, destr; PriorityQueue<Reaction> react = new PriorityQueue<Reaction>(500000); for (int i = 0; i < m; i++) { aa = f[2 * i]; bb = f[2 * i + 1]; a = whereis[aa]; b = whereis[bb]; ar = t[a]; br = t[b]; react.clear(); if (ar.size < br.size) { r = ar.head; br.append(ar); destr = br; dest = b; } else { r = br.head; ar.append(br); destr = ar; dest = a; } whereis[bb] = dest; while (r != null) { r.list = destr; if (r.affects == dest) { react.add(r); r.remove(); r.sibling.remove(); } else { r.sibling.affects = dest; } r = r.next; } while (!react.isEmpty()) { r = react.poll(); c = r.first; d = r.second; o = Math.min(g[c], g[d]); if (o > 0) { osad += 2 * o; g[c] -= o; g[d] -= o; // usuwanie reakcji wyzerowanego if (g[c] == 0) { delr = tozero[c]; while (delr != null && delr.first == c) { delr.remove(); delr.sibling.remove(); delr = delr.next; } } if (g[d] == 0) { delr = tozero[d]; while (delr != null && delr.first == d) { delr.remove(); delr.sibling.remove(); delr = delr.next; } } } } } W.l(osad); W.flush(); } static class R { static InputStream is=new BufferedInputStream(System.in); static int b='\n'; static void next(){try{b=is.read();}catch(Exception e){}} static void skip(){while(b=='\n'||b==' '||b=='\r'||b=='\t')next();} static char c(){skip();char c=(char)b;next();return c;} static int i(){skip();int i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} static long l(){skip();long i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} } static class W { static OutputStream os=new BufferedOutputStream(System.out,4096); static void b(int b){try{os.write(b);}catch(Exception e){}} static void c(char c){b(c);} static void cs(char c){c(c);space();} static void cn(char c){c(c);newLine();} static void i(int i){if(i>9)i(i/10);b('0'+(i%10));} static void is(int i){i(i);space();} static void in(int i){i(i);newLine();} static void l(long l){if(l>9)l(l/10);b('0'+(int)(l%10));} static void ls(int l){l(l);space();} static void ln(int l){l(l);newLine();} static void s(String s){try{os.write(s.getBytes());}catch(Exception e){}} static void ss(String s){s(s);space();} static void sn(String s){s(s);newLine();} static void tn(boolean b){sn(b?"TAK":"NIE");} static void space(){b(' ');} static void newLine(){b('\n');} static void flush(){try{os.flush();}catch(Exception e){}}; } static class T { static List<String> s=new ArrayList<String>(); static List<Long> t=new ArrayList<Long>(); static void t(String n){s.add(n);t.add(System.currentTimeMillis());} static void err(){t.add(System.currentTimeMillis());System.err.println();for(int i=0;i<s.size();i++){System.err.println(s.get(i)+": "+(t.get(i+1)-t.get(i)));}System.err.println("TOTAL: "+(t.get(t.size() - 1)-t.get(0)));System.err.flush();} } }
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 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; public class fio { static Reaction[] tozero; static class Reaction implements Comparable<Reaction> { int first; int second; int affects; int priority; Reaction(int first, int second, int priority) { this.first = first; this.second = second; this.priority = priority; } Reaction sibling; Reaction next, previous; RList list; boolean removed = false; void remove() { if (this == list.head) list.head = next; if (this == list.tail) list.tail = previous; if (next != null) next.previous = previous; if (previous != null) previous.next = next; list.size--; if (this == tozero[this.first]) tozero[this.first] = next; } @Override public int compareTo(Reaction o) { return priority - o.priority; } } static class RList { int size = 0; Reaction head = null; Reaction tail = null; void add(Reaction r) { r.list = this; if (size == 0) { head = r; tail = r; tozero[r.first] = r; } else { r.previous = tail; tail.next = r; tail = r; } size++; } void append(RList a) { if (a.size > 0) { if (size == 0) { head = a.head; } else { tail.next = a.head; a.head.previous = tail; } tail = a.tail; size += a.size; } } } public static void main(String[] args) { long osad = 0; int n = R.i(); int m = R.i(); int k = R.i(); int[] g = new int[n + 1]; int[] whereis = new int[n + 1]; RList[] t = new RList[n + 1]; for (int i = 1; i <= n; i++) { g[i] = R.i(); whereis[i] = i; t[i] = new RList(); } int[] f = new int[2 * m]; for (int i = 0; i < m; i++) { f[2 * i] = R.i(); f[2 * i + 1] = R.i(); } int c, d; Reaction r1, r2; tozero = new Reaction[n + 1]; for (int i = 0; i < k; i++) { c = R.i(); d = R.i(); r1 = new Reaction(c, d, i); r2 = new Reaction(d, c, i); r1.affects = d; r2.affects = c; r1.sibling = r2; r2.sibling = r1; t[c].add(r1); t[d].add(r2); } int a, b, dest; int aa, bb; int o; Reaction r, delr; RList ar, br, destr; PriorityQueue<Reaction> react = new PriorityQueue<Reaction>(500000); for (int i = 0; i < m; i++) { aa = f[2 * i]; bb = f[2 * i + 1]; a = whereis[aa]; b = whereis[bb]; ar = t[a]; br = t[b]; react.clear(); if (ar.size < br.size) { r = ar.head; br.append(ar); destr = br; dest = b; } else { r = br.head; ar.append(br); destr = ar; dest = a; } whereis[bb] = dest; while (r != null) { r.list = destr; if (r.affects == dest) { react.add(r); r.remove(); r.sibling.remove(); } else { r.sibling.affects = dest; } r = r.next; } while (!react.isEmpty()) { r = react.poll(); c = r.first; d = r.second; o = Math.min(g[c], g[d]); if (o > 0) { osad += 2 * o; g[c] -= o; g[d] -= o; // usuwanie reakcji wyzerowanego if (g[c] == 0) { delr = tozero[c]; while (delr != null && delr.first == c) { delr.remove(); delr.sibling.remove(); delr = delr.next; } } if (g[d] == 0) { delr = tozero[d]; while (delr != null && delr.first == d) { delr.remove(); delr.sibling.remove(); delr = delr.next; } } } } } W.l(osad); W.flush(); } static class R { static InputStream is=new BufferedInputStream(System.in); static int b='\n'; static void next(){try{b=is.read();}catch(Exception e){}} static void skip(){while(b=='\n'||b==' '||b=='\r'||b=='\t')next();} static char c(){skip();char c=(char)b;next();return c;} static int i(){skip();int i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} static long l(){skip();long i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} } static class W { static OutputStream os=new BufferedOutputStream(System.out,4096); static void b(int b){try{os.write(b);}catch(Exception e){}} static void c(char c){b(c);} static void cs(char c){c(c);space();} static void cn(char c){c(c);newLine();} static void i(int i){if(i>9)i(i/10);b('0'+(i%10));} static void is(int i){i(i);space();} static void in(int i){i(i);newLine();} static void l(long l){if(l>9)l(l/10);b('0'+(int)(l%10));} static void ls(int l){l(l);space();} static void ln(int l){l(l);newLine();} static void s(String s){try{os.write(s.getBytes());}catch(Exception e){}} static void ss(String s){s(s);space();} static void sn(String s){s(s);newLine();} static void tn(boolean b){sn(b?"TAK":"NIE");} static void space(){b(' ');} static void newLine(){b('\n');} static void flush(){try{os.flush();}catch(Exception e){}}; } static class T { static List<String> s=new ArrayList<String>(); static List<Long> t=new ArrayList<Long>(); static void t(String n){s.add(n);t.add(System.currentTimeMillis());} static void err(){t.add(System.currentTimeMillis());System.err.println();for(int i=0;i<s.size();i++){System.err.println(s.get(i)+": "+(t.get(i+1)-t.get(i)));}System.err.println("TOTAL: "+(t.get(t.size() - 1)-t.get(0)));System.err.flush();} } } |