#include <cstdio> #include <cstdlib> #include <ctime> #include <vector> #include <string> #include <queue> #include <map> #include <set> #include <algorithm> using namespace std; //#define MJMREAD typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; #if defined(MJMREAD) int rd[] = {12,11,66, 0,0,0,0,0,0,0,0,0,0,0,0, 1,2,3,4,5,6,7,8,9,10,11,12,2,4,6,8,10,12,4,8,8,12, 1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,1,11,1,12,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2,12,3,4,3,5,3,6,3,7,3,8,3,9,3,10,3,11,3,12,4,5,4,6,4,7,4,8,4,9,4,10,4,11,4,12,5,6,5,7,5,8,5,9,5,10,5,11,5,12,6,7,6,8,6,9,6,10,6,11,6,12,7,8,7,9,7,10,7,11,7,12,8,9,8,10,8,11,8,12,9,10,9,11,9,12,10,11,10,12,11,12}; inline int read() { static int i=0; return rd[i++]; } #else // MJMREAD inline int read() { int n; scanf("%d", &n); return n; } #endif // MJMREAD class FU { std::vector<int> p; public: FU(int n); int f(int x); // find void u(int x, int y); // union bool s(int x, int y); // are x and y in the same set? int a(); // add new element as singleton, return id of new element int size(int x); // return number of elements in set containing x int operator()(int x); // alias for f bool operator()(int x, int y); // alias for s }; FU::FU(int n) : p(n, -1) {} int FU::f(int x) { int t, r = x; while (p[r] >= 0) r = p[r]; while (x != r) t = p[x], p[x] = r, x = t; return r; } void FU::u(int x, int y) { x = f(x); y = f(y); if (x == y) return; if (p[x] < p[y]) p[x] += p[y], p[y] = x; else p[y] += p[x], p[x] = y; } bool FU::s(int x, int y) { return f(x) == f(y); } int FU::a() { p.push_back(-1); return p.size() - 1; } int FU::size(int x) { return -p[f(x)]; } int FU::operator()(int x) { return f(x); } bool FU::operator()(int x, int y) { return s(x, y); } // http://pl.wikipedia.org/wiki/Algorytm_Tarjana void tarjanOLCA(int u, FU &fu, vi &ancestor, vector<bool> &color, vi &child1, vi &child2, vector<pii> &synthesis, vvi &tmp, vvi &final, int &n, int &m) { ancestor[u] = u; if (u >= n) { tarjanOLCA(child1[u-n], fu, ancestor, color, child1, child2, synthesis, tmp, final, n, m); fu.u(u, child1[u-n]); ancestor[fu.f(u)] = u; tarjanOLCA(child2[u-n], fu, ancestor, color, child1, child2, synthesis, tmp, final, n, m); fu.u(u, child2[u-n]); ancestor[fu.f(u)] = u; } color[u] = true; if (u < n) for (int i=0; i<tmp[u].size(); ++i) { int id = tmp[u][i]; int v = synthesis[id].first + synthesis[id].second - u; if (color[v]) final[ancestor[fu.f(v)] - n].push_back(id); } } int main() { int n = read(); int m = read(); int k = read(); vi weight(n); FU phialId(n); vi realPhial(n); vi parent(n+m, -1); vi child1(m), child2(m); vi ancestor(n+m); vector<bool> color(n+m, false); vector<pii> synthesis(k); vvi tmp(n); vvi final(m); set<int> roots; FU fu(n+m); for (int i=0; i<n; ++i) { weight[i] = read(); realPhial[i] = i; } for (int i=0; i<m; ++i) { int a = read() - 1; int b = read() - 1; int aa = phialId.f(a); int bb = phialId.f(b); int aaa = realPhial[aa]; int bbb = realPhial[bb]; phialId.u(aa, bb); realPhial[phialId.f(aa)] = parent[aaa] = parent[bbb] = n+i; roots.erase(aaa); roots.erase(bbb); roots.insert(n+i); child1[i] = aaa; child2[i] = bbb; } for (int i=0; i<k; ++i) { int a = read() - 1; int b = read() - 1; synthesis[i].first = a; synthesis[i].second = b; tmp[a].push_back(i); tmp[b].push_back(i); } for (set<int>::iterator it = roots.begin(); it != roots.end(); ++it) { tarjanOLCA(*it, fu, ancestor, color, child1, child2, synthesis, tmp, final, n, m); } ll res = 0; for (int i=0; i<m; ++i) { for (int j=0; j<final[i].size(); ++j) { int id = final[i][j]; int cur = std::min(weight[synthesis[id].first], weight[synthesis[id].second]); res += 2*cur; weight[synthesis[id].first] -= cur; weight[synthesis[id].second] -= cur; //fprintf(stderr, "%d: %d %d\n", 1+i, 1+synthesis[id].first, 1+synthesis[id].second); } } 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 | #include <cstdio> #include <cstdlib> #include <ctime> #include <vector> #include <string> #include <queue> #include <map> #include <set> #include <algorithm> using namespace std; //#define MJMREAD typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; #if defined(MJMREAD) int rd[] = {12,11,66, 0,0,0,0,0,0,0,0,0,0,0,0, 1,2,3,4,5,6,7,8,9,10,11,12,2,4,6,8,10,12,4,8,8,12, 1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,1,11,1,12,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2,12,3,4,3,5,3,6,3,7,3,8,3,9,3,10,3,11,3,12,4,5,4,6,4,7,4,8,4,9,4,10,4,11,4,12,5,6,5,7,5,8,5,9,5,10,5,11,5,12,6,7,6,8,6,9,6,10,6,11,6,12,7,8,7,9,7,10,7,11,7,12,8,9,8,10,8,11,8,12,9,10,9,11,9,12,10,11,10,12,11,12}; inline int read() { static int i=0; return rd[i++]; } #else // MJMREAD inline int read() { int n; scanf("%d", &n); return n; } #endif // MJMREAD class FU { std::vector<int> p; public: FU(int n); int f(int x); // find void u(int x, int y); // union bool s(int x, int y); // are x and y in the same set? int a(); // add new element as singleton, return id of new element int size(int x); // return number of elements in set containing x int operator()(int x); // alias for f bool operator()(int x, int y); // alias for s }; FU::FU(int n) : p(n, -1) {} int FU::f(int x) { int t, r = x; while (p[r] >= 0) r = p[r]; while (x != r) t = p[x], p[x] = r, x = t; return r; } void FU::u(int x, int y) { x = f(x); y = f(y); if (x == y) return; if (p[x] < p[y]) p[x] += p[y], p[y] = x; else p[y] += p[x], p[x] = y; } bool FU::s(int x, int y) { return f(x) == f(y); } int FU::a() { p.push_back(-1); return p.size() - 1; } int FU::size(int x) { return -p[f(x)]; } int FU::operator()(int x) { return f(x); } bool FU::operator()(int x, int y) { return s(x, y); } // http://pl.wikipedia.org/wiki/Algorytm_Tarjana void tarjanOLCA(int u, FU &fu, vi &ancestor, vector<bool> &color, vi &child1, vi &child2, vector<pii> &synthesis, vvi &tmp, vvi &final, int &n, int &m) { ancestor[u] = u; if (u >= n) { tarjanOLCA(child1[u-n], fu, ancestor, color, child1, child2, synthesis, tmp, final, n, m); fu.u(u, child1[u-n]); ancestor[fu.f(u)] = u; tarjanOLCA(child2[u-n], fu, ancestor, color, child1, child2, synthesis, tmp, final, n, m); fu.u(u, child2[u-n]); ancestor[fu.f(u)] = u; } color[u] = true; if (u < n) for (int i=0; i<tmp[u].size(); ++i) { int id = tmp[u][i]; int v = synthesis[id].first + synthesis[id].second - u; if (color[v]) final[ancestor[fu.f(v)] - n].push_back(id); } } int main() { int n = read(); int m = read(); int k = read(); vi weight(n); FU phialId(n); vi realPhial(n); vi parent(n+m, -1); vi child1(m), child2(m); vi ancestor(n+m); vector<bool> color(n+m, false); vector<pii> synthesis(k); vvi tmp(n); vvi final(m); set<int> roots; FU fu(n+m); for (int i=0; i<n; ++i) { weight[i] = read(); realPhial[i] = i; } for (int i=0; i<m; ++i) { int a = read() - 1; int b = read() - 1; int aa = phialId.f(a); int bb = phialId.f(b); int aaa = realPhial[aa]; int bbb = realPhial[bb]; phialId.u(aa, bb); realPhial[phialId.f(aa)] = parent[aaa] = parent[bbb] = n+i; roots.erase(aaa); roots.erase(bbb); roots.insert(n+i); child1[i] = aaa; child2[i] = bbb; } for (int i=0; i<k; ++i) { int a = read() - 1; int b = read() - 1; synthesis[i].first = a; synthesis[i].second = b; tmp[a].push_back(i); tmp[b].push_back(i); } for (set<int>::iterator it = roots.begin(); it != roots.end(); ++it) { tarjanOLCA(*it, fu, ancestor, color, child1, child2, synthesis, tmp, final, n, m); } ll res = 0; for (int i=0; i<m; ++i) { for (int j=0; j<final[i].size(); ++j) { int id = final[i][j]; int cur = std::min(weight[synthesis[id].first], weight[synthesis[id].second]); res += 2*cur; weight[synthesis[id].first] -= cur; weight[synthesis[id].second] -= cur; //fprintf(stderr, "%d: %d %d\n", 1+i, 1+synthesis[id].first, 1+synthesis[id].second); } } printf("%lld\n", res); return 0; } |