#include <algorithm> #include <iostream> #include <vector> using namespace std; const int nmax = 2*1e5+5; const int tmax = 2*nmax; const int kmax = 5*1e5+5; const int logtmax = 20; typedef long long ll; int n, m, k; ll g[nmax]; vector<int> t[tmax]; int tl[tmax]; int tn; struct { int c, d; int node; } mix[kmax]; vector<int> mixorder; bool mixcmp(const int lhs, const int rhs) { if (mix[lhs].node != mix[rhs].node) return mix[lhs].node < mix[rhs].node; //tl[mix[lhs].node] > tl[mix[rhs].node]; return lhs < rhs; } void debugTree() { for (int i = 1; i <= tn; i++) { cerr << i << ": "; for (auto w : t[i]) cerr << w << " "; cerr << "\n"; } } struct { int T[tmax]; int P[tmax][logtmax]; bool vis[tmax]; void dfs(int v, int f, int level) { // cerr << v << " " << f << endl; vis[v] = true; T[v] = f; tl[v] = level; for (auto w : t[v]) { if (w == f) continue; dfs(w, v, level+1); } } int query(int p, int q) { int tmp, log, i; if (tl[p] < tl[q]) tmp = p, p = q, q = tmp; for (log = 1; (1 << log) <= tl[p]; log++) {} log--; for (i = log; i >= 0; i--) if (tl[p] - (1 << i) >= tl[q]) p = P[p][i]; if (p == q) return p; for (i = log; i >= 0; i--) if (P[p][i] != -1 && P[p][i] != P[q][i]) p = P[p][i], q = P[q][i]; return T[p]; } void build() { for (int i = 0; i < tmax; i++) vis[i] = false; // cerr << "lca.build()\n"; // debugTree(); for (int i = tn; i > 0; i--) { if (!vis[i]) { t[0].push_back(i); t[i].push_back(0); dfs(i, 0, 1); } } // for (int i = 1; i <= tn; i++) { // cerr << T[i] << " "; // } cerr << "\n"; int i, j; for (i = 1; i <= tn; i++) { for (j = 0; (1 << j) <= tn; j++) P[i][j] = -1; } for (i = 1; i <= tn; i++) { P[i][0] = T[i]; } for (j = 1; (1 << j) <= tn; j++) for (i = 1; i <= tn; i++) if (P[i][j-1] != -1) P[i][j] = P[P[i][j-1]][j-1]; } } lca; void readTree() { int f[tmax]; for (int i = 1; i <= n; i++) f[i] = i; tn = n; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; tn++; t[tn].push_back(f[a]); t[tn].push_back(f[b]); t[f[b]].push_back(tn); t[f[a]].push_back(tn); f[b] = tn; f[a] = tn; } } int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> g[i]; readTree(); lca.build(); for (int i = 0; i < k; i++) { cin >> mix[i].c >> mix[i].d; mix[i].node = lca.query(mix[i].c, mix[i].d); // cerr << mix[i].node << " " << mix[i].c << " " << mix[i].d << "\n"; if (mix[i].node != 0) mixorder.push_back(i); // else { cerr << '.'; } } // cerr << '\n'; // cerr << mixorder.size() << '\n'; sort(mixorder.begin(), mixorder.end(), mixcmp); ll ans = 0; for (auto step : mixorder) { // cerr << step << ": " << mix[step].c << " " << mix[step].d << " _" << mix[step].node << endl; ll diff = min(g[mix[step].c], g[mix[step].d]); ans += 2L * diff; g[mix[step].c] -= diff; g[mix[step].d] -= diff; } cout << ans << 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <algorithm> #include <iostream> #include <vector> using namespace std; const int nmax = 2*1e5+5; const int tmax = 2*nmax; const int kmax = 5*1e5+5; const int logtmax = 20; typedef long long ll; int n, m, k; ll g[nmax]; vector<int> t[tmax]; int tl[tmax]; int tn; struct { int c, d; int node; } mix[kmax]; vector<int> mixorder; bool mixcmp(const int lhs, const int rhs) { if (mix[lhs].node != mix[rhs].node) return mix[lhs].node < mix[rhs].node; //tl[mix[lhs].node] > tl[mix[rhs].node]; return lhs < rhs; } void debugTree() { for (int i = 1; i <= tn; i++) { cerr << i << ": "; for (auto w : t[i]) cerr << w << " "; cerr << "\n"; } } struct { int T[tmax]; int P[tmax][logtmax]; bool vis[tmax]; void dfs(int v, int f, int level) { // cerr << v << " " << f << endl; vis[v] = true; T[v] = f; tl[v] = level; for (auto w : t[v]) { if (w == f) continue; dfs(w, v, level+1); } } int query(int p, int q) { int tmp, log, i; if (tl[p] < tl[q]) tmp = p, p = q, q = tmp; for (log = 1; (1 << log) <= tl[p]; log++) {} log--; for (i = log; i >= 0; i--) if (tl[p] - (1 << i) >= tl[q]) p = P[p][i]; if (p == q) return p; for (i = log; i >= 0; i--) if (P[p][i] != -1 && P[p][i] != P[q][i]) p = P[p][i], q = P[q][i]; return T[p]; } void build() { for (int i = 0; i < tmax; i++) vis[i] = false; // cerr << "lca.build()\n"; // debugTree(); for (int i = tn; i > 0; i--) { if (!vis[i]) { t[0].push_back(i); t[i].push_back(0); dfs(i, 0, 1); } } // for (int i = 1; i <= tn; i++) { // cerr << T[i] << " "; // } cerr << "\n"; int i, j; for (i = 1; i <= tn; i++) { for (j = 0; (1 << j) <= tn; j++) P[i][j] = -1; } for (i = 1; i <= tn; i++) { P[i][0] = T[i]; } for (j = 1; (1 << j) <= tn; j++) for (i = 1; i <= tn; i++) if (P[i][j-1] != -1) P[i][j] = P[P[i][j-1]][j-1]; } } lca; void readTree() { int f[tmax]; for (int i = 1; i <= n; i++) f[i] = i; tn = n; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; tn++; t[tn].push_back(f[a]); t[tn].push_back(f[b]); t[f[b]].push_back(tn); t[f[a]].push_back(tn); f[b] = tn; f[a] = tn; } } int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> g[i]; readTree(); lca.build(); for (int i = 0; i < k; i++) { cin >> mix[i].c >> mix[i].d; mix[i].node = lca.query(mix[i].c, mix[i].d); // cerr << mix[i].node << " " << mix[i].c << " " << mix[i].d << "\n"; if (mix[i].node != 0) mixorder.push_back(i); // else { cerr << '.'; } } // cerr << '\n'; // cerr << mixorder.size() << '\n'; sort(mixorder.begin(), mixorder.end(), mixcmp); ll ans = 0; for (auto step : mixorder) { // cerr << step << ": " << mix[step].c << " " << mix[step].d << " _" << mix[step].node << endl; ll diff = min(g[mix[step].c], g[mix[step].d]); ans += 2L * diff; g[mix[step].c] -= diff; g[mix[step].d] -= diff; } cout << ans << endl; return 0; } |