#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define DEBUG(x) #define DEBUG2(x) #define DEBUG3(x) #define MAXF 400011 #define MAXR 500011 #define BLACK 1 struct OpFio { //Find-Union int parent; int rank; //Tree int ancestor; int color; int l,p; //Fio int waga; int cojest; }; struct Reag { int a,b; int ancestor; }; struct ReagZ { int v; int reag; }; bool cmp(const Reag& a,const Reag& b) { return a.ancestor < b.ancestor; } OpFio opFio[MAXF]; Reag reag[MAXR]; vector<ReagZ> reagF[MAXF]; int n,m,k,op_ile, end_ile; void printT(int r, int ile = 0) { if(r<=0) return; for(int i=0;i<ile;i++) cout << " "; cout << "r: " << r << " (p:" << opFio[r].parent << " r:" << opFio[r].rank << ")" " [a:" << opFio[r].ancestor << " c:" << opFio[r].color <<"]" << " {w:" << opFio[r].waga << " j:" << opFio[r].cojest << "}\n"; printT(opFio[r].l,ile+3); printT(opFio[r].p,ile+3); } void printR(){ for(int i=0;i<k;i++) { cout << "reag: " << i << " a: " << reag[i].a << " b: " << reag[i].b << " w: " <<reag[i].ancestor <<"\n"; } } void MakeSet(int x) { opFio[x].parent = x; opFio[x].rank = 0; } int Find(int x) { if(opFio[x].parent == x) return x; opFio[x].parent = Find(opFio[x].parent); return opFio[x].parent; } void Union(int x, int y) { int xRoot = Find(x); int yRoot = Find(y); if(opFio[xRoot].rank > opFio[yRoot].rank) { opFio[yRoot].parent = xRoot; } else if(opFio[xRoot].rank < opFio[yRoot].rank) { opFio[xRoot].parent = yRoot; } else if(xRoot != yRoot) { opFio[yRoot].parent = xRoot; opFio[xRoot].rank = opFio[xRoot].rank + 1; } } void TarjanOLCA(int u) { MakeSet(u); opFio[u].ancestor = u; int v = opFio[u].l; if(v>0) { TarjanOLCA(v); Union(u,v); opFio[Find(u)].ancestor = u; } v = opFio[u].p; if(v>0) { TarjanOLCA(v); Union(u,v); opFio[Find(u)].ancestor = u; } opFio[u].color = BLACK; for(vector<ReagZ>::iterator it = reagF[u].begin();it!=reagF[u].end();it++) { if(opFio[it->v].color == BLACK) { reag[it->reag].ancestor = opFio[Find(it->v)].ancestor; } } } int main() { scanf("%d %d %d",&n,&m,&k); for(op_ile=1;op_ile<=n;op_ile++) { scanf("%d ", &opFio[op_ile].waga); opFio[op_ile].cojest=op_ile; } for(int i=0;i<m;i++) { int a,b; scanf("%d %d", &a,&b); opFio[op_ile].l = opFio[a].cojest; opFio[op_ile].p = opFio[b].cojest; opFio[b].cojest = op_ile; opFio[a].cojest = -1; op_ile++; } end_ile = op_ile; int b,a = 0; for(int i=1;i<=n;i++) { if(opFio[i].cojest > 0) { if(a <= 0) { a = i; } else { b=i; opFio[op_ile].l = opFio[a].cojest; opFio[op_ile].p = opFio[b].cojest; opFio[b].cojest = op_ile; opFio[a].cojest = -1; op_ile++; a = b; } } } for(int i=0;i<k;i++) { int a,b; scanf("%d %d", &a,&b); reag[i].a = a; reag[i].b = b; ReagZ az,bz; az.v = b; bz.v = a; az.reag = bz.reag = i; reagF[a].push_back(az); reagF[b].push_back(bz); } TarjanOLCA(op_ile-1); DEBUG(printT(op_ile-1)); DEBUG(printR()); stable_sort(&reag[0], &reag[k], cmp); DEBUG2(cout << "sorted:\n"); DEBUG2(printR()); long long sum = 0; for(int i=0;i<k;i++) { if(0< reag[i].ancestor && reag[i].ancestor < end_ile) { int a,b,abmin; a = reag[i].a; b = reag[i].b; abmin = min(opFio[a].waga,opFio[b].waga); opFio[a].waga -= abmin; opFio[b].waga -= abmin; sum+=2*abmin; } } cout << sum << "\n"; 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 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define DEBUG(x) #define DEBUG2(x) #define DEBUG3(x) #define MAXF 400011 #define MAXR 500011 #define BLACK 1 struct OpFio { //Find-Union int parent; int rank; //Tree int ancestor; int color; int l,p; //Fio int waga; int cojest; }; struct Reag { int a,b; int ancestor; }; struct ReagZ { int v; int reag; }; bool cmp(const Reag& a,const Reag& b) { return a.ancestor < b.ancestor; } OpFio opFio[MAXF]; Reag reag[MAXR]; vector<ReagZ> reagF[MAXF]; int n,m,k,op_ile, end_ile; void printT(int r, int ile = 0) { if(r<=0) return; for(int i=0;i<ile;i++) cout << " "; cout << "r: " << r << " (p:" << opFio[r].parent << " r:" << opFio[r].rank << ")" " [a:" << opFio[r].ancestor << " c:" << opFio[r].color <<"]" << " {w:" << opFio[r].waga << " j:" << opFio[r].cojest << "}\n"; printT(opFio[r].l,ile+3); printT(opFio[r].p,ile+3); } void printR(){ for(int i=0;i<k;i++) { cout << "reag: " << i << " a: " << reag[i].a << " b: " << reag[i].b << " w: " <<reag[i].ancestor <<"\n"; } } void MakeSet(int x) { opFio[x].parent = x; opFio[x].rank = 0; } int Find(int x) { if(opFio[x].parent == x) return x; opFio[x].parent = Find(opFio[x].parent); return opFio[x].parent; } void Union(int x, int y) { int xRoot = Find(x); int yRoot = Find(y); if(opFio[xRoot].rank > opFio[yRoot].rank) { opFio[yRoot].parent = xRoot; } else if(opFio[xRoot].rank < opFio[yRoot].rank) { opFio[xRoot].parent = yRoot; } else if(xRoot != yRoot) { opFio[yRoot].parent = xRoot; opFio[xRoot].rank = opFio[xRoot].rank + 1; } } void TarjanOLCA(int u) { MakeSet(u); opFio[u].ancestor = u; int v = opFio[u].l; if(v>0) { TarjanOLCA(v); Union(u,v); opFio[Find(u)].ancestor = u; } v = opFio[u].p; if(v>0) { TarjanOLCA(v); Union(u,v); opFio[Find(u)].ancestor = u; } opFio[u].color = BLACK; for(vector<ReagZ>::iterator it = reagF[u].begin();it!=reagF[u].end();it++) { if(opFio[it->v].color == BLACK) { reag[it->reag].ancestor = opFio[Find(it->v)].ancestor; } } } int main() { scanf("%d %d %d",&n,&m,&k); for(op_ile=1;op_ile<=n;op_ile++) { scanf("%d ", &opFio[op_ile].waga); opFio[op_ile].cojest=op_ile; } for(int i=0;i<m;i++) { int a,b; scanf("%d %d", &a,&b); opFio[op_ile].l = opFio[a].cojest; opFio[op_ile].p = opFio[b].cojest; opFio[b].cojest = op_ile; opFio[a].cojest = -1; op_ile++; } end_ile = op_ile; int b,a = 0; for(int i=1;i<=n;i++) { if(opFio[i].cojest > 0) { if(a <= 0) { a = i; } else { b=i; opFio[op_ile].l = opFio[a].cojest; opFio[op_ile].p = opFio[b].cojest; opFio[b].cojest = op_ile; opFio[a].cojest = -1; op_ile++; a = b; } } } for(int i=0;i<k;i++) { int a,b; scanf("%d %d", &a,&b); reag[i].a = a; reag[i].b = b; ReagZ az,bz; az.v = b; bz.v = a; az.reag = bz.reag = i; reagF[a].push_back(az); reagF[b].push_back(bz); } TarjanOLCA(op_ile-1); DEBUG(printT(op_ile-1)); DEBUG(printR()); stable_sort(&reag[0], &reag[k], cmp); DEBUG2(cout << "sorted:\n"); DEBUG2(printR()); long long sum = 0; for(int i=0;i<k;i++) { if(0< reag[i].ancestor && reag[i].ancestor < end_ile) { int a,b,abmin; a = reag[i].a; b = reag[i].b; abmin = min(opFio[a].waga,opFio[b].waga); opFio[a].waga -= abmin; opFio[b].waga -= abmin; sum+=2*abmin; } } cout << sum << "\n"; return 0; } |