#include <bits/stdc++.h> //#include <emmintrin.h> using namespace std; #define FORE(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,a) FOR(i,0,a) #define ZERO(m) memset(m,0,sizeof(m)) #define ALL(x) x.begin(),x.end() #define PB push_back #define S size() #define LL long long #define ULL unsigned long long #define LD long double #define MP make_pair #define X first #define Y second #define VC vector #define PII pair <int, int> #define VI VC < int > #define VPII VC < PII > #define VVI VC < VI > #define VD VC < double > #define VVD VC < VD > #define VS VC < string > #define DB(a) cerr << #a << ": " << (a) << endl; template<class T> void print(VC < T > v) {cerr << "[";if (v.S) cerr << v[0];FOR(i, 1, v.S) cerr << ", " << v[i];cerr << "]\n";} template<class T> string i2s(T x) {ostringstream o; o << x; return o.str(); } VS splt(string s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < s.S) all.PB(s.substr(p)); return all;} const int MAX_N = 201000; const int MAX_K = 501000; const int LOG_MAX_N = 20; LL RV = 0; int N, M, K; int G[MAX_N]; int TP[MAX_N]; int TL[MAX_N]; VI TC[MAX_N]; int TO[MAX_N]; VPII TR[MAX_N]; PII R[MAX_K]; int LCA[MAX_N][LOG_MAX_N]; void simulate(int p) { for (int x : TC[p]) simulate(x); if (p == 0) return; sort(ALL(TR[p])); for (PII r : TR[p]) { int x = r.Y; int a = min(G[R[x].X], G[R[x].Y]); RV += 2 * a; G[R[x].X] -= a; G[R[x].Y] -= a; } } void calcTL(int p, int l = 0) { TL[p] = l; for (int x : TC[p]) calcTL(x, l + 1); } void solve() { //compute TL calcTL(0); //LCA preprocess memset(LCA, -1, sizeof(LCA)); REP(i, N + 1) LCA[i][0] = TP[i]; FOR(j, 1, LOG_MAX_N) REP(i, N + 1) if (LCA[i][j-1] != -1) LCA[i][j] = LCA[LCA[i][j-1]][j-1]; //LCA REP(k, K) { int a = R[k].X; int b = R[k].Y; if (TL[a] < TL[b]) swap(a, b); if (TL[a] == TL[b]) b = TP[b]; for (int i = LOG_MAX_N - 1; i >= 0; i--) if (TL[a] - (1 << i) > TL[b]) a = LCA[a][i]; if (TP[a] != b) { a = TP[a]; for (int i = LOG_MAX_N - 1; i >= 0; i--) if (LCA[a][i] != LCA[b][i]) { a = LCA[a][i]; b = LCA[b][i]; } TR[TP[a]].PB(MP(max(TO[a], TO[b]), k)); } else { TR[TP[a]].PB(MP(TO[a], k)); } } //simulate simulate(0); } void add(int a, int b) { TP[a] = b; TC[b].PB(a); TO[a] = TC[b].S; } int main() { scanf("%d%d%d", &N, &M, &K); REP(i, N) scanf("%d",&G[i+1]); memset(TP, -1, sizeof(TP)); REP(i, M) { int a, b; scanf("%d%d", &a, &b); add(a, b); } FOR(i, 1, N+1) if (TP[i] == -1) add(i, 0); REP(i, K) scanf("%d%d", &R[i].X, &R[i].Y); solve(); cout << RV << endl; 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 | #include <bits/stdc++.h> //#include <emmintrin.h> using namespace std; #define FORE(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,a) FOR(i,0,a) #define ZERO(m) memset(m,0,sizeof(m)) #define ALL(x) x.begin(),x.end() #define PB push_back #define S size() #define LL long long #define ULL unsigned long long #define LD long double #define MP make_pair #define X first #define Y second #define VC vector #define PII pair <int, int> #define VI VC < int > #define VPII VC < PII > #define VVI VC < VI > #define VD VC < double > #define VVD VC < VD > #define VS VC < string > #define DB(a) cerr << #a << ": " << (a) << endl; template<class T> void print(VC < T > v) {cerr << "[";if (v.S) cerr << v[0];FOR(i, 1, v.S) cerr << ", " << v[i];cerr << "]\n";} template<class T> string i2s(T x) {ostringstream o; o << x; return o.str(); } VS splt(string s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < s.S) all.PB(s.substr(p)); return all;} const int MAX_N = 201000; const int MAX_K = 501000; const int LOG_MAX_N = 20; LL RV = 0; int N, M, K; int G[MAX_N]; int TP[MAX_N]; int TL[MAX_N]; VI TC[MAX_N]; int TO[MAX_N]; VPII TR[MAX_N]; PII R[MAX_K]; int LCA[MAX_N][LOG_MAX_N]; void simulate(int p) { for (int x : TC[p]) simulate(x); if (p == 0) return; sort(ALL(TR[p])); for (PII r : TR[p]) { int x = r.Y; int a = min(G[R[x].X], G[R[x].Y]); RV += 2 * a; G[R[x].X] -= a; G[R[x].Y] -= a; } } void calcTL(int p, int l = 0) { TL[p] = l; for (int x : TC[p]) calcTL(x, l + 1); } void solve() { //compute TL calcTL(0); //LCA preprocess memset(LCA, -1, sizeof(LCA)); REP(i, N + 1) LCA[i][0] = TP[i]; FOR(j, 1, LOG_MAX_N) REP(i, N + 1) if (LCA[i][j-1] != -1) LCA[i][j] = LCA[LCA[i][j-1]][j-1]; //LCA REP(k, K) { int a = R[k].X; int b = R[k].Y; if (TL[a] < TL[b]) swap(a, b); if (TL[a] == TL[b]) b = TP[b]; for (int i = LOG_MAX_N - 1; i >= 0; i--) if (TL[a] - (1 << i) > TL[b]) a = LCA[a][i]; if (TP[a] != b) { a = TP[a]; for (int i = LOG_MAX_N - 1; i >= 0; i--) if (LCA[a][i] != LCA[b][i]) { a = LCA[a][i]; b = LCA[b][i]; } TR[TP[a]].PB(MP(max(TO[a], TO[b]), k)); } else { TR[TP[a]].PB(MP(TO[a], k)); } } //simulate simulate(0); } void add(int a, int b) { TP[a] = b; TC[b].PB(a); TO[a] = TC[b].S; } int main() { scanf("%d%d%d", &N, &M, &K); REP(i, N) scanf("%d",&G[i+1]); memset(TP, -1, sizeof(TP)); REP(i, M) { int a, b; scanf("%d%d", &a, &b); add(a, b); } FOR(i, 1, N+1) if (TP[i] == -1) add(i, 0); REP(i, K) scanf("%d%d", &R[i].X, &R[i].Y); solve(); cout << RV << endl; return 0; } |