#include <cstdio> #include <iostream> #include <set> #include <algorithm> #include <iomanip> #include <cassert> #include <array> #define REP(i, n) for(int i = 0; i < n; i++) #define FWD(i, a, b) for(int i = a; i < b; i++) #define ALL(u) (u).begin(), (u).end() using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef uint uint; const int INF = 1000000000; class FindUnion { vector<int> pre, height; public: FindUnion(int n) { init(n); } FindUnion() {} void init(int n) { pre.clear(); height.clear(); pre.resize(n); height.resize(n, 1); REP(i, n) pre[i] = i; } int find(int u) { if (u != pre.at(u)) pre[u] = find(pre.at(u)); return pre.at(u); } bool link(int u, int v) { u = find(u); v = find(v); if (u == v) return false; if (height[u] > height[v]) { swap(u, v); } pre[u] = v; if (height[u] == height[v]) { height[v]++; } return true; } }; struct Reaction { int first, second, prio; Reaction(int _first, int _second, int _prio) : first(_first), second(_second), prio(_prio) {} bool operator<(Reaction r) const { return prio < r.prio; } }; class Sol { public: int m, n, k; vector<int> amount, vial_to_node, lca_subtree_root, lca_visited, rek_calc_done; struct Node { Node(int _id, int left=-1, int right=-1) : id(_id) { adj[0] = left; adj[1] = right; } int id; array<int, 2> adj; vector<Reaction> reactions; vector<Reaction> lca; }; vector<Node> nodes; FindUnion fu; void lca() { fu.init(nodes.size()); lca_subtree_root.resize(nodes.size()); lca_visited.resize(nodes.size(), -1); for(int root = nodes.size() - 1; root >= 0; root--) { if (lca_visited[root] == -1) { rek_lca(root, root); } } } void rek_lca(int root, int super_root) { if (nodes[root].id == -1) { REP(i, 2) { rek_lca(nodes[root].adj[i], super_root); fu.link(root, nodes[root].adj[i]); lca_subtree_root[fu.find(root)] = root; } } else { for (Reaction query : nodes[root].lca) { if (lca_visited[query.second] == super_root) { nodes[lca_subtree_root[fu.find(query.second)]].reactions.push_back(query); } } } lca_visited[root] = super_root; } LL rek_calc(int root) { rek_calc_done[root] = 1; LL res = 0; if (nodes[root].id == -1) { REP(i, 2) { res += rek_calc(nodes[root].adj[i]); } sort(nodes[root].reactions.begin(), nodes[root].reactions.end()); for (Reaction reaction : nodes[root].reactions) { LL amount_used = min(amount[reaction.first], amount[reaction.second]); //printf("root: %d, reaction: %d, %d, used: %lld\n", root, reaction.first, reaction.second, amount_used); res += 2 * amount_used; amount[reaction.first] -= amount_used; amount[reaction.second] -= amount_used; } } //printf("%lld\n", res); return res; } LL calc() { LL res = 0; rek_calc_done.resize(nodes.size()); for (int root = nodes.size() - 1; root >= 0; root--) { if (rek_calc_done[root] == 0) { res += rek_calc(root); } } return res; } void sol() { scanf("%d %d %d", &n, &m, &k); amount.resize(n); vial_to_node.resize(n); REP(i, n) { scanf("%d", &amount[i]); nodes.push_back(Node(i)); vial_to_node[i] = i; } REP(i, m) { int u, v; scanf("%d %d", &u, &v); u--; v--; int next_node = nodes.size(); nodes.push_back(Node(-1, vial_to_node[u], vial_to_node[v])); vial_to_node[v] = next_node; } REP(i, k) { int u, v; scanf("%d %d", &u, &v); u--; v--; nodes[u].lca.push_back(Reaction(u, v, i)); nodes[v].lca.push_back(Reaction(v, u, i)); } lca(); printf("%lld\n", calc()); } }; int main() { Sol s; s.sol(); 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 | #include <cstdio> #include <iostream> #include <set> #include <algorithm> #include <iomanip> #include <cassert> #include <array> #define REP(i, n) for(int i = 0; i < n; i++) #define FWD(i, a, b) for(int i = a; i < b; i++) #define ALL(u) (u).begin(), (u).end() using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef uint uint; const int INF = 1000000000; class FindUnion { vector<int> pre, height; public: FindUnion(int n) { init(n); } FindUnion() {} void init(int n) { pre.clear(); height.clear(); pre.resize(n); height.resize(n, 1); REP(i, n) pre[i] = i; } int find(int u) { if (u != pre.at(u)) pre[u] = find(pre.at(u)); return pre.at(u); } bool link(int u, int v) { u = find(u); v = find(v); if (u == v) return false; if (height[u] > height[v]) { swap(u, v); } pre[u] = v; if (height[u] == height[v]) { height[v]++; } return true; } }; struct Reaction { int first, second, prio; Reaction(int _first, int _second, int _prio) : first(_first), second(_second), prio(_prio) {} bool operator<(Reaction r) const { return prio < r.prio; } }; class Sol { public: int m, n, k; vector<int> amount, vial_to_node, lca_subtree_root, lca_visited, rek_calc_done; struct Node { Node(int _id, int left=-1, int right=-1) : id(_id) { adj[0] = left; adj[1] = right; } int id; array<int, 2> adj; vector<Reaction> reactions; vector<Reaction> lca; }; vector<Node> nodes; FindUnion fu; void lca() { fu.init(nodes.size()); lca_subtree_root.resize(nodes.size()); lca_visited.resize(nodes.size(), -1); for(int root = nodes.size() - 1; root >= 0; root--) { if (lca_visited[root] == -1) { rek_lca(root, root); } } } void rek_lca(int root, int super_root) { if (nodes[root].id == -1) { REP(i, 2) { rek_lca(nodes[root].adj[i], super_root); fu.link(root, nodes[root].adj[i]); lca_subtree_root[fu.find(root)] = root; } } else { for (Reaction query : nodes[root].lca) { if (lca_visited[query.second] == super_root) { nodes[lca_subtree_root[fu.find(query.second)]].reactions.push_back(query); } } } lca_visited[root] = super_root; } LL rek_calc(int root) { rek_calc_done[root] = 1; LL res = 0; if (nodes[root].id == -1) { REP(i, 2) { res += rek_calc(nodes[root].adj[i]); } sort(nodes[root].reactions.begin(), nodes[root].reactions.end()); for (Reaction reaction : nodes[root].reactions) { LL amount_used = min(amount[reaction.first], amount[reaction.second]); //printf("root: %d, reaction: %d, %d, used: %lld\n", root, reaction.first, reaction.second, amount_used); res += 2 * amount_used; amount[reaction.first] -= amount_used; amount[reaction.second] -= amount_used; } } //printf("%lld\n", res); return res; } LL calc() { LL res = 0; rek_calc_done.resize(nodes.size()); for (int root = nodes.size() - 1; root >= 0; root--) { if (rek_calc_done[root] == 0) { res += rek_calc(root); } } return res; } void sol() { scanf("%d %d %d", &n, &m, &k); amount.resize(n); vial_to_node.resize(n); REP(i, n) { scanf("%d", &amount[i]); nodes.push_back(Node(i)); vial_to_node[i] = i; } REP(i, m) { int u, v; scanf("%d %d", &u, &v); u--; v--; int next_node = nodes.size(); nodes.push_back(Node(-1, vial_to_node[u], vial_to_node[v])); vial_to_node[v] = next_node; } REP(i, k) { int u, v; scanf("%d %d", &u, &v); u--; v--; nodes[u].lca.push_back(Reaction(u, v, i)); nodes[v].lca.push_back(Reaction(v, u, i)); } lca(); printf("%lld\n", calc()); } }; int main() { Sol s; s.sol(); return 0; } |