#include <algorithm> #include <vector> #include <cstdio> #include <unordered_map> struct Vertex { Vertex() : tree(-1), level(0), to(-1) {} int tree; // nr drzewa do ktorego wierzcholek nalezy int level; // poziom w danym drzewie int to; // krawedzie od wierzcholka std::vector<int> from; // krawedzie do wierzcholka }; std::vector<Vertex> vertices(200001); struct TreeData { TreeData(int _uniq = -1) : root(-1), index(0), unique_id(_uniq) {} void prepare(int _ver); // przygotowuje drzewo, szuka roota --- O(m), m - ilosc wierzcholkow void createEulerTour(int _ver, int _depth); // tworzy sciezke po drzewie --- O(2*m) void prepareRMQ(); // przygotowuje rmq --- O(2 * m * log(m)) int LCA(int _v1, int _v2); // odpowiada na najmniejsza wartosc w przedziale // --- int root, index, unique_id; std::vector<std::vector<int>> acount; // licznik pozycji przodkow std::vector<std::vector<int>> rmq; // range minumum query - dla szybkiego wyznaczania LCA std::vector<int> euler; std::unordered_map<int, int> mapping; // mapuje wierzcholki na wewnetrzna notacje std::unordered_map<int, int> inv_map; }; void TreeData::prepare(int _ver) { // szukaj roota root = _ver; while (vertices[root].to != -1) { root = vertices[root].to; } // ----- index = 0; createEulerTour(root, 0); prepareRMQ(); } void TreeData::createEulerTour(int _ver, int _depth) { int tidx = index; vertices[_ver].level = _depth; vertices[_ver].tree = unique_id; mapping[_ver] = tidx; inv_map[tidx] = _ver; euler.push_back(tidx); while (tidx >= acount.size()) { acount.emplace_back(std::vector<int>()); } acount[tidx].emplace_back(euler.size() - 1); for (auto &v : vertices[_ver].from) { ++index; createEulerTour(v, _depth + 1); euler.push_back(tidx); acount[tidx].emplace_back(euler.size() - 1); } } void TreeData::prepareRMQ() { int lg = (int)(std::log2(euler.size())); rmq.resize(euler.size()); for (int i = 0; i < rmq.size(); ++i) { rmq[i].resize(lg + 1); rmq[i][0] = euler[i]; } for (int j = 1; 1 << j < rmq.size(); ++j) { for (int i = 0; i + (1 << j) - 1 < rmq.size(); ++i) { rmq[i][j] = std::min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]); } } } int TreeData::LCA(int _v1, int _v2) { _v1 = mapping[_v1]; _v2 = mapping[_v2]; _v1 = acount[_v1][0]; _v2 = acount[_v2][0]; if (_v1 > _v2) { std::swap(_v1, _v2); } int k = (int)(std::log2(_v2 - _v1 + 1)); int tmp = 1 << k; return std::min(rmq[_v1][k], rmq[_v2-tmp+1][k]); } // ----- struct Query { int treeId; int lca; int level; int anc; int c, d; }; bool operator<(const Query &_q1, const Query &_q2) { return (_q1.treeId < _q2.treeId) || (!(_q1.treeId > _q2.treeId) && ((_q1.level > _q2.level) || (!(_q1.level < _q2.level) && ((_q1.lca < _q2.lca) || (!(_q1.lca > _q2.lca) && (_q1.anc < _q2.anc)))))); } int main() { int n, m, k; int treeCounter = 0; scanf("%d%d%d", &n, &m, &k); std::vector<long long> sub(n+1); std::vector<TreeData> trees; for (int i = 0; i < n; ++i) { scanf("%d", &sub[i+1]); } for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); vertices[a].to = b; vertices[b].from.emplace_back(a); } for (int i = 1; i <= n; ++i) { if (vertices[i].tree == -1) { trees.emplace_back(TreeData(treeCounter++)); trees.back().prepare(i); } } std::vector<Query> queries; for (int i = 0; i < k; ++i) { int c, d; scanf("%d%d", &c, &d); if (vertices[c].tree == vertices[d].tree) { int tid = vertices[c].tree; queries.emplace_back(Query{}); queries.back().treeId = vertices[c].tree; queries.back().lca = trees[tid].LCA(c, d); queries.back().level = vertices[trees[tid].inv_map[queries.back().lca]].level; int far = std::max(trees[tid].acount[trees[tid].mapping[c]][0], trees[tid].acount[trees[tid].mapping[d]][0]); queries.back().anc = std::lower_bound(trees[tid].acount[queries.back().lca].begin(), trees[tid].acount[queries.back().lca].end(), far) - trees[tid].acount[queries.back().lca].begin(); queries.back().c = c; queries.back().d = d; } } std::stable_sort(queries.begin(), queries.end()); long long res = 0; for (auto &it : queries) { long long m = std::min(sub[it.c], sub[it.d]); res += m + m; sub[it.c] -= m; sub[it.d] -= m; } printf("%lld\n", res); 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 | #include <algorithm> #include <vector> #include <cstdio> #include <unordered_map> struct Vertex { Vertex() : tree(-1), level(0), to(-1) {} int tree; // nr drzewa do ktorego wierzcholek nalezy int level; // poziom w danym drzewie int to; // krawedzie od wierzcholka std::vector<int> from; // krawedzie do wierzcholka }; std::vector<Vertex> vertices(200001); struct TreeData { TreeData(int _uniq = -1) : root(-1), index(0), unique_id(_uniq) {} void prepare(int _ver); // przygotowuje drzewo, szuka roota --- O(m), m - ilosc wierzcholkow void createEulerTour(int _ver, int _depth); // tworzy sciezke po drzewie --- O(2*m) void prepareRMQ(); // przygotowuje rmq --- O(2 * m * log(m)) int LCA(int _v1, int _v2); // odpowiada na najmniejsza wartosc w przedziale // --- int root, index, unique_id; std::vector<std::vector<int>> acount; // licznik pozycji przodkow std::vector<std::vector<int>> rmq; // range minumum query - dla szybkiego wyznaczania LCA std::vector<int> euler; std::unordered_map<int, int> mapping; // mapuje wierzcholki na wewnetrzna notacje std::unordered_map<int, int> inv_map; }; void TreeData::prepare(int _ver) { // szukaj roota root = _ver; while (vertices[root].to != -1) { root = vertices[root].to; } // ----- index = 0; createEulerTour(root, 0); prepareRMQ(); } void TreeData::createEulerTour(int _ver, int _depth) { int tidx = index; vertices[_ver].level = _depth; vertices[_ver].tree = unique_id; mapping[_ver] = tidx; inv_map[tidx] = _ver; euler.push_back(tidx); while (tidx >= acount.size()) { acount.emplace_back(std::vector<int>()); } acount[tidx].emplace_back(euler.size() - 1); for (auto &v : vertices[_ver].from) { ++index; createEulerTour(v, _depth + 1); euler.push_back(tidx); acount[tidx].emplace_back(euler.size() - 1); } } void TreeData::prepareRMQ() { int lg = (int)(std::log2(euler.size())); rmq.resize(euler.size()); for (int i = 0; i < rmq.size(); ++i) { rmq[i].resize(lg + 1); rmq[i][0] = euler[i]; } for (int j = 1; 1 << j < rmq.size(); ++j) { for (int i = 0; i + (1 << j) - 1 < rmq.size(); ++i) { rmq[i][j] = std::min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]); } } } int TreeData::LCA(int _v1, int _v2) { _v1 = mapping[_v1]; _v2 = mapping[_v2]; _v1 = acount[_v1][0]; _v2 = acount[_v2][0]; if (_v1 > _v2) { std::swap(_v1, _v2); } int k = (int)(std::log2(_v2 - _v1 + 1)); int tmp = 1 << k; return std::min(rmq[_v1][k], rmq[_v2-tmp+1][k]); } // ----- struct Query { int treeId; int lca; int level; int anc; int c, d; }; bool operator<(const Query &_q1, const Query &_q2) { return (_q1.treeId < _q2.treeId) || (!(_q1.treeId > _q2.treeId) && ((_q1.level > _q2.level) || (!(_q1.level < _q2.level) && ((_q1.lca < _q2.lca) || (!(_q1.lca > _q2.lca) && (_q1.anc < _q2.anc)))))); } int main() { int n, m, k; int treeCounter = 0; scanf("%d%d%d", &n, &m, &k); std::vector<long long> sub(n+1); std::vector<TreeData> trees; for (int i = 0; i < n; ++i) { scanf("%d", &sub[i+1]); } for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); vertices[a].to = b; vertices[b].from.emplace_back(a); } for (int i = 1; i <= n; ++i) { if (vertices[i].tree == -1) { trees.emplace_back(TreeData(treeCounter++)); trees.back().prepare(i); } } std::vector<Query> queries; for (int i = 0; i < k; ++i) { int c, d; scanf("%d%d", &c, &d); if (vertices[c].tree == vertices[d].tree) { int tid = vertices[c].tree; queries.emplace_back(Query{}); queries.back().treeId = vertices[c].tree; queries.back().lca = trees[tid].LCA(c, d); queries.back().level = vertices[trees[tid].inv_map[queries.back().lca]].level; int far = std::max(trees[tid].acount[trees[tid].mapping[c]][0], trees[tid].acount[trees[tid].mapping[d]][0]); queries.back().anc = std::lower_bound(trees[tid].acount[queries.back().lca].begin(), trees[tid].acount[queries.back().lca].end(), far) - trees[tid].acount[queries.back().lca].begin(); queries.back().c = c; queries.back().d = d; } } std::stable_sort(queries.begin(), queries.end()); long long res = 0; for (auto &it : queries) { long long m = std::min(sub[it.c], sub[it.d]); res += m + m; sub[it.c] -= m; sub[it.d] -= m; } printf("%lld\n", res); return 0; } |