#include <cstdio> #include <vector> #include <cmath> #include <algorithm> #define PB push_back #define ST first #define ND second using namespace std; typedef pair<int, int> PII; typedef long long LL; const int MX_N = 200010; LL res; int n, m, k, size, korzen, ct, a, b; // long longi!!! int w[MX_N], akt[MX_N], p[20][2*MX_N], pre[2*MX_N], post[2*MX_N], r[2*MX_N]; bool vis[2*MX_N]; vector<int> v[2*MX_N]; vector<PII> vlca[2*MX_N]; void dfs(int x, int rep) { ct++; pre[x] = ct; vis[x] = true; r[x] = rep; for (int i = 0; i < (int)v[x].size(); ++i) dfs(v[x][i], rep); ct++; post[x] = ct; } bool potomek(int u, int v) { return ((pre[u] > pre[v]) && (post[u] < post[v])); } int sufit(double x) { if ((x - (int)x) == 0) return (int)x; else return (int)x + 1; } int lca(int u, int v) { if (potomek(u, v)) return v; else if (potomek(v, u)) return u; else { int i = u; int j = sufit(log(n) / log(2)); while (j >= 0) { if (potomek(v, p[j][i])) j--; else i = p[j][i]; } return p[0][i]; } } void dfs2(int x) { vis[x] = true; for (int i = 0; i < (int)v[x].size(); ++i) dfs2(v[x][i]); for (int i = 0; i < (int)vlca[x].size(); ++i) { LL wag = min(w[vlca[x][i].ST], w[vlca[x][i].ND]); w[vlca[x][i].ST] -= wag; w[vlca[x][i].ND] -= wag; res += 2*wag; } } int main () { scanf ("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) // Waga { scanf ("%d", &w[i]); akt[i] = i; } size = n; for (int i = 0; i < m; ++i) // Przelania { scanf ("%d%d", &a, &b); size++; p[0][akt[a]] = p[0][akt[b]] = size; v[size].PB(akt[a]); v[size].PB(akt[b]); akt[b] = size; } korzen = size; for (int i = korzen; i >= 1; --i) { if (!vis[i]) { p[0][i] = i; dfs(i, i); } } int LOGN = sufit(log(n) / log(2)); for(int i = 1; i <= LOGN; ++i) // p[i][j] = wierzcholek u taki, ze d(u, j) = 2^i for(int j = korzen; j >= 1; --j) p[i][j] = p[i-1][p[i-1][j]]; for (int i = 0; i < k; ++i) { scanf ("%d%d", &a, &b); if (r[a] == r[b]) vlca[lca(a, b)].PB(PII(a, b)); } for (int i = 0; i <= korzen; ++i) vis[i] = false; for (int i = korzen; i >= 1; --i) { if (!vis[i]) dfs2(i); } printf ("%lld", res); }
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 | #include <cstdio> #include <vector> #include <cmath> #include <algorithm> #define PB push_back #define ST first #define ND second using namespace std; typedef pair<int, int> PII; typedef long long LL; const int MX_N = 200010; LL res; int n, m, k, size, korzen, ct, a, b; // long longi!!! int w[MX_N], akt[MX_N], p[20][2*MX_N], pre[2*MX_N], post[2*MX_N], r[2*MX_N]; bool vis[2*MX_N]; vector<int> v[2*MX_N]; vector<PII> vlca[2*MX_N]; void dfs(int x, int rep) { ct++; pre[x] = ct; vis[x] = true; r[x] = rep; for (int i = 0; i < (int)v[x].size(); ++i) dfs(v[x][i], rep); ct++; post[x] = ct; } bool potomek(int u, int v) { return ((pre[u] > pre[v]) && (post[u] < post[v])); } int sufit(double x) { if ((x - (int)x) == 0) return (int)x; else return (int)x + 1; } int lca(int u, int v) { if (potomek(u, v)) return v; else if (potomek(v, u)) return u; else { int i = u; int j = sufit(log(n) / log(2)); while (j >= 0) { if (potomek(v, p[j][i])) j--; else i = p[j][i]; } return p[0][i]; } } void dfs2(int x) { vis[x] = true; for (int i = 0; i < (int)v[x].size(); ++i) dfs2(v[x][i]); for (int i = 0; i < (int)vlca[x].size(); ++i) { LL wag = min(w[vlca[x][i].ST], w[vlca[x][i].ND]); w[vlca[x][i].ST] -= wag; w[vlca[x][i].ND] -= wag; res += 2*wag; } } int main () { scanf ("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) // Waga { scanf ("%d", &w[i]); akt[i] = i; } size = n; for (int i = 0; i < m; ++i) // Przelania { scanf ("%d%d", &a, &b); size++; p[0][akt[a]] = p[0][akt[b]] = size; v[size].PB(akt[a]); v[size].PB(akt[b]); akt[b] = size; } korzen = size; for (int i = korzen; i >= 1; --i) { if (!vis[i]) { p[0][i] = i; dfs(i, i); } } int LOGN = sufit(log(n) / log(2)); for(int i = 1; i <= LOGN; ++i) // p[i][j] = wierzcholek u taki, ze d(u, j) = 2^i for(int j = korzen; j >= 1; --j) p[i][j] = p[i-1][p[i-1][j]]; for (int i = 0; i < k; ++i) { scanf ("%d%d", &a, &b); if (r[a] == r[b]) vlca[lca(a, b)].PB(PII(a, b)); } for (int i = 0; i <= korzen; ++i) vis[i] = false; for (int i = korzen; i >= 1; --i) { if (!vis[i]) dfs2(i); } printf ("%lld", res); } |