#include <iostream> #include <vector> #include <list> #include <algorithm> using namespace std; typedef long long int LL; LL n, m, k; LL g[200001]; LL TopSort[200001]; LL kolor[200001]; bool cykl = false; vector <LL> Graf[200001]; vector <LL> w_i_probowce[200001]; vector <LL> tab; void dfs(int x) { kolor[x] = 1; for(int y = 0 ; y < Graf[x].size(); y++) { if(kolor[Graf[x][y]] == 0) { dfs(Graf[x][y]); } else if(kolor[Graf[x][y]] == 1) { cykl = true; } } tab.push_back(x); kolor[x] = 2; } struct reakcja { LL c, d; }; list <reakcja> P; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for(int i = 1; i <= n; i++) { cin >> g[i]; w_i_probowce[i].push_back(i); TopSort[i] = 0; kolor[i] = 0; } for(int i = 1; i <= m; i++) { int a, b; cin >> a >> b; Graf[a].push_back(b); } for(int i = 1; i <= k; i++) { reakcja HASH; cin >> HASH.c >> HASH.d; P.push_back(HASH); } for(int i = 1; i <= n; i++) if(kolor[i] != 2) dfs(i); for(int i = tab.size() - 1; i >= 0; i--) TopSort[tab.size()- i] = tab[i]; LL strata = 0; for(int i = 1; i <= n; i++) { LL wychodzimy_z = TopSort[i]; if(Graf[TopSort[i]].size() != 0) { LL idziemy_do = Graf[TopSort[i]][0]; vector <LL> subs_w_start = w_i_probowce[wychodzimy_z]; vector <LL> subs_w_koniec = w_i_probowce[idziemy_do]; for(int i = 0; i < subs_w_start.size(); i++) subs_w_koniec.push_back(subs_w_start[i]); sort(subs_w_koniec.begin(), subs_w_koniec.end()); for(list <reakcja> :: iterator X = P.begin(); X != P.end(); X++) { if(binary_search(subs_w_koniec.begin(), subs_w_koniec.end(), X->c) && binary_search(subs_w_koniec.begin(), subs_w_koniec.end(), X->d)) { int ilosc_c = g[X->c], ilosc_d = g[X->d]; int mart = min(ilosc_c, ilosc_d); strata += 2 * mart; g[X->c] -= mart; g[X->d] -= mart; } } } } cout << strata << 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 | #include <iostream> #include <vector> #include <list> #include <algorithm> using namespace std; typedef long long int LL; LL n, m, k; LL g[200001]; LL TopSort[200001]; LL kolor[200001]; bool cykl = false; vector <LL> Graf[200001]; vector <LL> w_i_probowce[200001]; vector <LL> tab; void dfs(int x) { kolor[x] = 1; for(int y = 0 ; y < Graf[x].size(); y++) { if(kolor[Graf[x][y]] == 0) { dfs(Graf[x][y]); } else if(kolor[Graf[x][y]] == 1) { cykl = true; } } tab.push_back(x); kolor[x] = 2; } struct reakcja { LL c, d; }; list <reakcja> P; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for(int i = 1; i <= n; i++) { cin >> g[i]; w_i_probowce[i].push_back(i); TopSort[i] = 0; kolor[i] = 0; } for(int i = 1; i <= m; i++) { int a, b; cin >> a >> b; Graf[a].push_back(b); } for(int i = 1; i <= k; i++) { reakcja HASH; cin >> HASH.c >> HASH.d; P.push_back(HASH); } for(int i = 1; i <= n; i++) if(kolor[i] != 2) dfs(i); for(int i = tab.size() - 1; i >= 0; i--) TopSort[tab.size()- i] = tab[i]; LL strata = 0; for(int i = 1; i <= n; i++) { LL wychodzimy_z = TopSort[i]; if(Graf[TopSort[i]].size() != 0) { LL idziemy_do = Graf[TopSort[i]][0]; vector <LL> subs_w_start = w_i_probowce[wychodzimy_z]; vector <LL> subs_w_koniec = w_i_probowce[idziemy_do]; for(int i = 0; i < subs_w_start.size(); i++) subs_w_koniec.push_back(subs_w_start[i]); sort(subs_w_koniec.begin(), subs_w_koniec.end()); for(list <reakcja> :: iterator X = P.begin(); X != P.end(); X++) { if(binary_search(subs_w_koniec.begin(), subs_w_koniec.end(), X->c) && binary_search(subs_w_koniec.begin(), subs_w_koniec.end(), X->d)) { int ilosc_c = g[X->c], ilosc_d = g[X->d]; int mart = min(ilosc_c, ilosc_d); strata += 2 * mart; g[X->c] -= mart; g[X->d] -= mart; } } } } cout << strata << endl; return 0; } |