#include <cstdio> #include <cstdlib> #include <algorithm> #include <utility> #include <vector> static const int VERY_LARGE_NUMBER = 2 * 1000 * 1000 * 1000; static const int MAX_N = 200 * 1000; static const int MAX_M = 200 * 1000; static const int MAX_K = 500 * 1000; static const int MAX_G = 1e9; static const int LOG_K = 20; struct phial { int substanceAmt; int momentOfPouring; int level; //Wysokość w drzewie int jumps[LOG_K]; inline phial() : substanceAmt(0), momentOfPouring(VERY_LARGE_NUMBER), level(0), jumps{0} {} }; struct rule { int a, b; int priority; int properPriority; int properiestPriority; inline rule() : a(0), b(0), priority(0), properPriority(0) {} inline bool operator<(const rule & other) const { if (properiestPriority == other.properiestPriority) { if (properPriority == other.properPriority) return priority < other.priority; return properPriority < other.properPriority; } return properiestPriority < other.properiestPriority; } }; //Ilość substancji + struktura do LCA std::vector<phial> phials; //Zasady reagowania std::vector<rule> rules; //Kolejność wylewania zawartości fiolek (+ 0 na początku) std::vector<int> ordering; int n, m, k; inline int traverseUp(int id, int toLevel) { int j = LOG_K; while (j >= 0) { int jumpamt = 1 << j; if (phials[id].level - jumpamt >= toLevel) id = phials[id].jumps[j]; j--; } return id; } //Zwraca LCA oraz poprzednika LCA na ścieżce z a do LCA oraz z b do LCA void comboLCA(int a, int b, int & lca, int & pre_lca_a, int & pre_lca_b) { if (phials[a].level > phials[b].level) { comboLCA(b, a, lca, pre_lca_b, pre_lca_a); return; } int proxyB = traverseUp(b, phials[a].level); //Teraz a i b są na tym samym poziomie if (a == proxyB) { //LCA(a, b) == a lca = a; pre_lca_a = a; pre_lca_b = traverseUp(b, phials[a].level + 1); } else { //LCA jest gdzieś wyżej int proxyA = a; int j = LOG_K; while (j >= 0) { int nextA = phials[proxyA].jumps[j]; int nextB = phials[proxyA].jumps[j]; if (nextA != nextB) { proxyA = nextA; proxyB = nextB; } j--; } lca = phials[proxyA].jumps[0]; pre_lca_a = proxyA; pre_lca_b = proxyB; } } int main() { scanf("%d %d %d", &n, &m, &k); if (m == 0 || k == 0) { puts("0"); return 0; } phials.reserve(n + 1); ordering.reserve(m + 1); rules.reserve(k); //Wczytanie ilości substancji w probówkach phials.push_back(phial()); for (int i = 1; i <= n; i++) { phial p; scanf("%d", &p.substanceAmt); phials.push_back(p); } //Wczytanie operacji na fiolkach ordering.push_back(0); for (int i = 1; i <= m; i++) { int a, b; scanf("%d %d", &a, &b); phials[a].jumps[0] = b; phials[a].momentOfPouring = i; ordering.push_back(a); } //Wczytanie reguł zachodzenia reakcji for (int i = 0; i < k; i++) { rule r; scanf("%d %d", &r.a, &r.b); r.priority = i; rules.push_back(r); } //Tworzymy strukturę LCA for (int i = 0; i <= m; i++) { //Kolejność, którą dostajemy to odwrotny porządek topologiczny int c = ordering[m - i]; phials[c].level = phials[phials[c].jumps[0]].level + 1; for (int j = 1; j < LOG_K; j++) phials[c].jumps[j] = phials[phials[c].jumps[j - 1]].jumps[j - 1]; } //Liczymy "prawdziwy priorytet" dla reakcji for (int i = 0; i < k; i++) { int lca, pA, pB; comboLCA(rules[i].a, rules[i].b, lca, pA, pB); if (lca == 0) { //Reagenty w zasadzie nigdy nie trafią do jednej fiolki //Unieważniamy tą zadadę rules[i].a = 0; rules[i].b = 0; continue; } if (rules[i].a == lca) rules[i].properPriority = phials[pB].momentOfPouring; else if (rules[i].b == lca) rules[i].properPriority = phials[pA].momentOfPouring; else rules[i].properPriority = std::max(phials[pA].momentOfPouring, phials[pB].momentOfPouring); rules[i].properiestPriority = phials[lca].momentOfPouring; } //Sortujemy względem kolejności stosowania zasad std::sort(rules.begin(), rules.end()); //Wykonujemy zasady na fiolkach long long int sedimentAmt = 0; for (const rule & r : rules) { int reactedAmt = std::min(phials[r.a].substanceAmt, phials[r.b].substanceAmt); phials[r.a].substanceAmt -= reactedAmt; phials[r.b].substanceAmt -= reactedAmt; sedimentAmt += 2 * reactedAmt; } printf("%lld\n", sedimentAmt); 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | #include <cstdio> #include <cstdlib> #include <algorithm> #include <utility> #include <vector> static const int VERY_LARGE_NUMBER = 2 * 1000 * 1000 * 1000; static const int MAX_N = 200 * 1000; static const int MAX_M = 200 * 1000; static const int MAX_K = 500 * 1000; static const int MAX_G = 1e9; static const int LOG_K = 20; struct phial { int substanceAmt; int momentOfPouring; int level; //Wysokość w drzewie int jumps[LOG_K]; inline phial() : substanceAmt(0), momentOfPouring(VERY_LARGE_NUMBER), level(0), jumps{0} {} }; struct rule { int a, b; int priority; int properPriority; int properiestPriority; inline rule() : a(0), b(0), priority(0), properPriority(0) {} inline bool operator<(const rule & other) const { if (properiestPriority == other.properiestPriority) { if (properPriority == other.properPriority) return priority < other.priority; return properPriority < other.properPriority; } return properiestPriority < other.properiestPriority; } }; //Ilość substancji + struktura do LCA std::vector<phial> phials; //Zasady reagowania std::vector<rule> rules; //Kolejność wylewania zawartości fiolek (+ 0 na początku) std::vector<int> ordering; int n, m, k; inline int traverseUp(int id, int toLevel) { int j = LOG_K; while (j >= 0) { int jumpamt = 1 << j; if (phials[id].level - jumpamt >= toLevel) id = phials[id].jumps[j]; j--; } return id; } //Zwraca LCA oraz poprzednika LCA na ścieżce z a do LCA oraz z b do LCA void comboLCA(int a, int b, int & lca, int & pre_lca_a, int & pre_lca_b) { if (phials[a].level > phials[b].level) { comboLCA(b, a, lca, pre_lca_b, pre_lca_a); return; } int proxyB = traverseUp(b, phials[a].level); //Teraz a i b są na tym samym poziomie if (a == proxyB) { //LCA(a, b) == a lca = a; pre_lca_a = a; pre_lca_b = traverseUp(b, phials[a].level + 1); } else { //LCA jest gdzieś wyżej int proxyA = a; int j = LOG_K; while (j >= 0) { int nextA = phials[proxyA].jumps[j]; int nextB = phials[proxyA].jumps[j]; if (nextA != nextB) { proxyA = nextA; proxyB = nextB; } j--; } lca = phials[proxyA].jumps[0]; pre_lca_a = proxyA; pre_lca_b = proxyB; } } int main() { scanf("%d %d %d", &n, &m, &k); if (m == 0 || k == 0) { puts("0"); return 0; } phials.reserve(n + 1); ordering.reserve(m + 1); rules.reserve(k); //Wczytanie ilości substancji w probówkach phials.push_back(phial()); for (int i = 1; i <= n; i++) { phial p; scanf("%d", &p.substanceAmt); phials.push_back(p); } //Wczytanie operacji na fiolkach ordering.push_back(0); for (int i = 1; i <= m; i++) { int a, b; scanf("%d %d", &a, &b); phials[a].jumps[0] = b; phials[a].momentOfPouring = i; ordering.push_back(a); } //Wczytanie reguł zachodzenia reakcji for (int i = 0; i < k; i++) { rule r; scanf("%d %d", &r.a, &r.b); r.priority = i; rules.push_back(r); } //Tworzymy strukturę LCA for (int i = 0; i <= m; i++) { //Kolejność, którą dostajemy to odwrotny porządek topologiczny int c = ordering[m - i]; phials[c].level = phials[phials[c].jumps[0]].level + 1; for (int j = 1; j < LOG_K; j++) phials[c].jumps[j] = phials[phials[c].jumps[j - 1]].jumps[j - 1]; } //Liczymy "prawdziwy priorytet" dla reakcji for (int i = 0; i < k; i++) { int lca, pA, pB; comboLCA(rules[i].a, rules[i].b, lca, pA, pB); if (lca == 0) { //Reagenty w zasadzie nigdy nie trafią do jednej fiolki //Unieważniamy tą zadadę rules[i].a = 0; rules[i].b = 0; continue; } if (rules[i].a == lca) rules[i].properPriority = phials[pB].momentOfPouring; else if (rules[i].b == lca) rules[i].properPriority = phials[pA].momentOfPouring; else rules[i].properPriority = std::max(phials[pA].momentOfPouring, phials[pB].momentOfPouring); rules[i].properiestPriority = phials[lca].momentOfPouring; } //Sortujemy względem kolejności stosowania zasad std::sort(rules.begin(), rules.end()); //Wykonujemy zasady na fiolkach long long int sedimentAmt = 0; for (const rule & r : rules) { int reactedAmt = std::min(phials[r.a].substanceAmt, phials[r.b].substanceAmt); phials[r.a].substanceAmt -= reactedAmt; phials[r.b].substanceAmt -= reactedAmt; sedimentAmt += 2 * reactedAmt; } printf("%lld\n", sedimentAmt); return 0; } |