#include <stdlib.h> //---===--- #include <stdio.h> #include <unistd.h> typedef unsigned int uint; char buf_in[4096000]; int buf_in_head = 0; int buf_in_tail = 0; static int getChar(void) { while (buf_in_head == buf_in_tail) { int rv = read(0, buf_in, sizeof(buf_in)); if (rv > 0) { buf_in_head = 0; buf_in_tail = rv; break; }; if (rv == 0) return EOF; } return buf_in[buf_in_head++]; } // only 1 call is safe, and only if previously getChar() had been called. static void ungetChar(void) { --buf_in_head; } static uint getUInt() { uint v; int c; for (;;) { c = getChar(); if (c < '0') continue; if (c > '9') continue; v = c - '0'; break; }; for (;;) { c = getChar(); if (c < '0') break; if (c > '9') break; v *= 10; v += c - '0'; }; ungetChar(); return v; } //---===--- #define MAX_NM 200000 #define MAX_K 500000 int n, m; // 0 <= m < n <= 200,000 int k; // 0 <= k <= 500,000 typedef struct { int f; int g; // 1 <= g[i] <= 1,000,000,000 } tt; tt Tubes[MAX_NM]; struct rr; typedef struct rr { struct rr * next; int c, d; // 0 <= c[i], d[i] < n, c[i] != d[i] int idx; int tmin, tmax; } rr; rr Reactions[MAX_K + 1]; typedef struct { int a, b; // 0 <= a[i], b[i] < n, a[i] != b[i] rr * queries; rr * known; } mm; mm Merges[MAX_NM]; // merges static inline int cmp(const rr * a, const rr * b) { if (a->tmin < b->tmin) return -1; if (a->tmin > b->tmin) return +1; if (a->idx < b->idx) return -1; if (a->idx > b->idx) return +1; return 0; } int void_cmp(const void * a, const void * b) { return cmp((const rr*)a, (const rr*)b); } int Find(int x) { int y = x; // Find the ultimate parent while (Tubes[y].f >= 0) { y = Tubes[y].f; }; // y is now the ultimate parent // Update everyone to point at it while (Tubes[x].f >= 0) { int px = x; x = Tubes[x].f; Tubes[px].f = y; }; return y; } int Union(int x, int y) { x = Find(x); y = Find(y); if (x == y) return 0; // false - no union needed if (Tubes[x].f < Tubes[y].f) { // x has high rank (stored negative) Tubes[y].f = x; } else if (Tubes[x].f > Tubes[y].f) { // y has has high rank (stored negative) Tubes[x].f = y; } else { // Tubes[x].f == Tubes[y].f // equal ranks --Tubes[y].f; // rank increase Tubes[x].f = y; }; //--Fcnt; return 1; // true - non-trivial union performed } int main (void) { long long v = 0; int i; // 0 <= m < n <= 200,000 n = getUInt(); m = getUInt(); // 0 <= k <= 500,000 k = getUInt(); for (i = 0; i < n; ++i) { Tubes[i].g = getUInt(); // 1 <= g[i] <= 1,000,000,000 }; // a[i] shows up at most once, and once it does show up, never as b[j] for any j >= i for (i = 0; i < m; ++i) { Merges[i].a = getUInt() - 1; // 0 <= a[i] < n, a[i] != b[i] Merges[i].b = getUInt() - 1; // 0 <= b[i] < n Merges[i].queries = NULL; Merges[i].known = NULL; }; // unordered pair (c[i], d[i]) shows up at most once for (i = 0; i < k; ++i) { Reactions[i].next = (i + 1 < k) ? &Reactions[i + 1] : NULL; Reactions[i].c = getUInt() - 1; // 0 <= c[i] < n, c[i] != d[i] Reactions[i].d = getUInt() - 1; // 0 <= d[i] < n Reactions[i].idx = i; Reactions[i].tmin = 0; // Could react in [tmin, tmax), known to have already reacted at tmax Reactions[i].tmax = m; }; Merges[(0 + m) / 2].queries = &Reactions[0]; for (;;) { int unknowns = 0; //printf("tick\n"); // reset FnU for (i = 0; i < n; ++i) Tubes[i].f = -1; for (i = 0; i < m; ++i) { rr * q = Merges[i].queries; Merges[i].queries = NULL; //printf("%d %d+%d\n", i, Merges[i].a, Merges[i].b); Union(Merges[i].a, Merges[i].b); while (q) { int tmid; rr * qnext = q->next; q->next = NULL; if (Find(q->c) == Find(q->d)) { // has already reacted at time 'i' q->tmax = tmid = i; } else { // hasn't reacted at time 'i', could still react at 'i+1' q->tmin = tmid = i + 1; }; //printf("%d: %d-%d\n", q->idx, q->tmin, q->tmax); if (q->tmin == q->tmax) { // and one of them is tmid q->next = Merges[tmid].known; Merges[tmid].known = q; } else { ++unknowns; tmid = (q->tmin + q->tmax) / 2; q->next = Merges[tmid].queries; Merges[tmid].queries = q; }; q = qnext; }; }; if (unknowns == 0) break; }; qsort(Reactions, k, sizeof(rr), void_cmp); for (i = 0; i < k; ++i) { int c = Reactions[i].c; int d = Reactions[i].d; //int idx = Reactions[i].idx; int tmin = Reactions[i].tmin; if (tmin >= m) break; //int tmax = Reactions[i].tmax; //printf("%d->%d (%d) %d-%d\n", c, d, idx, tmin, tmax); if (Tubes[c].g < Tubes[d].g) { v += Tubes[c].g; Tubes[d].g -= Tubes[c].g; Tubes[c].g = 0; } else { v += Tubes[d].g; Tubes[c].g -= Tubes[d].g; Tubes[d].g = 0; }; }; printf("%lld\n", v * 2); return 0; }
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 | #include <stdlib.h> //---===--- #include <stdio.h> #include <unistd.h> typedef unsigned int uint; char buf_in[4096000]; int buf_in_head = 0; int buf_in_tail = 0; static int getChar(void) { while (buf_in_head == buf_in_tail) { int rv = read(0, buf_in, sizeof(buf_in)); if (rv > 0) { buf_in_head = 0; buf_in_tail = rv; break; }; if (rv == 0) return EOF; } return buf_in[buf_in_head++]; } // only 1 call is safe, and only if previously getChar() had been called. static void ungetChar(void) { --buf_in_head; } static uint getUInt() { uint v; int c; for (;;) { c = getChar(); if (c < '0') continue; if (c > '9') continue; v = c - '0'; break; }; for (;;) { c = getChar(); if (c < '0') break; if (c > '9') break; v *= 10; v += c - '0'; }; ungetChar(); return v; } //---===--- #define MAX_NM 200000 #define MAX_K 500000 int n, m; // 0 <= m < n <= 200,000 int k; // 0 <= k <= 500,000 typedef struct { int f; int g; // 1 <= g[i] <= 1,000,000,000 } tt; tt Tubes[MAX_NM]; struct rr; typedef struct rr { struct rr * next; int c, d; // 0 <= c[i], d[i] < n, c[i] != d[i] int idx; int tmin, tmax; } rr; rr Reactions[MAX_K + 1]; typedef struct { int a, b; // 0 <= a[i], b[i] < n, a[i] != b[i] rr * queries; rr * known; } mm; mm Merges[MAX_NM]; // merges static inline int cmp(const rr * a, const rr * b) { if (a->tmin < b->tmin) return -1; if (a->tmin > b->tmin) return +1; if (a->idx < b->idx) return -1; if (a->idx > b->idx) return +1; return 0; } int void_cmp(const void * a, const void * b) { return cmp((const rr*)a, (const rr*)b); } int Find(int x) { int y = x; // Find the ultimate parent while (Tubes[y].f >= 0) { y = Tubes[y].f; }; // y is now the ultimate parent // Update everyone to point at it while (Tubes[x].f >= 0) { int px = x; x = Tubes[x].f; Tubes[px].f = y; }; return y; } int Union(int x, int y) { x = Find(x); y = Find(y); if (x == y) return 0; // false - no union needed if (Tubes[x].f < Tubes[y].f) { // x has high rank (stored negative) Tubes[y].f = x; } else if (Tubes[x].f > Tubes[y].f) { // y has has high rank (stored negative) Tubes[x].f = y; } else { // Tubes[x].f == Tubes[y].f // equal ranks --Tubes[y].f; // rank increase Tubes[x].f = y; }; //--Fcnt; return 1; // true - non-trivial union performed } int main (void) { long long v = 0; int i; // 0 <= m < n <= 200,000 n = getUInt(); m = getUInt(); // 0 <= k <= 500,000 k = getUInt(); for (i = 0; i < n; ++i) { Tubes[i].g = getUInt(); // 1 <= g[i] <= 1,000,000,000 }; // a[i] shows up at most once, and once it does show up, never as b[j] for any j >= i for (i = 0; i < m; ++i) { Merges[i].a = getUInt() - 1; // 0 <= a[i] < n, a[i] != b[i] Merges[i].b = getUInt() - 1; // 0 <= b[i] < n Merges[i].queries = NULL; Merges[i].known = NULL; }; // unordered pair (c[i], d[i]) shows up at most once for (i = 0; i < k; ++i) { Reactions[i].next = (i + 1 < k) ? &Reactions[i + 1] : NULL; Reactions[i].c = getUInt() - 1; // 0 <= c[i] < n, c[i] != d[i] Reactions[i].d = getUInt() - 1; // 0 <= d[i] < n Reactions[i].idx = i; Reactions[i].tmin = 0; // Could react in [tmin, tmax), known to have already reacted at tmax Reactions[i].tmax = m; }; Merges[(0 + m) / 2].queries = &Reactions[0]; for (;;) { int unknowns = 0; //printf("tick\n"); // reset FnU for (i = 0; i < n; ++i) Tubes[i].f = -1; for (i = 0; i < m; ++i) { rr * q = Merges[i].queries; Merges[i].queries = NULL; //printf("%d %d+%d\n", i, Merges[i].a, Merges[i].b); Union(Merges[i].a, Merges[i].b); while (q) { int tmid; rr * qnext = q->next; q->next = NULL; if (Find(q->c) == Find(q->d)) { // has already reacted at time 'i' q->tmax = tmid = i; } else { // hasn't reacted at time 'i', could still react at 'i+1' q->tmin = tmid = i + 1; }; //printf("%d: %d-%d\n", q->idx, q->tmin, q->tmax); if (q->tmin == q->tmax) { // and one of them is tmid q->next = Merges[tmid].known; Merges[tmid].known = q; } else { ++unknowns; tmid = (q->tmin + q->tmax) / 2; q->next = Merges[tmid].queries; Merges[tmid].queries = q; }; q = qnext; }; }; if (unknowns == 0) break; }; qsort(Reactions, k, sizeof(rr), void_cmp); for (i = 0; i < k; ++i) { int c = Reactions[i].c; int d = Reactions[i].d; //int idx = Reactions[i].idx; int tmin = Reactions[i].tmin; if (tmin >= m) break; //int tmax = Reactions[i].tmax; //printf("%d->%d (%d) %d-%d\n", c, d, idx, tmin, tmax); if (Tubes[c].g < Tubes[d].g) { v += Tubes[c].g; Tubes[d].g -= Tubes[c].g; Tubes[c].g = 0; } else { v += Tubes[d].g; Tubes[c].g -= Tubes[d].g; Tubes[d].g = 0; }; }; printf("%lld\n", v * 2); return 0; } |