#include <cassert> #include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 210000; const int M = 510000; int ile_jest[N]; //VPII kraw[N]; int nazwa[N]; int gdzie_jest[N]; PII kraw[2*M]; int start_edge[N]; int krawedzie[M][2]; int deg[N]; VI podzial[N]; vector<pair<int,PII> > pary; int main(){ int n,m,k; scanf("%d %d %d",&n,&m,&k); REP(i,n) scanf("%d", ile_jest+i); VPII przelew; przelew.reserve(m); REP(i,m) { int a,b; scanf("%d %d",&a,&b); przelew.PB(MP(a-1,b-1)); } REP(i,k) { int a,b; scanf("%d %d",&a,&b); a--; b--; krawedzie[i][0] = a; krawedzie[i][1] = b; start_edge[a]++; start_edge[b]++; //kraw[a].PB(MP(b,i)); //kraw[b].PB(MP(a,i)); } int sum = start_edge[0]; start_edge[0] = 0; FOR(i,1,n) { int nsum = sum + start_edge[i]; start_edge[i] = sum; sum = nsum; } REP(i,k) { int a = krawedzie[i][0]; int b = krawedzie[i][1]; kraw[start_edge[a]+(deg[a]++)] = MP(b,i); kraw[start_edge[b]+(deg[b]++)] = MP(a,i); } REP(i,n) nazwa[i] = i; REP(i,n) gdzie_jest[i] = i; REP(i,n) podzial[i].PB(i); LL res = 0; pary.reserve(m); FOREACH(itt, przelew) { int a = nazwa[itt->ST]; int b = nazwa[itt->ND]; int swapped = 0; if (SIZE(podzial[a]) > SIZE(podzial[b])){ swap(a,b); swapped = 1; } FOREACH(it2, podzial[a]) { for (int i = start_edge[*it2]; i < start_edge[*it2+1]; ++i) { int x = kraw[i].ST; int y = kraw[i].ND; if (gdzie_jest[x] == b) { pary.PB(MP(y, MP(*it2, x))); } } } sort(ALL(pary)); FOREACH(it, pary) { int x = min(ile_jest[it->ND.ST],ile_jest[it->ND.ND]); res += 2 * x; ile_jest[it->ND.ST] -= x; ile_jest[it->ND.ND] -= x; } pary.clear(); podzial[b].reserve(SIZE(podzial[a])+SIZE(podzial[b])); FOREACH(it, podzial[a]) if (ile_jest[*it]) { podzial[b].PB(*it); gdzie_jest[*it] = b; } else gdzie_jest[*it] = -1; if (swapped) nazwa[itt->ND] = b; } 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 | #include <cassert> #include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 210000; const int M = 510000; int ile_jest[N]; //VPII kraw[N]; int nazwa[N]; int gdzie_jest[N]; PII kraw[2*M]; int start_edge[N]; int krawedzie[M][2]; int deg[N]; VI podzial[N]; vector<pair<int,PII> > pary; int main(){ int n,m,k; scanf("%d %d %d",&n,&m,&k); REP(i,n) scanf("%d", ile_jest+i); VPII przelew; przelew.reserve(m); REP(i,m) { int a,b; scanf("%d %d",&a,&b); przelew.PB(MP(a-1,b-1)); } REP(i,k) { int a,b; scanf("%d %d",&a,&b); a--; b--; krawedzie[i][0] = a; krawedzie[i][1] = b; start_edge[a]++; start_edge[b]++; //kraw[a].PB(MP(b,i)); //kraw[b].PB(MP(a,i)); } int sum = start_edge[0]; start_edge[0] = 0; FOR(i,1,n) { int nsum = sum + start_edge[i]; start_edge[i] = sum; sum = nsum; } REP(i,k) { int a = krawedzie[i][0]; int b = krawedzie[i][1]; kraw[start_edge[a]+(deg[a]++)] = MP(b,i); kraw[start_edge[b]+(deg[b]++)] = MP(a,i); } REP(i,n) nazwa[i] = i; REP(i,n) gdzie_jest[i] = i; REP(i,n) podzial[i].PB(i); LL res = 0; pary.reserve(m); FOREACH(itt, przelew) { int a = nazwa[itt->ST]; int b = nazwa[itt->ND]; int swapped = 0; if (SIZE(podzial[a]) > SIZE(podzial[b])){ swap(a,b); swapped = 1; } FOREACH(it2, podzial[a]) { for (int i = start_edge[*it2]; i < start_edge[*it2+1]; ++i) { int x = kraw[i].ST; int y = kraw[i].ND; if (gdzie_jest[x] == b) { pary.PB(MP(y, MP(*it2, x))); } } } sort(ALL(pary)); FOREACH(it, pary) { int x = min(ile_jest[it->ND.ST],ile_jest[it->ND.ND]); res += 2 * x; ile_jest[it->ND.ST] -= x; ile_jest[it->ND.ND] -= x; } pary.clear(); podzial[b].reserve(SIZE(podzial[a])+SIZE(podzial[b])); FOREACH(it, podzial[a]) if (ile_jest[*it]) { podzial[b].PB(*it); gdzie_jest[*it] = b; } else gdzie_jest[*it] = -1; if (swapped) nazwa[itt->ND] = b; } printf("%lld\n", res); return 0; } |