#include <iostream> #include <vector> #include <utility> #include <set> #include <cassert> #include <algorithm> using namespace std; class UnionFind; class Vial; class Reaction; typedef long long int LL; class UnionFind { public: /* elements are 1..size */ UnionFind(int size) { sizes = vector<int>(size+1); for(int i = 1; i <= size; ++i) sizes[i] = 1; representatives = vector<int>(size+1); for(int i = 1; i <= size; ++i) representatives[i] = i; } void unionf(int a, int b) { int aRep = find(a); int bRep = find(b); if(sizes[aRep] < sizes[bRep]) representatives[aRep] = bRep; else if(sizes[aRep] > sizes[bRep]) representatives[bRep] = aRep; else if(aRep != bRep) { sizes[aRep]++; representatives[bRep] = aRep; } } int find(int a) { if(a == representatives[a]) return a; representatives[a] = find(representatives[a]); return representatives[a]; } private: vector<int> sizes; vector<int> representatives; }; class Vial { public: static Vial load(int nr) { int amount; cin >> amount; return Vial(nr, amount); } Vial(): nr(0), amount(0) {} Vial(int nr, int amount): nr(nr), amount(amount), reactionsAdded(false) { supervial = NULL; fatherVialForReactions = NULL; } int getNr() { return nr; } bool hasSuperVial() { return supervial != NULL; } void assignSuperVial(Vial * other) { supervial = other; other->subvials.push_back(this); } void clear() { reactionsAdded = false; for(auto subvial: subvials) subvial->clear(); } void print(); void addReactions(vector<vector<Reaction>> & reactions, UnionFind & uf, vector<Vial> & vials, int & visitNrPassed); void insertReactionByMaxVisitNr(Reaction & reaction, int maxVisitNr); LL calculateWaste(); private: vector<Vial *> subvials; vector<set<Reaction>> vialReactions; vector<int> visitThresholds; Vial * supervial; int nr; int amount; int visitNr; bool reactionsAdded; Vial * fatherVialForReactions; }; class Reaction { public: Reaction(Vial * one, Vial * other, int priority): one(one), other(other), priority(priority) {}; Vial * one; Vial * other; int priority; bool operator<(const Reaction & other) const { return priority < other.priority; } void print() { cout << "first: " << one->getNr() << ", second: " << other->getNr() << ", priority " << priority << endl; } }; void Vial::addReactions(vector<vector<Reaction>> & reactions, UnionFind & uf, vector<Vial> & vials, int & visitNrPassed) { //cout << "adding reactions for " << nr << endl; fatherVialForReactions = this; visitThresholds.push_back(visitNrPassed); visitNr = visitNrPassed; visitNrPassed++; vialReactions.push_back(set<Reaction>()); for(auto subvial : subvials) { subvial->addReactions(reactions, uf, vials, visitNrPassed); uf.unionf(subvial->nr,nr); vials[uf.find(nr)].fatherVialForReactions = this; vialReactions.push_back(set<Reaction>()); visitThresholds.push_back(visitNrPassed); visitNrPassed++; } reactionsAdded = true; for(auto reaction: reactions[nr]) { if(reaction.other->reactionsAdded > 0) { int repVialNr = uf.find(reaction.other->nr); int maxVisitNr = max(reaction.one->visitNr, reaction.other->visitNr); Vial * lcaVial = vials[repVialNr].fatherVialForReactions; assert(vials[repVialNr].fatherVialForReactions != NULL); lcaVial->insertReactionByMaxVisitNr(reaction, maxVisitNr); } } } inline void Vial::insertReactionByMaxVisitNr(Reaction & reaction, int maxVisitNr) { auto iterForReaction = upper_bound(visitThresholds.begin(), visitThresholds.end(), maxVisitNr); int positionForReaction = iterForReaction - visitThresholds.begin()-1; vialReactions[positionForReaction].insert(reaction); } LL Vial::calculateWaste() { LL waste = 0; for(auto subvial: subvials) waste += subvial->calculateWaste(); for(auto reactionSet: vialReactions) { for(auto reaction: reactionSet) { LL reactionWaste = min(reaction.one->amount, reaction.other->amount); reaction.one->amount -= reactionWaste; reaction.other->amount -= reactionWaste; waste += 2*reactionWaste; } } return waste; } void Vial::print() { cout << "Vial " << nr << ":\n"; if(hasSuperVial()) cout << "Supervial: " << supervial->nr << endl; if(fatherVialForReactions != NULL) cout << "current fVial: " << fatherVialForReactions->nr << endl; cout << "Reactions: \n"; for(auto reactionSet: vialReactions) { for(auto reaction: reactionSet) reaction.print(); } } int main() { ios_base::sync_with_stdio(0); int vialCount; int stepCount; int reactionsCount; cin >> vialCount >> stepCount >> reactionsCount; vector<Vial> allVials(1); vector<vector<Reaction>> reactions(vialCount+1); UnionFind uf(vialCount); for(int i = 1; i <= vialCount; ++i) { allVials.push_back(Vial::load(i)); } for(int i = 1; i <= stepCount; ++i) { int from, to; cin >> from >> to; allVials[from].assignSuperVial(&allVials[to]); } for(int i = 0; i < reactionsCount; ++i) { int react1, react2; cin >> react1 >> react2; reactions[react1].push_back(Reaction(&allVials[react1], &allVials[react2], i)); reactions[react2].push_back(Reaction(&allVials[react2], &allVials[react1], i)); } for(auto & vial: allVials) { if(!vial.hasSuperVial()) { int visitNrPassed = 0; vial.addReactions(reactions, uf, allVials, visitNrPassed); vial.clear(); } } LL totalWaste = 0; for(auto & vial: allVials) { if(!vial.hasSuperVial()) { LL waste = vial.calculateWaste(); totalWaste += waste; } } cout << totalWaste << endl; }
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | #include <iostream> #include <vector> #include <utility> #include <set> #include <cassert> #include <algorithm> using namespace std; class UnionFind; class Vial; class Reaction; typedef long long int LL; class UnionFind { public: /* elements are 1..size */ UnionFind(int size) { sizes = vector<int>(size+1); for(int i = 1; i <= size; ++i) sizes[i] = 1; representatives = vector<int>(size+1); for(int i = 1; i <= size; ++i) representatives[i] = i; } void unionf(int a, int b) { int aRep = find(a); int bRep = find(b); if(sizes[aRep] < sizes[bRep]) representatives[aRep] = bRep; else if(sizes[aRep] > sizes[bRep]) representatives[bRep] = aRep; else if(aRep != bRep) { sizes[aRep]++; representatives[bRep] = aRep; } } int find(int a) { if(a == representatives[a]) return a; representatives[a] = find(representatives[a]); return representatives[a]; } private: vector<int> sizes; vector<int> representatives; }; class Vial { public: static Vial load(int nr) { int amount; cin >> amount; return Vial(nr, amount); } Vial(): nr(0), amount(0) {} Vial(int nr, int amount): nr(nr), amount(amount), reactionsAdded(false) { supervial = NULL; fatherVialForReactions = NULL; } int getNr() { return nr; } bool hasSuperVial() { return supervial != NULL; } void assignSuperVial(Vial * other) { supervial = other; other->subvials.push_back(this); } void clear() { reactionsAdded = false; for(auto subvial: subvials) subvial->clear(); } void print(); void addReactions(vector<vector<Reaction>> & reactions, UnionFind & uf, vector<Vial> & vials, int & visitNrPassed); void insertReactionByMaxVisitNr(Reaction & reaction, int maxVisitNr); LL calculateWaste(); private: vector<Vial *> subvials; vector<set<Reaction>> vialReactions; vector<int> visitThresholds; Vial * supervial; int nr; int amount; int visitNr; bool reactionsAdded; Vial * fatherVialForReactions; }; class Reaction { public: Reaction(Vial * one, Vial * other, int priority): one(one), other(other), priority(priority) {}; Vial * one; Vial * other; int priority; bool operator<(const Reaction & other) const { return priority < other.priority; } void print() { cout << "first: " << one->getNr() << ", second: " << other->getNr() << ", priority " << priority << endl; } }; void Vial::addReactions(vector<vector<Reaction>> & reactions, UnionFind & uf, vector<Vial> & vials, int & visitNrPassed) { //cout << "adding reactions for " << nr << endl; fatherVialForReactions = this; visitThresholds.push_back(visitNrPassed); visitNr = visitNrPassed; visitNrPassed++; vialReactions.push_back(set<Reaction>()); for(auto subvial : subvials) { subvial->addReactions(reactions, uf, vials, visitNrPassed); uf.unionf(subvial->nr,nr); vials[uf.find(nr)].fatherVialForReactions = this; vialReactions.push_back(set<Reaction>()); visitThresholds.push_back(visitNrPassed); visitNrPassed++; } reactionsAdded = true; for(auto reaction: reactions[nr]) { if(reaction.other->reactionsAdded > 0) { int repVialNr = uf.find(reaction.other->nr); int maxVisitNr = max(reaction.one->visitNr, reaction.other->visitNr); Vial * lcaVial = vials[repVialNr].fatherVialForReactions; assert(vials[repVialNr].fatherVialForReactions != NULL); lcaVial->insertReactionByMaxVisitNr(reaction, maxVisitNr); } } } inline void Vial::insertReactionByMaxVisitNr(Reaction & reaction, int maxVisitNr) { auto iterForReaction = upper_bound(visitThresholds.begin(), visitThresholds.end(), maxVisitNr); int positionForReaction = iterForReaction - visitThresholds.begin()-1; vialReactions[positionForReaction].insert(reaction); } LL Vial::calculateWaste() { LL waste = 0; for(auto subvial: subvials) waste += subvial->calculateWaste(); for(auto reactionSet: vialReactions) { for(auto reaction: reactionSet) { LL reactionWaste = min(reaction.one->amount, reaction.other->amount); reaction.one->amount -= reactionWaste; reaction.other->amount -= reactionWaste; waste += 2*reactionWaste; } } return waste; } void Vial::print() { cout << "Vial " << nr << ":\n"; if(hasSuperVial()) cout << "Supervial: " << supervial->nr << endl; if(fatherVialForReactions != NULL) cout << "current fVial: " << fatherVialForReactions->nr << endl; cout << "Reactions: \n"; for(auto reactionSet: vialReactions) { for(auto reaction: reactionSet) reaction.print(); } } int main() { ios_base::sync_with_stdio(0); int vialCount; int stepCount; int reactionsCount; cin >> vialCount >> stepCount >> reactionsCount; vector<Vial> allVials(1); vector<vector<Reaction>> reactions(vialCount+1); UnionFind uf(vialCount); for(int i = 1; i <= vialCount; ++i) { allVials.push_back(Vial::load(i)); } for(int i = 1; i <= stepCount; ++i) { int from, to; cin >> from >> to; allVials[from].assignSuperVial(&allVials[to]); } for(int i = 0; i < reactionsCount; ++i) { int react1, react2; cin >> react1 >> react2; reactions[react1].push_back(Reaction(&allVials[react1], &allVials[react2], i)); reactions[react2].push_back(Reaction(&allVials[react2], &allVials[react1], i)); } for(auto & vial: allVials) { if(!vial.hasSuperVial()) { int visitNrPassed = 0; vial.addReactions(reactions, uf, allVials, visitNrPassed); vial.clear(); } } LL totalWaste = 0; for(auto & vial: allVials) { if(!vial.hasSuperVial()) { LL waste = vial.calculateWaste(); totalWaste += waste; } } cout << totalWaste << endl; } |