#include<cstdio> #include<vector> #include<algorithm> #include<map> using namespace std; typedef pair<int,int> pii; int n; /// number of products int m; /// number of operations int k; /// number of reactions vector<int> weight; /// weight of u vector<int> number; /// number[u] = time when u is being used vector< vector<int> > child; /// child[u] = { v : v -> u is an operation } vector< vector<pii> > react; /// react[u] = { (v,i) : (u,v) is an reaction with number i } vector<pii> reactions; /// list of reactions (from the input) vector<int> lca; vector<int> anchestor; /// for Taran algorithm computing LCA; see http://en.wikipedia.org/wiki/Tarjan%27s_off-line_lowest_common_ancestors_algorithm vector<int> colour; /// colours for Tarjan algorithm vector<int> counter; /// Union-Find vector<int> father; /// Union-Find vector< vector<int> > lcaadj; map< pii, int > pathmax; // pathmax( (u,v) ) = max{ number[u1], number[u2], ..., number[un] } where u1,u2...,un is a path fromu to v. vector< vector< pair<int,pii> > > todo; // todo[t] = vector of operation that should be done at the moment t int Find(int a) { return (father[a]==a) ? a : (father[a] = Find( father[a] )); } void Union(int a, int b) { int fa = Find(a), fb = Find(b); if ( counter[fa] > counter[fb] ) swap(fa, fb); father[fa] = fb; counter[fb] += counter[fa]; } int black; void Tarjan(int u) { //fprintf(stderr, "Tarjan(%d)\n", u); anchestor[u] = u; for (int v : child[u]) { Tarjan(v); Union(u,v); anchestor[ Find(u) ] = u; } colour[u] = black; // = true; for (pii V : react[u]) { int v = V.first, nr = V.second; if ( colour[v]==black ) { // LCA(u,v) is anchestor[ Find(v) ] // thus the reaction (u,v) will have place at number[ anchestor[Find(v)] ] int c = anchestor[Find(v)]; //fprintf(stderr, "lca_tarjan(%d,%d)=%d\n", u,v,c); lca[nr] = c; if ( u!=c ) lcaadj[u].push_back(c); if ( v!=c ) lcaadj[v].push_back(c); } } } void dfs(int u) { //fprintf(stderr, "dfs(%d)\n", u); for ( int v : lcaadj[u] ) pathmax[ pii(min(u,v), max(u,v)) ] = colour[v]; for ( int v : child[u] ) { colour[u] = number[v]; dfs(v); } } int main() { // read n,m,k scanf("%d%d%d", &n, &m, &k); // read the weights of products weight.resize(n+1); for (int u=1; u<=n; u++) scanf("%d", &weight[u]); // read the operations v -> u number.resize(n+1,n+1); child.resize(n+1); //vector<bool> root(n+1, true); for (int nr = 1; nr<=m; nr++) { int v, u; scanf("%d%d", &v, &u); child[u].push_back(v); number[v] = nr; //root[v] = false; } vector<int> roots; for (int u=1; u<=n; u++) if ( number[u] > n ) roots.push_back(u); // read the reactions react.resize(n+1); reactions.resize(k+1); for (int i=1; i<=k; i++) { int u, v; scanf("%d%d", &u, &v); if (u>v) swap(u,v); react[u].push_back(pii(v,i)); react[v].push_back(pii(u,i)); reactions[i] = pii(u,v); } // Union-Find initialization counter.resize(n+1, 1); father.resize(n+1); for (int u=0; u<=n; u++) { father[u] = u; } todo.resize(n+2); // Run Tarjan algorithm in every root colour.resize(n+1, 0); anchestor.resize(n+1); lca.resize(k+1); lcaadj.resize(n+1); black = 0; for (int r : roots) { black++; //fprintf(stderr, "Running Tarjan in the root %d\n", r); Tarjan(r); } anchestor.resize(0); counter.resize(0); father.resize(0); colour.resize(0); colour.resize(n+1, 0); for (int r : roots) dfs(r); //for (int u=1; u<=n; u++) for (auto v : lcaadj[u]) fprintf(stderr, "lcaadj[%d][%d] with max=%d\n", u,v, (u==v)?0:pathmax[ pii(min(u,v), max(u,v)) ]); todo.resize(n+2); for (int nr=1; nr<=k; nr++) { int u = reactions[nr].first, v = reactions[nr].second; int c = lca[nr]; //fprintf(stderr, "lca[%d:(%d,%d)]=%d\n", nr, u,v, c); int uc = (u==c) ? 0 : pathmax[ pii( min(u,c), max(u,c) ) ]; int vc = (v==c) ? 0 : pathmax[ pii( min(v,c), max(v,c) ) ]; int t = max(uc, vc); //fprintf(stderr, "todo[%d].push_back %d:(%d,%d)\n", t, nr, u,v); todo[t].push_back( make_pair(nr,pii(u,v)) ); } long long totalResidue = 0; for ( int t = 1; t<=n+1; t++ ) { sort( todo[t].begin(), todo[t].end() ); for ( auto reaction : todo[t] ) { int u = reaction.second.first, v = reaction.second.second; if ( weight[u] > weight[v] ) swap(u,v); //fprintf(stderr, "Reaction %d (%d grams) with %d (%d grams)\n", u, weight[u], v, weight[v]); totalResidue += weight[u]; weight[v] -= weight[u]; weight[u] = 0; } } printf("%lld\n", 2*totalResidue); 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 | #include<cstdio> #include<vector> #include<algorithm> #include<map> using namespace std; typedef pair<int,int> pii; int n; /// number of products int m; /// number of operations int k; /// number of reactions vector<int> weight; /// weight of u vector<int> number; /// number[u] = time when u is being used vector< vector<int> > child; /// child[u] = { v : v -> u is an operation } vector< vector<pii> > react; /// react[u] = { (v,i) : (u,v) is an reaction with number i } vector<pii> reactions; /// list of reactions (from the input) vector<int> lca; vector<int> anchestor; /// for Taran algorithm computing LCA; see http://en.wikipedia.org/wiki/Tarjan%27s_off-line_lowest_common_ancestors_algorithm vector<int> colour; /// colours for Tarjan algorithm vector<int> counter; /// Union-Find vector<int> father; /// Union-Find vector< vector<int> > lcaadj; map< pii, int > pathmax; // pathmax( (u,v) ) = max{ number[u1], number[u2], ..., number[un] } where u1,u2...,un is a path fromu to v. vector< vector< pair<int,pii> > > todo; // todo[t] = vector of operation that should be done at the moment t int Find(int a) { return (father[a]==a) ? a : (father[a] = Find( father[a] )); } void Union(int a, int b) { int fa = Find(a), fb = Find(b); if ( counter[fa] > counter[fb] ) swap(fa, fb); father[fa] = fb; counter[fb] += counter[fa]; } int black; void Tarjan(int u) { //fprintf(stderr, "Tarjan(%d)\n", u); anchestor[u] = u; for (int v : child[u]) { Tarjan(v); Union(u,v); anchestor[ Find(u) ] = u; } colour[u] = black; // = true; for (pii V : react[u]) { int v = V.first, nr = V.second; if ( colour[v]==black ) { // LCA(u,v) is anchestor[ Find(v) ] // thus the reaction (u,v) will have place at number[ anchestor[Find(v)] ] int c = anchestor[Find(v)]; //fprintf(stderr, "lca_tarjan(%d,%d)=%d\n", u,v,c); lca[nr] = c; if ( u!=c ) lcaadj[u].push_back(c); if ( v!=c ) lcaadj[v].push_back(c); } } } void dfs(int u) { //fprintf(stderr, "dfs(%d)\n", u); for ( int v : lcaadj[u] ) pathmax[ pii(min(u,v), max(u,v)) ] = colour[v]; for ( int v : child[u] ) { colour[u] = number[v]; dfs(v); } } int main() { // read n,m,k scanf("%d%d%d", &n, &m, &k); // read the weights of products weight.resize(n+1); for (int u=1; u<=n; u++) scanf("%d", &weight[u]); // read the operations v -> u number.resize(n+1,n+1); child.resize(n+1); //vector<bool> root(n+1, true); for (int nr = 1; nr<=m; nr++) { int v, u; scanf("%d%d", &v, &u); child[u].push_back(v); number[v] = nr; //root[v] = false; } vector<int> roots; for (int u=1; u<=n; u++) if ( number[u] > n ) roots.push_back(u); // read the reactions react.resize(n+1); reactions.resize(k+1); for (int i=1; i<=k; i++) { int u, v; scanf("%d%d", &u, &v); if (u>v) swap(u,v); react[u].push_back(pii(v,i)); react[v].push_back(pii(u,i)); reactions[i] = pii(u,v); } // Union-Find initialization counter.resize(n+1, 1); father.resize(n+1); for (int u=0; u<=n; u++) { father[u] = u; } todo.resize(n+2); // Run Tarjan algorithm in every root colour.resize(n+1, 0); anchestor.resize(n+1); lca.resize(k+1); lcaadj.resize(n+1); black = 0; for (int r : roots) { black++; //fprintf(stderr, "Running Tarjan in the root %d\n", r); Tarjan(r); } anchestor.resize(0); counter.resize(0); father.resize(0); colour.resize(0); colour.resize(n+1, 0); for (int r : roots) dfs(r); //for (int u=1; u<=n; u++) for (auto v : lcaadj[u]) fprintf(stderr, "lcaadj[%d][%d] with max=%d\n", u,v, (u==v)?0:pathmax[ pii(min(u,v), max(u,v)) ]); todo.resize(n+2); for (int nr=1; nr<=k; nr++) { int u = reactions[nr].first, v = reactions[nr].second; int c = lca[nr]; //fprintf(stderr, "lca[%d:(%d,%d)]=%d\n", nr, u,v, c); int uc = (u==c) ? 0 : pathmax[ pii( min(u,c), max(u,c) ) ]; int vc = (v==c) ? 0 : pathmax[ pii( min(v,c), max(v,c) ) ]; int t = max(uc, vc); //fprintf(stderr, "todo[%d].push_back %d:(%d,%d)\n", t, nr, u,v); todo[t].push_back( make_pair(nr,pii(u,v)) ); } long long totalResidue = 0; for ( int t = 1; t<=n+1; t++ ) { sort( todo[t].begin(), todo[t].end() ); for ( auto reaction : todo[t] ) { int u = reaction.second.first, v = reaction.second.second; if ( weight[u] > weight[v] ) swap(u,v); //fprintf(stderr, "Reaction %d (%d grams) with %d (%d grams)\n", u, weight[u], v, weight[v]); totalResidue += weight[u]; weight[v] -= weight[u]; weight[u] = 0; } } printf("%lld\n", 2*totalResidue); return 0; } |