#include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define VDEBUG(...) #define DEBUG(...) //#define DEBUG(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) //#define VDEBUG(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) //#define SETIMPL unordered_set<int> //#define SETIMPL set<int> #define SETIMPL hash_table #define MAXN 200007 #define MAXM 200007 #define MAXK 500007 #define INF 1000000000 #define HASH(x, nbuckets) (x * 1442968193) & (nbuckets - 1) class hash_table { public: int nbits, nbuckets, nvalues, v; int *buckets; int *values; int *next; hash_table() { v = 0; nvalues = 0; nbuckets = 0; values = NULL; next = NULL; buckets = NULL; grow(); } ~hash_table() { free(buckets); free(values); free(next); } void grow() { nvalues = nvalues ? 2 * nvalues : 16; values = (int*) realloc(values, nvalues * sizeof(int)); next = (int*) realloc(next, nvalues * sizeof(int)); nbuckets = nbuckets ? 2 * nbuckets : 16; buckets = (int*) realloc(buckets, nbuckets * sizeof(int)); memset(buckets, 0, nbuckets * sizeof(int)); for (int i = 1; i <= v; i++) { int b = HASH(values[i], nbuckets); next[i] = buckets[b]; buckets[b] = i; } for (int i = 0; i < nbuckets; i++) { int j = buckets[i]; while (j) { j = next[j]; } } } void insert(int val) { if (v == nvalues - 1) grow(); v++; int b = HASH(val, nbuckets); values[v] = val; next[v] = buckets[b]; buckets[b] = v; } bool find(int val) { int b = HASH(val, nbuckets); int i = buckets[b]; while (i) { if (values[i] == val) { return true; } i = next[i]; } return false; } int size() { return v; } }; int N, M, K, Rcnt; int G[MAXN]; int A[MAXM], B[MAXM]; SETIMPL *F[MAXN]; struct reaction { int c, d; int id; } R[MAXK], Rfinal[MAXK]; bool rcmp(const reaction &a, const reaction &b) { return a.id < b.id; } int main() { int i; long long cnt = 0; scanf("%d %d %d", &N, &M, &K); for (i = 0; i < N; i++) scanf("%d", &G[i+1]); for (i = 0; i < M; i++) scanf("%d %d", &A[i], &B[i]); for (i = 0; i < K; i++) { scanf("%d %d", &R[i].c, &R[i].d); R[i].id = i; if (!F[R[i].c]) F[R[i].c] = new SETIMPL(); if (!F[R[i].d]) F[R[i].d] = new SETIMPL(); F[R[i].c]->insert(i); F[R[i].d]->insert(i); } Rcnt = 0; for (i = 0; i < M; i++) { SETIMPL *from, *to; VDEBUG("[%d %d]\n", A[i], B[i]); if (!F[A[i]]) continue; if (!F[B[i]]) { F[B[i]] = F[A[i]]; continue; } if (F[A[i]]->size() < F[B[i]]->size()) { from = F[A[i]]; to = F[B[i]]; } else { from = F[B[i]]; to = F[A[i]]; } VDEBUG("(%d: %d %d)\n", i, from->size(), to->size()); cnt += from->size(); int Rstart = Rcnt; #if 0 SETIMPL::iterator it, hint = to->begin();; for (it = from->begin(); it != from->end(); it++) { pair<SETIMPL::iterator,bool> r = to->insert(*it); if (r.second == false) { Rfinal[Rcnt++] = R[from->values[j]]; to->erase(r.first); } } #else for (int j = 1; j <= from->v; j++) { if (to->find(from->values[j])) { Rfinal[Rcnt++] = R[from->values[j]]; } else { to->insert(from->values[j]); } } #endif if (Rcnt - Rstart > 1) sort(Rfinal + Rstart, Rfinal + Rcnt, rcmp); delete from; F[A[i]] = NULL; F[B[i]] = to; } long long ret = 0; for (i = 0; i < Rcnt; i++) { int osad; VDEBUG("%d %d %d %d\n", Rfinal[i].c, Rfinal[i].d, Rfinal[i].t, Rfinal[i].id); osad = min(G[Rfinal[i].c], G[Rfinal[i].d]); ret += (long long) osad; G[Rfinal[i].c] -= osad; G[Rfinal[i].d] -= osad; } DEBUG("cnt = %lld\n", cnt); printf("%lld\n", 2LL*ret); 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 | #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define VDEBUG(...) #define DEBUG(...) //#define DEBUG(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) //#define VDEBUG(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) //#define SETIMPL unordered_set<int> //#define SETIMPL set<int> #define SETIMPL hash_table #define MAXN 200007 #define MAXM 200007 #define MAXK 500007 #define INF 1000000000 #define HASH(x, nbuckets) (x * 1442968193) & (nbuckets - 1) class hash_table { public: int nbits, nbuckets, nvalues, v; int *buckets; int *values; int *next; hash_table() { v = 0; nvalues = 0; nbuckets = 0; values = NULL; next = NULL; buckets = NULL; grow(); } ~hash_table() { free(buckets); free(values); free(next); } void grow() { nvalues = nvalues ? 2 * nvalues : 16; values = (int*) realloc(values, nvalues * sizeof(int)); next = (int*) realloc(next, nvalues * sizeof(int)); nbuckets = nbuckets ? 2 * nbuckets : 16; buckets = (int*) realloc(buckets, nbuckets * sizeof(int)); memset(buckets, 0, nbuckets * sizeof(int)); for (int i = 1; i <= v; i++) { int b = HASH(values[i], nbuckets); next[i] = buckets[b]; buckets[b] = i; } for (int i = 0; i < nbuckets; i++) { int j = buckets[i]; while (j) { j = next[j]; } } } void insert(int val) { if (v == nvalues - 1) grow(); v++; int b = HASH(val, nbuckets); values[v] = val; next[v] = buckets[b]; buckets[b] = v; } bool find(int val) { int b = HASH(val, nbuckets); int i = buckets[b]; while (i) { if (values[i] == val) { return true; } i = next[i]; } return false; } int size() { return v; } }; int N, M, K, Rcnt; int G[MAXN]; int A[MAXM], B[MAXM]; SETIMPL *F[MAXN]; struct reaction { int c, d; int id; } R[MAXK], Rfinal[MAXK]; bool rcmp(const reaction &a, const reaction &b) { return a.id < b.id; } int main() { int i; long long cnt = 0; scanf("%d %d %d", &N, &M, &K); for (i = 0; i < N; i++) scanf("%d", &G[i+1]); for (i = 0; i < M; i++) scanf("%d %d", &A[i], &B[i]); for (i = 0; i < K; i++) { scanf("%d %d", &R[i].c, &R[i].d); R[i].id = i; if (!F[R[i].c]) F[R[i].c] = new SETIMPL(); if (!F[R[i].d]) F[R[i].d] = new SETIMPL(); F[R[i].c]->insert(i); F[R[i].d]->insert(i); } Rcnt = 0; for (i = 0; i < M; i++) { SETIMPL *from, *to; VDEBUG("[%d %d]\n", A[i], B[i]); if (!F[A[i]]) continue; if (!F[B[i]]) { F[B[i]] = F[A[i]]; continue; } if (F[A[i]]->size() < F[B[i]]->size()) { from = F[A[i]]; to = F[B[i]]; } else { from = F[B[i]]; to = F[A[i]]; } VDEBUG("(%d: %d %d)\n", i, from->size(), to->size()); cnt += from->size(); int Rstart = Rcnt; #if 0 SETIMPL::iterator it, hint = to->begin();; for (it = from->begin(); it != from->end(); it++) { pair<SETIMPL::iterator,bool> r = to->insert(*it); if (r.second == false) { Rfinal[Rcnt++] = R[from->values[j]]; to->erase(r.first); } } #else for (int j = 1; j <= from->v; j++) { if (to->find(from->values[j])) { Rfinal[Rcnt++] = R[from->values[j]]; } else { to->insert(from->values[j]); } } #endif if (Rcnt - Rstart > 1) sort(Rfinal + Rstart, Rfinal + Rcnt, rcmp); delete from; F[A[i]] = NULL; F[B[i]] = to; } long long ret = 0; for (i = 0; i < Rcnt; i++) { int osad; VDEBUG("%d %d %d %d\n", Rfinal[i].c, Rfinal[i].d, Rfinal[i].t, Rfinal[i].id); osad = min(G[Rfinal[i].c], G[Rfinal[i].d]); ret += (long long) osad; G[Rfinal[i].c] -= osad; G[Rfinal[i].d] -= osad; } DEBUG("cnt = %lld\n", cnt); printf("%lld\n", 2LL*ret); return 0; } |