#include <cstdio> #include <algorithm> using namespace std; #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) (c).begin(),(c).end() #define ZERO(x) memset(x,0,sizeof(x)) typedef long long LL; typedef unsigned long long ULL; const int INF = 1000000000; const int MAXN = 200000; const int MAXM = 200000; const int MAXK = 500000; int G[MAXN]; int A[MAXM], B[MAXM]; int C[MAXK], D[MAXK]; int P[MAXN]; bool S[MAXN]; bool V[MAXK]; void set(int x, int y) { while (P[x] != -1) x = P[x]; while (P[y] != -1) y = P[y]; P[y] = x; } int parent(int x) { if (P[x] != -1) { P[x] = parent(P[x]); return P[x]; } else return x; } int main(int argc, char **args) { int n, m, k; scanf("%d %d %d", &n, &m, &k); REP(i, n) scanf("%d", &G[i]); REP(i, m) scanf("%d %d", &A[i], &B[i]); REP(i, k) scanf("%d %d", &C[i], &D[i]); REP(i, n) { P[i] = -1; S[i] = false; } REP(i, k) V[i] = false; LL res = 0; REP(i, m) { int a = A[i] - 1; int b = B[i] - 1; S[a] = S[b] = true; parent(a); parent(b); set(a, b); REP(j, k) { if (!V[j] && S[C[j] - 1] && S[D[j] - 1]) { int c = C[j] - 1; int d = D[j] - 1; if (parent(c) == parent(d)) { int add = min(G[c], G[d]); G[c] -= add; G[d] -= add; V[j] = true; res += 2 * add; } } } } printf("%d\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 | #include <cstdio> #include <algorithm> using namespace std; #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) (c).begin(),(c).end() #define ZERO(x) memset(x,0,sizeof(x)) typedef long long LL; typedef unsigned long long ULL; const int INF = 1000000000; const int MAXN = 200000; const int MAXM = 200000; const int MAXK = 500000; int G[MAXN]; int A[MAXM], B[MAXM]; int C[MAXK], D[MAXK]; int P[MAXN]; bool S[MAXN]; bool V[MAXK]; void set(int x, int y) { while (P[x] != -1) x = P[x]; while (P[y] != -1) y = P[y]; P[y] = x; } int parent(int x) { if (P[x] != -1) { P[x] = parent(P[x]); return P[x]; } else return x; } int main(int argc, char **args) { int n, m, k; scanf("%d %d %d", &n, &m, &k); REP(i, n) scanf("%d", &G[i]); REP(i, m) scanf("%d %d", &A[i], &B[i]); REP(i, k) scanf("%d %d", &C[i], &D[i]); REP(i, n) { P[i] = -1; S[i] = false; } REP(i, k) V[i] = false; LL res = 0; REP(i, m) { int a = A[i] - 1; int b = B[i] - 1; S[a] = S[b] = true; parent(a); parent(b); set(a, b); REP(j, k) { if (!V[j] && S[C[j] - 1] && S[D[j] - 1]) { int c = C[j] - 1; int d = D[j] - 1; if (parent(c) == parent(d)) { int add = min(G[c], G[d]); G[c] -= add; G[d] -= add; V[j] = true; res += 2 * add; } } } } printf("%d\n", res); return 0; } |