#include <cstdio> #include <cstdlib> #include <vector> #include <list> #include <algorithm> #include <map> using namespace std; const int N_MAX = 200000; const int K_MAX = 500000; /*typedef vector<int> edges; vector<edges> G;*/ int fiolki[N_MAX]; pair<int, int> kroki[N_MAX]; pair<int, int> pary[K_MAX]; map< pair<int, int>, int > M; typedef struct { int parent; int rank; list<int> content; } item; item sets[N_MAX]; void init(int i) { sets[i].parent = i; sets[i].rank = 0; sets[i].content.push_back(i); } int find(int x) { int z = x; while (z != sets[z].parent) z = sets[z].parent; while (x != sets[x].parent) { int y = sets[x].parent; sets[x].parent = z; x = y; } return x; } int union_set(int a, int b) { int aParent = find(a); int bParent = find(b); if (sets[aParent].rank < sets[bParent].rank) { sets[aParent].parent = bParent; sets[bParent].content.merge(sets[aParent].content); return bParent; } else if (sets[aParent].rank > sets[bParent].rank) { sets[bParent].parent = aParent; sets[aParent].content.merge(sets[bParent].content); return aParent; } else { ++sets[aParent].rank; sets[bParent].parent = aParent; sets[aParent].content.merge(sets[bParent].content); return aParent; } } int min(int a, int b) { return a > b ? b : a; } int main() { int n, m, k; scanf("%d %d %d\n", &n, &m, &k); for (int i = 0; i < n; ++i) { scanf("%d", &fiolki[i]); init(i); } for (int i = 0; i < m; ++i) { int a,b; scanf("%d %d\n", &a, &b); --a, --b; kroki[i] = make_pair(a, b); } for (int i = 0; i < k; ++i) { int a, b; scanf("%d %d\n", &a, &b); --a, --b; if (a < b) M.insert(make_pair(make_pair(a, b), i)); else M.insert(make_pair(make_pair(b, a), i)); } long long int result = 0; vector<pair<pair<int, int>, pair<int, int> > > v; for (int i = 0; i < m; ++i) { // printf("(%d %d)\n", kroki[i].first, kroki[i].second); int firstSet = find(kroki[i].first); int secondSet = find(kroki[i].second); // printf("(%d %d)\n", firstSet, secondSet); for (list<int>::iterator it = sets[firstSet].content.begin(); it != sets[firstSet].content.end(); ++it) { for (list<int>::iterator jt = sets[secondSet].content.begin(); jt != sets[secondSet].content.end(); ++jt) { int a = *it, b = *jt; // printf("%d %d\n", a, b); if (a < b) { map< pair<int, int>, int >::iterator val = M.find(make_pair(a, b)); if (val != M.end()) { v.push_back(make_pair(make_pair(i, val->second), make_pair(a, b))); } } else { map< pair<int, int>, int >::iterator val = M.find(make_pair(b, a)); if (val != M.end()) { v.push_back(make_pair(make_pair(i, val->second), make_pair(b, a))); } } } } union_set(kroki[i].first, kroki[i].second); /* for (list<int>::iterator it = sets[c].content.begin(); it != sets[c].content.end(); ++it) { printf("%d ", *it); } printf("\n");*/ /* for (int j = 0; j < k; ++j) { int a = pary[j].first, b = pary[j].second; if (find(a) == find(b)) { int ile = min(fiolki[a], fiolki[b]); result += 2 * (long long int) ile; fiolki[a] -= ile; fiolki[b] -= ile; } }*/ } sort(v.begin(), v.end()); for (int i = 0; i < v.size(); ++i) { // printf("%d %d %d %d\n", v[i].first.first, v[i].first.second, v[i].second.first, v[i].second.second); int a = v[i].second.first; int b = v[i].second.second; int ile = min(fiolki[a], fiolki[b]); result += 2 * (long long int) ile; fiolki[a] -= ile; fiolki[b] -= ile; } printf("%lld\n", result); 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 | #include <cstdio> #include <cstdlib> #include <vector> #include <list> #include <algorithm> #include <map> using namespace std; const int N_MAX = 200000; const int K_MAX = 500000; /*typedef vector<int> edges; vector<edges> G;*/ int fiolki[N_MAX]; pair<int, int> kroki[N_MAX]; pair<int, int> pary[K_MAX]; map< pair<int, int>, int > M; typedef struct { int parent; int rank; list<int> content; } item; item sets[N_MAX]; void init(int i) { sets[i].parent = i; sets[i].rank = 0; sets[i].content.push_back(i); } int find(int x) { int z = x; while (z != sets[z].parent) z = sets[z].parent; while (x != sets[x].parent) { int y = sets[x].parent; sets[x].parent = z; x = y; } return x; } int union_set(int a, int b) { int aParent = find(a); int bParent = find(b); if (sets[aParent].rank < sets[bParent].rank) { sets[aParent].parent = bParent; sets[bParent].content.merge(sets[aParent].content); return bParent; } else if (sets[aParent].rank > sets[bParent].rank) { sets[bParent].parent = aParent; sets[aParent].content.merge(sets[bParent].content); return aParent; } else { ++sets[aParent].rank; sets[bParent].parent = aParent; sets[aParent].content.merge(sets[bParent].content); return aParent; } } int min(int a, int b) { return a > b ? b : a; } int main() { int n, m, k; scanf("%d %d %d\n", &n, &m, &k); for (int i = 0; i < n; ++i) { scanf("%d", &fiolki[i]); init(i); } for (int i = 0; i < m; ++i) { int a,b; scanf("%d %d\n", &a, &b); --a, --b; kroki[i] = make_pair(a, b); } for (int i = 0; i < k; ++i) { int a, b; scanf("%d %d\n", &a, &b); --a, --b; if (a < b) M.insert(make_pair(make_pair(a, b), i)); else M.insert(make_pair(make_pair(b, a), i)); } long long int result = 0; vector<pair<pair<int, int>, pair<int, int> > > v; for (int i = 0; i < m; ++i) { // printf("(%d %d)\n", kroki[i].first, kroki[i].second); int firstSet = find(kroki[i].first); int secondSet = find(kroki[i].second); // printf("(%d %d)\n", firstSet, secondSet); for (list<int>::iterator it = sets[firstSet].content.begin(); it != sets[firstSet].content.end(); ++it) { for (list<int>::iterator jt = sets[secondSet].content.begin(); jt != sets[secondSet].content.end(); ++jt) { int a = *it, b = *jt; // printf("%d %d\n", a, b); if (a < b) { map< pair<int, int>, int >::iterator val = M.find(make_pair(a, b)); if (val != M.end()) { v.push_back(make_pair(make_pair(i, val->second), make_pair(a, b))); } } else { map< pair<int, int>, int >::iterator val = M.find(make_pair(b, a)); if (val != M.end()) { v.push_back(make_pair(make_pair(i, val->second), make_pair(b, a))); } } } } union_set(kroki[i].first, kroki[i].second); /* for (list<int>::iterator it = sets[c].content.begin(); it != sets[c].content.end(); ++it) { printf("%d ", *it); } printf("\n");*/ /* for (int j = 0; j < k; ++j) { int a = pary[j].first, b = pary[j].second; if (find(a) == find(b)) { int ile = min(fiolki[a], fiolki[b]); result += 2 * (long long int) ile; fiolki[a] -= ile; fiolki[b] -= ile; } }*/ } sort(v.begin(), v.end()); for (int i = 0; i < v.size(); ++i) { // printf("%d %d %d %d\n", v[i].first.first, v[i].first.second, v[i].second.first, v[i].second.second); int a = v[i].second.first; int b = v[i].second.second; int ile = min(fiolki[a], fiolki[b]); result += 2 * (long long int) ile; fiolki[a] -= ile; fiolki[b] -= ile; } printf("%lld\n", result); return 0; } |