#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> using namespace std; const int N = 200001; const int M = 500001; const int LM = 21; int v[N]; int lab[LM][N]; pair<int,int> reacts[M]; pair<int,int> ops[N]; int n, m, k; long long ret; int parent(int l, int i) { int r = i; while (lab[l][r] != r) { r = lab[l][r]; } while (i != r) { int t = lab[l][i]; lab[l][i] = r; i = t; } return r; } bool check(int l, int a, int b) { return parent(l, a) == parent(l, b); } void join(int l, int a, int b) { int pa = parent(l, a); int pb = parent(l, b); lab[l][pb] = pa; } void go(int a, int b, int l) { copy(lab[l - 1], lab[l - 1] + n, lab[l]); for (int i = a; i < b; ++i) { join(l, ops[i].second, ops[i].first); } } void splitList(int l, list<int> &a, list<int> &b) { for (auto it = a.begin(); it != a.end(); ) { int x = reacts[*it].first; int y = reacts[*it].second; if (check(l, x, y)) { b.push_back(*it); auto del = it; ++it; a.erase(del); } else { ++it; } } } void perform(list<int> &a) { for (auto i : a) { int x = reacts[i].first; int y = reacts[i].second; int dv = min(v[x], v[y]); ret += dv + dv; v[x] -= dv; v[y] -= dv; } } void filter(list<int> &a) { for (auto it = a.begin(); it != a.end(); ) { int x = reacts[*it].first; int y = reacts[*it].second; if (min(v[x], v[y]) == 0) { auto del = it; ++it; a.erase(del); } else { ++it; } } } void go(int a, int b, int l, list<int> &ol) { if (a + 1 == b || ol.empty()) { perform(ol); return; } int c = (a + b) / 2; go(a, c, l + 1); list<int> ol2; splitList(l + 1, ol, ol2); if (!ol2.empty()) { go(a, c, l, ol2); filter(ol); if (!ol.empty()) { go(a, c, l + 1); } } if (!ol.empty()) { go(c, b, l + 1, ol); } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) { scanf("%d", v + i); lab[0][i] = i; } for (int i = 0; i < m; ++i) { scanf("%d%d", &ops[i].first, &ops[i].second); } list<int> ol; list<int> olx; for (int i = 0; i < k; ++i) { scanf("%d%d", &reacts[i].first, &reacts[i].second); ol.push_back(i); } ret = 0; go(0, m, 1); splitList(1, ol, olx); go(0, m, 0, olx); printf("%lld\n", 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 | #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> using namespace std; const int N = 200001; const int M = 500001; const int LM = 21; int v[N]; int lab[LM][N]; pair<int,int> reacts[M]; pair<int,int> ops[N]; int n, m, k; long long ret; int parent(int l, int i) { int r = i; while (lab[l][r] != r) { r = lab[l][r]; } while (i != r) { int t = lab[l][i]; lab[l][i] = r; i = t; } return r; } bool check(int l, int a, int b) { return parent(l, a) == parent(l, b); } void join(int l, int a, int b) { int pa = parent(l, a); int pb = parent(l, b); lab[l][pb] = pa; } void go(int a, int b, int l) { copy(lab[l - 1], lab[l - 1] + n, lab[l]); for (int i = a; i < b; ++i) { join(l, ops[i].second, ops[i].first); } } void splitList(int l, list<int> &a, list<int> &b) { for (auto it = a.begin(); it != a.end(); ) { int x = reacts[*it].first; int y = reacts[*it].second; if (check(l, x, y)) { b.push_back(*it); auto del = it; ++it; a.erase(del); } else { ++it; } } } void perform(list<int> &a) { for (auto i : a) { int x = reacts[i].first; int y = reacts[i].second; int dv = min(v[x], v[y]); ret += dv + dv; v[x] -= dv; v[y] -= dv; } } void filter(list<int> &a) { for (auto it = a.begin(); it != a.end(); ) { int x = reacts[*it].first; int y = reacts[*it].second; if (min(v[x], v[y]) == 0) { auto del = it; ++it; a.erase(del); } else { ++it; } } } void go(int a, int b, int l, list<int> &ol) { if (a + 1 == b || ol.empty()) { perform(ol); return; } int c = (a + b) / 2; go(a, c, l + 1); list<int> ol2; splitList(l + 1, ol, ol2); if (!ol2.empty()) { go(a, c, l, ol2); filter(ol); if (!ol.empty()) { go(a, c, l + 1); } } if (!ol.empty()) { go(c, b, l + 1, ol); } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) { scanf("%d", v + i); lab[0][i] = i; } for (int i = 0; i < m; ++i) { scanf("%d%d", &ops[i].first, &ops[i].second); } list<int> ol; list<int> olx; for (int i = 0; i < k; ++i) { scanf("%d%d", &reacts[i].first, &reacts[i].second); ol.push_back(i); } ret = 0; go(0, m, 1); splitList(1, ol, olx); go(0, m, 0, olx); printf("%lld\n", ret); return 0; } |