#include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <deque> #include <list> #include <set> #include <map> #include <cstdlib> #include <cassert> #include <cmath> #include <cstdarg> using namespace std; #define ALL(x) (x).begin(), (x).end() #ifdef DBG #define DBGprint(format, ...) do { fprintf(stderr, "[%s:%d] " format "\n", __func__, __LINE__, ## __VA_ARGS__); } while (0) #else #define DBGprint(...) do { } while (0) #endif typedef long double LD; typedef double DL; typedef long long LL; typedef unsigned long long ULL; typedef pair<LD,LD> PLD; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; void memcheck() { #ifdef DBG char c[100]; FILE *p = fopen("/proc/self/status","r"); while (fgets(c,100,p) ) if (strncmp("VmPeak",c,6)==0) fputs(c, stderr); fclose(p); #endif } //------ /headers -------- int n, m, k; VI tubes; LL sediment; VPII unions; VPII reactions; typedef set<int> set_t; vector<pair<int, set_t>> parent; int find(int x) { if (x != parent[x].first) { parent[x].first = find(parent[x].first); } return parent[x].first; } void onion(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } // DBGprint("unitING x %d y %d", x, y); // for (int i : parent[x].second) { // DBGprint("set[x]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } // for (int i : parent[y].second) { // DBGprint("set[y]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } if (parent[x].second.size() > parent[y].second.size()) { swap(x, y); } for (int w : parent[x].second) { // musimy przelatywac od najmniejszych auto it = parent[y].second.find(w); if (it == parent[y].second.end()) { parent[y].second.insert(w); } else { // osad! int s = min(tubes[reactions[w].first], tubes[reactions[w].second]); DBGprint("while uniting %d %d found %d (sedim %d)", x, y, w, s); tubes[reactions[w].first] -= s; tubes[reactions[w].second] -= s; sediment += 2 * s; } } parent[x].first = y; parent[x].second.clear(); // DBGprint("united x %d y %d", x, y); // for (int i : parent[x].second) { // DBGprint("set[x]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } // for (int i : parent[y].second) { // DBGprint("set[y]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; ++i) { int tmp; scanf("%d", &tmp); tubes.push_back(tmp); parent.emplace_back(i, set_t()); } for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); unions.emplace_back(a - 1, b - 1); } for (int i = 0; i < k; ++i) { int a, b; scanf("%d%d", &a, &b); --a; --b; parent[a].second.insert(i); parent[b].second.insert(i); reactions.emplace_back(a, b); } for (const PII& uni : unions) { onion(uni.first, uni.second); } printf("%lld\n", sediment); memcheck(); }
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 | #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <deque> #include <list> #include <set> #include <map> #include <cstdlib> #include <cassert> #include <cmath> #include <cstdarg> using namespace std; #define ALL(x) (x).begin(), (x).end() #ifdef DBG #define DBGprint(format, ...) do { fprintf(stderr, "[%s:%d] " format "\n", __func__, __LINE__, ## __VA_ARGS__); } while (0) #else #define DBGprint(...) do { } while (0) #endif typedef long double LD; typedef double DL; typedef long long LL; typedef unsigned long long ULL; typedef pair<LD,LD> PLD; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; void memcheck() { #ifdef DBG char c[100]; FILE *p = fopen("/proc/self/status","r"); while (fgets(c,100,p) ) if (strncmp("VmPeak",c,6)==0) fputs(c, stderr); fclose(p); #endif } //------ /headers -------- int n, m, k; VI tubes; LL sediment; VPII unions; VPII reactions; typedef set<int> set_t; vector<pair<int, set_t>> parent; int find(int x) { if (x != parent[x].first) { parent[x].first = find(parent[x].first); } return parent[x].first; } void onion(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } // DBGprint("unitING x %d y %d", x, y); // for (int i : parent[x].second) { // DBGprint("set[x]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } // for (int i : parent[y].second) { // DBGprint("set[y]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } if (parent[x].second.size() > parent[y].second.size()) { swap(x, y); } for (int w : parent[x].second) { // musimy przelatywac od najmniejszych auto it = parent[y].second.find(w); if (it == parent[y].second.end()) { parent[y].second.insert(w); } else { // osad! int s = min(tubes[reactions[w].first], tubes[reactions[w].second]); DBGprint("while uniting %d %d found %d (sedim %d)", x, y, w, s); tubes[reactions[w].first] -= s; tubes[reactions[w].second] -= s; sediment += 2 * s; } } parent[x].first = y; parent[x].second.clear(); // DBGprint("united x %d y %d", x, y); // for (int i : parent[x].second) { // DBGprint("set[x]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } // for (int i : parent[y].second) { // DBGprint("set[y]: %d (%d-%d with %d, %d)", i, // reactions[i].first, reactions[i].second, tubes[reactions[i].first], tubes[reactions[i].second]); // } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; ++i) { int tmp; scanf("%d", &tmp); tubes.push_back(tmp); parent.emplace_back(i, set_t()); } for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); unions.emplace_back(a - 1, b - 1); } for (int i = 0; i < k; ++i) { int a, b; scanf("%d%d", &a, &b); --a; --b; parent[a].second.insert(i); parent[b].second.insert(i); reactions.emplace_back(a, b); } for (const PII& uni : unions) { onion(uni.first, uni.second); } printf("%lld\n", sediment); memcheck(); } |