#include <unordered_set> #include <unordered_map> #include <functional> #include <algorithm> //#include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <cstdio> #include <vector> #include <queue> //#include <cmath> #include <set> #include <map> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b);i>=(e);--i) #define PB push_back #define ALL(V) (V).begin(),(V).end() #define SIZE(V) ((int)(V).size()) #define MP make_pair #define ST first #define ND second #define DBG #ifdef DBG #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif int __stmp; #define scanf __stmp=scanf const int INF = 1000000001; const int MAXN = 200010; const int MAXK = 500010; VI G[MAXN], F[MAXN]; int cap[MAXN], cc[MAXN]; PII moves[MAXN], reaction[MAXK]; int n, m, k; int main(int argc, char *argv[]) { scanf("%d %d %d", &n, &m, &k); REP(i,n) { scanf("%d", cap+i); F[i].PB(i); cc[i] = i; } REP(i,m) { scanf("%d %d", &moves[i].ST, &moves[i].ND); moves[i].ST--, moves[i].ND--; } REP(i,k) { int a, b; scanf("%d %d", &a, &b); a--, b--; reaction[i] = PII(a, b); G[a].PB(i); G[b].PB(i); } LL res = 0; VI C; REP(i,m) { int from = moves[i].ST, to = moves[i].ND; if(SIZE(F[from]) > SIZE(F[to])) { F[from].swap(F[to]); } C.clear(); for(int st : F[from]) { for(int j=0;j<SIZE(G[st]);) { int r = G[st][j]; int nd = reaction[r].ST + reaction[r].ND - st; if(cc[nd] == cc[F[to][0]]) { C.PB(r); G[st][j] = G[st].back(); G[st].pop_back(); } else j++; } } sort(ALL(C)); for(int r : C) { int st = reaction[r].ST, nd = reaction[r].ND; int t = min(cap[st], cap[nd]); cap[st] -= t; cap[nd] -= t; res += 2LL * t; } for(int a : F[from]) cc[a] = cc[F[to][0]]; F[to].insert(F[to].end(), ALL(F[from])); { VI tmp; tmp.swap(F[from]); } } 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 | #include <unordered_set> #include <unordered_map> #include <functional> #include <algorithm> //#include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <cstdio> #include <vector> #include <queue> //#include <cmath> #include <set> #include <map> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b);i>=(e);--i) #define PB push_back #define ALL(V) (V).begin(),(V).end() #define SIZE(V) ((int)(V).size()) #define MP make_pair #define ST first #define ND second #define DBG #ifdef DBG #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif int __stmp; #define scanf __stmp=scanf const int INF = 1000000001; const int MAXN = 200010; const int MAXK = 500010; VI G[MAXN], F[MAXN]; int cap[MAXN], cc[MAXN]; PII moves[MAXN], reaction[MAXK]; int n, m, k; int main(int argc, char *argv[]) { scanf("%d %d %d", &n, &m, &k); REP(i,n) { scanf("%d", cap+i); F[i].PB(i); cc[i] = i; } REP(i,m) { scanf("%d %d", &moves[i].ST, &moves[i].ND); moves[i].ST--, moves[i].ND--; } REP(i,k) { int a, b; scanf("%d %d", &a, &b); a--, b--; reaction[i] = PII(a, b); G[a].PB(i); G[b].PB(i); } LL res = 0; VI C; REP(i,m) { int from = moves[i].ST, to = moves[i].ND; if(SIZE(F[from]) > SIZE(F[to])) { F[from].swap(F[to]); } C.clear(); for(int st : F[from]) { for(int j=0;j<SIZE(G[st]);) { int r = G[st][j]; int nd = reaction[r].ST + reaction[r].ND - st; if(cc[nd] == cc[F[to][0]]) { C.PB(r); G[st][j] = G[st].back(); G[st].pop_back(); } else j++; } } sort(ALL(C)); for(int r : C) { int st = reaction[r].ST, nd = reaction[r].ND; int t = min(cap[st], cap[nd]); cap[st] -= t; cap[nd] -= t; res += 2LL * t; } for(int a : F[from]) cc[a] = cc[F[to][0]]; F[to].insert(F[to].end(), ALL(F[from])); { VI tmp; tmp.swap(F[from]); } } printf("%lld\n", res); return 0; } |