#include <cstdio> #include <vector> #include <algorithm> #include <cstdlib> #include <set> #include <random> #define REP(i,n) for(int i=0; i<(n); ++i) #define FOR(i,p,k) for(int i=(p); i<=(k); ++i) #define FORD(i,p,k) for(int i=(p); i>=(k); --i) #define DEBUG if(0) enum { INF = 1000000 }; typedef long long LL; std::mt19937 rnd(78263842); struct Reac { int a,b,l; bool operator<(const Reac &c) const { return l>c.l; } }; void print(const char *name, std::vector<int> &v) { printf("%s =",name); for(int i : v) printf(" %d",i); puts(""); } struct In { typedef unsigned int ui; std::vector<int> G; std::vector<Reac> A,C; void read() { int n,m,k; scanf("%d%d%d",&n,&m,&k); G.resize(n); REP(i,n) scanf("%d",&G[i]); A.resize(m); REP(i,m){ scanf("%d%d",&A[i].a,&A[i].b); A[i].a--; A[i].b--; } C.resize(k); REP(i,k){ scanf("%d%d",&C[i].a,&C[i].b); C[i].a--; C[i].b--; } } void gen_max(){ gen(2e5,2e5 - 3,5e5,1e9); } void gen(ui n = 100, ui m = 80, ui k = 70, ui x = 1000) { G.resize(n); REP(i,n) G[i] = rnd()%x; std::vector<int> X(n); REP(i,n) X[i] = i; A.resize(m); for(auto &r : A) { std::swap(X[rnd()%X.size()],X.back()); r.a = X.back(); X.pop_back(); r.b = X[rnd()%X.size()]; } C.resize(k); for(auto &r : C) while(1) { r = Reac{int(rnd()%n),int(rnd()%n)}; if(r.a!=r.b) break; } } void print() { ::print("G",G); printf("A = "); REP(i,A.size()) printf("(%d,%d) ",A[i].a,A[i].b); puts(""); printf("C = "); REP(i,C.size()) printf("(%d,%d) ",C[i].a,C[i].b); puts(""); } }; int bits(unsigned int n) { return 32-__builtin_clz(n); } struct RMQ { int h,n; int & at(int hh, int nn){ return A[hh*n+nn]; } std::vector<int> A; void init(std::vector<int> &b) { n = b.size(); h = bits(n); A.resize(bits(n)*n); REP(i,n) at(0,i) = b[i]; REP(hh,h-1) REP(i,n+1-(1<<hh+1)) at(hh+1,i) = std::min(at(hh,i),at(hh,i+(1<<hh))); } int query(int a, int b) { if(b<a) std::swap(a,b); int hh = bits(b-a+1)-1; return std::min(at(hh,a),at(hh,b+1-(1<<hh))); } }; struct Graph { void init(int n){ V.clear(); V.resize(n); } std::vector<std::vector<int>> V; void add(int a, int b){ V[b].push_back(a); } }; struct Solution { Graph G; std::vector<int> depth,pos; void dfs(int v, int d) { for(int w : G.V[v]) { dfs(w,d+1); depth.push_back(d++); } pos[v] = depth.size(); depth.push_back(d); } LL run(const In &I) { int n = I.G.size(); G.init(n); std::vector<int> leaf(n,0); FORD(i,I.A.size()-1,0){ G.add(I.A[i].a,I.A[i].b); leaf[I.A[i].a] = 1; } pos.resize(n); REP(i,n) if(!leaf[i]){ dfs(i,0); depth.push_back(-1); } DEBUG print("pos",pos); DEBUG print("depth",depth); RMQ R; R.init(depth); std::vector<Reac> C(I.C.begin(),I.C.end()); for(auto &r : C) r.l = R.query(pos[r.a],pos[r.b]); std::stable_sort(C.begin(),C.end()); DEBUG for(auto r : C) printf("%d %d (%d)\n",r.a,r.b,r.l); std::vector<int> G(I.G.begin(),I.G.end()); LL res = 0; for(auto r : C) if(r.l!=-1) { int s = std::min(G[r.a],G[r.b]); res += 2*s; G[r.a] -= s; G[r.b] -= s; } return res; } }; LL brute(const In &I) { int n = I.G.size(); LL res = 0; std::vector<int> G(I.G.begin(),I.G.end()); std::vector<std::set<int>> F(n); REP(i,n) F[i].insert(i); for(auto r : I.A) { for(auto c : I.C) if(F[r.a].count(c.a) && F[r.b].count(c.b) || F[r.a].count(c.b) && F[r.b].count(c.a)) { LL s = std::min(G[c.a],G[c.b]); res += 2*s; G[c.a] -= s; G[c.b] -= s; } F[r.b].insert(F[r.a].begin(),F[r.a].end()); } return res; } void test() { REP(_,5) { puts("max test"); In I; I.gen_max(); Solution S; S.run(I); } for(int c=0;; c++) { In I; I.gen(); Solution S; LL s = S.run(I); LL b = brute(I); if(s!=b) { printf("ERROR s=%lld; b=%lld\n",s,b); I.print(); exit(1); } if(!(c%1000)) printf("ok %d\n",c); } } int main() { //test(); In I; I.read(); Solution S; printf("%lld\n",S.run(I)); 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 | #include <cstdio> #include <vector> #include <algorithm> #include <cstdlib> #include <set> #include <random> #define REP(i,n) for(int i=0; i<(n); ++i) #define FOR(i,p,k) for(int i=(p); i<=(k); ++i) #define FORD(i,p,k) for(int i=(p); i>=(k); --i) #define DEBUG if(0) enum { INF = 1000000 }; typedef long long LL; std::mt19937 rnd(78263842); struct Reac { int a,b,l; bool operator<(const Reac &c) const { return l>c.l; } }; void print(const char *name, std::vector<int> &v) { printf("%s =",name); for(int i : v) printf(" %d",i); puts(""); } struct In { typedef unsigned int ui; std::vector<int> G; std::vector<Reac> A,C; void read() { int n,m,k; scanf("%d%d%d",&n,&m,&k); G.resize(n); REP(i,n) scanf("%d",&G[i]); A.resize(m); REP(i,m){ scanf("%d%d",&A[i].a,&A[i].b); A[i].a--; A[i].b--; } C.resize(k); REP(i,k){ scanf("%d%d",&C[i].a,&C[i].b); C[i].a--; C[i].b--; } } void gen_max(){ gen(2e5,2e5 - 3,5e5,1e9); } void gen(ui n = 100, ui m = 80, ui k = 70, ui x = 1000) { G.resize(n); REP(i,n) G[i] = rnd()%x; std::vector<int> X(n); REP(i,n) X[i] = i; A.resize(m); for(auto &r : A) { std::swap(X[rnd()%X.size()],X.back()); r.a = X.back(); X.pop_back(); r.b = X[rnd()%X.size()]; } C.resize(k); for(auto &r : C) while(1) { r = Reac{int(rnd()%n),int(rnd()%n)}; if(r.a!=r.b) break; } } void print() { ::print("G",G); printf("A = "); REP(i,A.size()) printf("(%d,%d) ",A[i].a,A[i].b); puts(""); printf("C = "); REP(i,C.size()) printf("(%d,%d) ",C[i].a,C[i].b); puts(""); } }; int bits(unsigned int n) { return 32-__builtin_clz(n); } struct RMQ { int h,n; int & at(int hh, int nn){ return A[hh*n+nn]; } std::vector<int> A; void init(std::vector<int> &b) { n = b.size(); h = bits(n); A.resize(bits(n)*n); REP(i,n) at(0,i) = b[i]; REP(hh,h-1) REP(i,n+1-(1<<hh+1)) at(hh+1,i) = std::min(at(hh,i),at(hh,i+(1<<hh))); } int query(int a, int b) { if(b<a) std::swap(a,b); int hh = bits(b-a+1)-1; return std::min(at(hh,a),at(hh,b+1-(1<<hh))); } }; struct Graph { void init(int n){ V.clear(); V.resize(n); } std::vector<std::vector<int>> V; void add(int a, int b){ V[b].push_back(a); } }; struct Solution { Graph G; std::vector<int> depth,pos; void dfs(int v, int d) { for(int w : G.V[v]) { dfs(w,d+1); depth.push_back(d++); } pos[v] = depth.size(); depth.push_back(d); } LL run(const In &I) { int n = I.G.size(); G.init(n); std::vector<int> leaf(n,0); FORD(i,I.A.size()-1,0){ G.add(I.A[i].a,I.A[i].b); leaf[I.A[i].a] = 1; } pos.resize(n); REP(i,n) if(!leaf[i]){ dfs(i,0); depth.push_back(-1); } DEBUG print("pos",pos); DEBUG print("depth",depth); RMQ R; R.init(depth); std::vector<Reac> C(I.C.begin(),I.C.end()); for(auto &r : C) r.l = R.query(pos[r.a],pos[r.b]); std::stable_sort(C.begin(),C.end()); DEBUG for(auto r : C) printf("%d %d (%d)\n",r.a,r.b,r.l); std::vector<int> G(I.G.begin(),I.G.end()); LL res = 0; for(auto r : C) if(r.l!=-1) { int s = std::min(G[r.a],G[r.b]); res += 2*s; G[r.a] -= s; G[r.b] -= s; } return res; } }; LL brute(const In &I) { int n = I.G.size(); LL res = 0; std::vector<int> G(I.G.begin(),I.G.end()); std::vector<std::set<int>> F(n); REP(i,n) F[i].insert(i); for(auto r : I.A) { for(auto c : I.C) if(F[r.a].count(c.a) && F[r.b].count(c.b) || F[r.a].count(c.b) && F[r.b].count(c.a)) { LL s = std::min(G[c.a],G[c.b]); res += 2*s; G[c.a] -= s; G[c.b] -= s; } F[r.b].insert(F[r.a].begin(),F[r.a].end()); } return res; } void test() { REP(_,5) { puts("max test"); In I; I.gen_max(); Solution S; S.run(I); } for(int c=0;; c++) { In I; I.gen(); Solution S; LL s = S.run(I); LL b = brute(I); if(s!=b) { printf("ERROR s=%lld; b=%lld\n",s,b); I.print(); exit(1); } if(!(c%1000)) printf("ok %d\n",c); } } int main() { //test(); In I; I.read(); Solution S; printf("%lld\n",S.run(I)); return 0; } |