#include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <cstdio> #include <set> #include <cstring> #include <list> #include <iostream> #include <queue> //#include <cassert> #define assert(exp) do { } while(0); using namespace std; #define FOR(i,n) for(int i = 0; i < n; i++) #define REP(i,n) for(int i = 1; i <= n; i++) #define FORI(it,n)for(typeof(n.begin()) it = n.begin(); it != n.end(); it++) #define frs first #define sec second #define psh push_back #define mkp make_pair typedef long long LL; typedef long double LD; const int INF = 2147483647; const int MAX = 200100; const int MAX_K = 500100; const int LOG = 20; int n, m, k; bool V[MAX]; int F[MAX], C[MAX], G[MAX], R[MAX], LG[MAX]; vector<int> E[MAX]; vector<pair<int,int>> RV[MAX]; int P[LOG][MAX]; queue<int> L; LL sumg; int par(int u, int f) { int c = 0; while(f) { if(f & 1) u = P[c][u]; c ++; f >>= 1; } return u; } void com_par(int u, int v, int &pu, int &pv) { if(F[u] < F[v]) swap(u,v); if(F[u] != F[v]) { u = par(u, F[u] - F[v] - 1); if(P[0][u] == v) { pu = u; pv = v; return; } u = P[0][u]; } for(int i = LOG - 1; i >= 0; i--) { if(P[i][u] != P[i][v]) { u = P[i][u]; v = P[i][v]; } } pu = u; pv = v; } void set_f(int u) { F[u] = F[P[0][u]] + 1; for(auto v: E[u]) set_f(v); } int main() { int u, v, pu, pv, pr, g; scanf("%d%d%d", &n, &m, &k); REP(i,n) { scanf("%d", G + i); P[0][i] = i; sumg += (LL)G[i]; assert(G[i] <= 1000000000); } while(m--) { scanf("%d%d", &u, &v); assert(V[v] == false); assert(V[u] == false); assert(u <= n); assert(v <= n); V[u] = true; E[v].psh(u); P[0][u] = v; C[v] ++; R[u] = E[v].size(); } REP(i,n) if(P[0][i] == i) set_f(i); // REP(i,n) fprintf(stderr, "F[%d] = %d\n", i, F[i]); REP(l, LOG - 1) { REP(i,n) { P[l][i] = P[ l - 1 ][ P[l - 1][i] ]; } } // for(int i = 1, c = 0; i <= n && c < LOG; c++) { // for(int j = 1 << c; j > 0; j--, i++) LG[i] = c; // } // REP(i,5) fprintf(stderr, "LG[%d] = %d\n", i, LG[i]); while(k--) { scanf("%d%d", &u, &v); if(F[u] < F[v]) swap(u,v); if(P[LOG - 1][u] != P[LOG - 1][v]) continue; com_par(u,v, pu, pv); if(P[0][pu] == v) { pr = pu; assert(P[0][pu] == pv); } else { pr = R[pu] > R[pv] ? pu : pv; assert(P[0][pu] == P[0][pv]); } RV[pr].psh(mkp(u,v)); } // REP(i,n) fprintf(stderr, "P[%d] = %d %d %d %d\n", i, P[0][i], P[1][i], P[2][i], P[3][i]); REP(i,n) if(C[i] == 0) L.push(i); while(L.size()) { u = L.front(); L.pop(); for(auto vv: E[u]) { for(auto r: RV[vv]) { g = min(G[r.frs], G[r.sec]); G[r.frs] -= g; G[r.sec] -= g; } } C[P[0][u]] --; if(C[P[0][u]] == 0) L.push(P[0][u]); } REP(i,n) sumg -= (LL)G[i]; printf("%lld\n", sumg); }
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 | #include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <cstdio> #include <set> #include <cstring> #include <list> #include <iostream> #include <queue> //#include <cassert> #define assert(exp) do { } while(0); using namespace std; #define FOR(i,n) for(int i = 0; i < n; i++) #define REP(i,n) for(int i = 1; i <= n; i++) #define FORI(it,n)for(typeof(n.begin()) it = n.begin(); it != n.end(); it++) #define frs first #define sec second #define psh push_back #define mkp make_pair typedef long long LL; typedef long double LD; const int INF = 2147483647; const int MAX = 200100; const int MAX_K = 500100; const int LOG = 20; int n, m, k; bool V[MAX]; int F[MAX], C[MAX], G[MAX], R[MAX], LG[MAX]; vector<int> E[MAX]; vector<pair<int,int>> RV[MAX]; int P[LOG][MAX]; queue<int> L; LL sumg; int par(int u, int f) { int c = 0; while(f) { if(f & 1) u = P[c][u]; c ++; f >>= 1; } return u; } void com_par(int u, int v, int &pu, int &pv) { if(F[u] < F[v]) swap(u,v); if(F[u] != F[v]) { u = par(u, F[u] - F[v] - 1); if(P[0][u] == v) { pu = u; pv = v; return; } u = P[0][u]; } for(int i = LOG - 1; i >= 0; i--) { if(P[i][u] != P[i][v]) { u = P[i][u]; v = P[i][v]; } } pu = u; pv = v; } void set_f(int u) { F[u] = F[P[0][u]] + 1; for(auto v: E[u]) set_f(v); } int main() { int u, v, pu, pv, pr, g; scanf("%d%d%d", &n, &m, &k); REP(i,n) { scanf("%d", G + i); P[0][i] = i; sumg += (LL)G[i]; assert(G[i] <= 1000000000); } while(m--) { scanf("%d%d", &u, &v); assert(V[v] == false); assert(V[u] == false); assert(u <= n); assert(v <= n); V[u] = true; E[v].psh(u); P[0][u] = v; C[v] ++; R[u] = E[v].size(); } REP(i,n) if(P[0][i] == i) set_f(i); // REP(i,n) fprintf(stderr, "F[%d] = %d\n", i, F[i]); REP(l, LOG - 1) { REP(i,n) { P[l][i] = P[ l - 1 ][ P[l - 1][i] ]; } } // for(int i = 1, c = 0; i <= n && c < LOG; c++) { // for(int j = 1 << c; j > 0; j--, i++) LG[i] = c; // } // REP(i,5) fprintf(stderr, "LG[%d] = %d\n", i, LG[i]); while(k--) { scanf("%d%d", &u, &v); if(F[u] < F[v]) swap(u,v); if(P[LOG - 1][u] != P[LOG - 1][v]) continue; com_par(u,v, pu, pv); if(P[0][pu] == v) { pr = pu; assert(P[0][pu] == pv); } else { pr = R[pu] > R[pv] ? pu : pv; assert(P[0][pu] == P[0][pv]); } RV[pr].psh(mkp(u,v)); } // REP(i,n) fprintf(stderr, "P[%d] = %d %d %d %d\n", i, P[0][i], P[1][i], P[2][i], P[3][i]); REP(i,n) if(C[i] == 0) L.push(i); while(L.size()) { u = L.front(); L.pop(); for(auto vv: E[u]) { for(auto r: RV[vv]) { g = min(G[r.frs], G[r.sec]); G[r.frs] -= g; G[r.sec] -= g; } } C[P[0][u]] --; if(C[P[0][u]] == 0) L.push(P[0][u]); } REP(i,n) sumg -= (LL)G[i]; printf("%lld\n", sumg); } |