/* * LCA adapted from: http://www.topcoder.com/tc?d1=tutorials&d2=lowestCommonAncestor&module=Static */ #include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; const int MAXN = 200005; const int MAXT = 2 * MAXN; const int MAXLOGT = 19; const int MAXK = 500005; int n, m, k; LL result; int w[MAXN]; PII r[MAXK]; VII sorted[MAXN]; struct Node { Node() : left(0), right(0), parent(0) {} int left, right, parent; }; Node tree[MAXT]; int T; int pos[MAXN]; int depth[MAXT]; int dp[MAXT][MAXLOGT]; void fillTree() { T = 2 * n - 1; if (m == n - 1) return; int idx = 1; while (idx <= n+m && tree[idx].parent != 0) { ++idx; } tree[idx].parent = n+m+1; tree[n+m+1].left = idx; int free = n+m+1; while (free < 2 * n) { if (tree[idx].parent == 0) { tree[idx].parent = free; tree[free].right = idx; if (free < T) { tree[free+1].left = free; tree[free].parent = free+1; } ++free; } ++idx; } } void DFS(int v, int d) { depth[v] = d; if (tree[v].left != 0) DFS(tree[v].left, d+1); if (tree[v].right != 0) DFS(tree[v].right, d+1); } void initLCA() { FOR (i, 1, T) { for (int j = 0; (1 << j) <= T; ++j) { dp[i][j] = -1; } } FOR (i, 1, T) { dp[i][0] = tree[i].parent; } for (int j = 1; (1 << j) <= T; j++) { FOR (i, 1, T) { if (dp[i][j-1] != -1) { dp[i][j] = dp[dp[i][j-1]][j-1]; } } } } int LCA(int p, int q) { int log; if (depth[p] < depth[q]) { swap(p, q); } for (log = 1; (1 << log) <= depth[p]; ++log); --log; FORD (i, log, 0) { if (depth[p] - (1 << i) >= depth[q]) { p = dp[p][i]; } } if (p == q) { return p; } FORD (i, log, 0) { if (dp[p][i] != -1 && dp[p][i] != dp[q][i]) { p = dp[p][i]; q = dp[q][i]; } } return tree[p].parent; } void findTimes() { REP (i, k) { int time = LCA(r[i].ST, r[i].ND); if (time <= n + m) { sorted[time-n-1].PB(r[i]); } } } void simulate() { int change; REP (i, m) { for (auto re : sorted[i]) { change = min(w[re.ST], w[re.ND]); result += 2 * change; w[re.ST] -= change; w[re.ND] -= change; } } } int main() { scanf("%d %d %d", &n, &m, &k); REP (i, n) { scanf("%d", &w[i+1]); pos[i+1] = i+1; } int from, to; REP (i, m) { scanf("%d %d", &from, &to); tree[n+i+1].left = pos[from]; tree[n+i+1].right = pos[to]; tree[pos[from]].parent = n+i+1; tree[pos[to]].parent = n+i+1; pos[to] = n+i+1; } REP (i, k) { scanf("%d %d", &r[i].ST, &r[i].ND); if (r[i].ST > r[i].ND) swap(r[i].ST, r[i].ND); } fillTree(); DFS(2*n-1, 0); initLCA(); findTimes(); simulate(); printf("%lld\n", result); 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | /* * LCA adapted from: http://www.topcoder.com/tc?d1=tutorials&d2=lowestCommonAncestor&module=Static */ #include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; const int MAXN = 200005; const int MAXT = 2 * MAXN; const int MAXLOGT = 19; const int MAXK = 500005; int n, m, k; LL result; int w[MAXN]; PII r[MAXK]; VII sorted[MAXN]; struct Node { Node() : left(0), right(0), parent(0) {} int left, right, parent; }; Node tree[MAXT]; int T; int pos[MAXN]; int depth[MAXT]; int dp[MAXT][MAXLOGT]; void fillTree() { T = 2 * n - 1; if (m == n - 1) return; int idx = 1; while (idx <= n+m && tree[idx].parent != 0) { ++idx; } tree[idx].parent = n+m+1; tree[n+m+1].left = idx; int free = n+m+1; while (free < 2 * n) { if (tree[idx].parent == 0) { tree[idx].parent = free; tree[free].right = idx; if (free < T) { tree[free+1].left = free; tree[free].parent = free+1; } ++free; } ++idx; } } void DFS(int v, int d) { depth[v] = d; if (tree[v].left != 0) DFS(tree[v].left, d+1); if (tree[v].right != 0) DFS(tree[v].right, d+1); } void initLCA() { FOR (i, 1, T) { for (int j = 0; (1 << j) <= T; ++j) { dp[i][j] = -1; } } FOR (i, 1, T) { dp[i][0] = tree[i].parent; } for (int j = 1; (1 << j) <= T; j++) { FOR (i, 1, T) { if (dp[i][j-1] != -1) { dp[i][j] = dp[dp[i][j-1]][j-1]; } } } } int LCA(int p, int q) { int log; if (depth[p] < depth[q]) { swap(p, q); } for (log = 1; (1 << log) <= depth[p]; ++log); --log; FORD (i, log, 0) { if (depth[p] - (1 << i) >= depth[q]) { p = dp[p][i]; } } if (p == q) { return p; } FORD (i, log, 0) { if (dp[p][i] != -1 && dp[p][i] != dp[q][i]) { p = dp[p][i]; q = dp[q][i]; } } return tree[p].parent; } void findTimes() { REP (i, k) { int time = LCA(r[i].ST, r[i].ND); if (time <= n + m) { sorted[time-n-1].PB(r[i]); } } } void simulate() { int change; REP (i, m) { for (auto re : sorted[i]) { change = min(w[re.ST], w[re.ND]); result += 2 * change; w[re.ST] -= change; w[re.ND] -= change; } } } int main() { scanf("%d %d %d", &n, &m, &k); REP (i, n) { scanf("%d", &w[i+1]); pos[i+1] = i+1; } int from, to; REP (i, m) { scanf("%d %d", &from, &to); tree[n+i+1].left = pos[from]; tree[n+i+1].right = pos[to]; tree[pos[from]].parent = n+i+1; tree[pos[to]].parent = n+i+1; pos[to] = n+i+1; } REP (i, k) { scanf("%d %d", &r[i].ST, &r[i].ND); if (r[i].ST > r[i].ND) swap(r[i].ST, r[i].ND); } fillTree(); DFS(2*n-1, 0); initLCA(); findTimes(); simulate(); printf("%lld\n", result); return 0; } |