#include <iostream> #include <cctype> #include <set> #include <vector> #include <algorithm> #ifndef _MSC_VER #define __int64 long long #endif typedef unsigned int uint; typedef unsigned __int64 uint64; uint get_uint() { int c; while (isspace(c = std::cin.get())); uint value = (c - '0'); while (isdigit(c = std::cin.get())) { value = (10 * value) + (c - '0'); } return value; } typedef std::set<uint> uint_set; typedef std::vector<uint> uint_vector; uint g[200002]; uint a[200002], b[200002]; uint c[500002], d[500002]; uint_set react[200002]; uint_vector need[200002]; bool done[500002]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); uint m = get_uint(); uint k = get_uint(); for (uint i = 0; i < n; ++i) { g[i + 1] = get_uint(); } for (uint i = 0; i < m; ++i) { a[i] = get_uint(); b[i] = get_uint(); } for (uint i = 0; i < k; ++i) { c[i] = get_uint(); d[i] = get_uint(); react[c[i]].insert(i); react[d[i]].insert(i); need[c[i]].push_back(i); need[d[i]].push_back(i); } uint64 result = 0; for (uint i = 0; i < m; ++i) { uint from = a[i]; uint to = b[i]; uint_set &react_from = react[from]; uint_set &react_to = react[to]; if (react_from.size() > react_to.size()) { std::swap(react_from, react_to); } for (auto x : react_from) { if (react_to.count(x)) { uint update = std::min(g[c[x]], g[d[x]]); result += update; g[c[x]] -= update; g[d[x]] -= update; if (g[c[x]] == 0) { for (auto y : need[c[x]]) { done[y] = true; } } if (g[d[x]] == 0) { for (auto y : need[d[x]]) { done[y] = true; } } } else if (!done[x]) { react_to.insert(x); } } react_from.clear(); } std::cout << result * 2 << std::endl; 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 | #include <iostream> #include <cctype> #include <set> #include <vector> #include <algorithm> #ifndef _MSC_VER #define __int64 long long #endif typedef unsigned int uint; typedef unsigned __int64 uint64; uint get_uint() { int c; while (isspace(c = std::cin.get())); uint value = (c - '0'); while (isdigit(c = std::cin.get())) { value = (10 * value) + (c - '0'); } return value; } typedef std::set<uint> uint_set; typedef std::vector<uint> uint_vector; uint g[200002]; uint a[200002], b[200002]; uint c[500002], d[500002]; uint_set react[200002]; uint_vector need[200002]; bool done[500002]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); uint m = get_uint(); uint k = get_uint(); for (uint i = 0; i < n; ++i) { g[i + 1] = get_uint(); } for (uint i = 0; i < m; ++i) { a[i] = get_uint(); b[i] = get_uint(); } for (uint i = 0; i < k; ++i) { c[i] = get_uint(); d[i] = get_uint(); react[c[i]].insert(i); react[d[i]].insert(i); need[c[i]].push_back(i); need[d[i]].push_back(i); } uint64 result = 0; for (uint i = 0; i < m; ++i) { uint from = a[i]; uint to = b[i]; uint_set &react_from = react[from]; uint_set &react_to = react[to]; if (react_from.size() > react_to.size()) { std::swap(react_from, react_to); } for (auto x : react_from) { if (react_to.count(x)) { uint update = std::min(g[c[x]], g[d[x]]); result += update; g[c[x]] -= update; g[d[x]] -= update; if (g[c[x]] == 0) { for (auto y : need[c[x]]) { done[y] = true; } } if (g[d[x]] == 0) { for (auto y : need[d[x]]) { done[y] = true; } } } else if (!done[x]) { react_to.insert(x); } } react_from.clear(); } std::cout << result * 2 << std::endl; return 0; } |