#include <cstdio> #include <vector> #include <set> #include <algorithm> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define MP make_pair #define FI first #define SE second #define PII pair<int, int> #define PB push_back #define LL long long using namespace std; int n, m, k; vector<int> sizes; vector<int> p; vector<int> ranks; vector<vector<PII > > r; vector<int> parent; vector<vector<PII > > children; vector<int> ancestor; vector<bool> color; vector<int> colored; vector<vector<PII > > meet; vector<pair<PII, PII > > final; vector<vector<PII > > notif; LL result; // Implementation of disjoint sets and LCA (Tarjan's algo) based on Cormen - start void makeSet(int x) { p[x] = x; ranks[x] = 0; } int findSet(int x) { if (x != p[x]) p[x] = findSet(p[x]); return p[x]; } void setLink(int x, int y) { if (ranks[x] > ranks[y]) p[y] = x; else { p[x] = y; if (ranks[x] == ranks[y]) ++ranks[y]; } } void setUnion(int x, int y) { setLink(findSet(x), findSet(y)); } void LCA(int u) { makeSet(u); ancestor[findSet(u)] = u; notif[u].clear(); int numChildren = children[u].size(); for (int i = 0; i < numChildren; ++i) { int v = children[u][i].FI; LCA(v); // Save reaction order int numMeet = meet[u].size(); for (int j = 0; j < numMeet; ++j) { int r1 = meet[u][j].FI; int r2 = r[r1][meet[u][j].SE].FI; int reactOrder = r[r1][meet[u][j].SE].SE; int traverseOrder = children[u][i].SE; final.PB(MP(MP(traverseOrder, reactOrder), MP(r1, r2))); } meet[u].clear(); int numNotif = notif[u].size(); for (int j = 0; j < numNotif; ++j) { int r1 = notif[u][j].FI; int r2 = r[r1][notif[u][j].SE].FI; int reactOrder = r[r1][notif[u][j].SE].SE; int traverseOrder = children[u][i].SE; final.PB(MP(MP(traverseOrder, reactOrder), MP(r1, r2))); } notif[u].clear(); // setUnion(u, v); ancestor[findSet(u)] = u; } color[u] = true; colored.PB(u); int numReacting = r[u].size(); for (int i = 0; i < numReacting; ++i) { int v = r[u][i].FI; if (color[v]) { int lca = ancestor[findSet(v)]; meet[lca].PB(MP(u, i)); } else { notif[v].PB(MP(u, i)); } } } // Implementation of disjoint sets and LCA (Tarjan's algo) based on Cormen - end void readInput() { scanf("%d%d%d", &n, &m, &k); REP(i, n) { int size; scanf("%d", &size); sizes.PB(size); } children.resize(n); parent.resize(n, -1); REP(i, m) { int from; int to; scanf("%d%d", &from, &to); --from; --to; children[to].PB(MP(from, i)); parent[from] = to; } r.resize(n); REP(i, k) { int s1; int s2; scanf("%d%d", &s1, &s2); --s1; --s2; r[s1].PB(MP(s2, i)); r[s2].PB(MP(s1, i)); } } int main() { readInput(); ancestor.resize(n); color.resize(n); meet.resize(n); p.resize(n); ranks.resize(n); notif.resize(n); REP(i, n) { if (parent[i] == -1) { colored.clear(); LCA(i); int numColored = colored.size(); REP(j, numColored) color[j] = false; } } sort(final.begin(), final.end()); int numFinal = final.size(); REP(i, numFinal) { int r1 = final[i].SE.FI; int r2 = final[i].SE.SE; int size1 = sizes[r1]; int size2 = sizes[r2]; int minimum = min(size1, size2); sizes[r1] -= minimum; sizes[r2] -= minimum; result += (LL)(2*minimum); } 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 | #include <cstdio> #include <vector> #include <set> #include <algorithm> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define MP make_pair #define FI first #define SE second #define PII pair<int, int> #define PB push_back #define LL long long using namespace std; int n, m, k; vector<int> sizes; vector<int> p; vector<int> ranks; vector<vector<PII > > r; vector<int> parent; vector<vector<PII > > children; vector<int> ancestor; vector<bool> color; vector<int> colored; vector<vector<PII > > meet; vector<pair<PII, PII > > final; vector<vector<PII > > notif; LL result; // Implementation of disjoint sets and LCA (Tarjan's algo) based on Cormen - start void makeSet(int x) { p[x] = x; ranks[x] = 0; } int findSet(int x) { if (x != p[x]) p[x] = findSet(p[x]); return p[x]; } void setLink(int x, int y) { if (ranks[x] > ranks[y]) p[y] = x; else { p[x] = y; if (ranks[x] == ranks[y]) ++ranks[y]; } } void setUnion(int x, int y) { setLink(findSet(x), findSet(y)); } void LCA(int u) { makeSet(u); ancestor[findSet(u)] = u; notif[u].clear(); int numChildren = children[u].size(); for (int i = 0; i < numChildren; ++i) { int v = children[u][i].FI; LCA(v); // Save reaction order int numMeet = meet[u].size(); for (int j = 0; j < numMeet; ++j) { int r1 = meet[u][j].FI; int r2 = r[r1][meet[u][j].SE].FI; int reactOrder = r[r1][meet[u][j].SE].SE; int traverseOrder = children[u][i].SE; final.PB(MP(MP(traverseOrder, reactOrder), MP(r1, r2))); } meet[u].clear(); int numNotif = notif[u].size(); for (int j = 0; j < numNotif; ++j) { int r1 = notif[u][j].FI; int r2 = r[r1][notif[u][j].SE].FI; int reactOrder = r[r1][notif[u][j].SE].SE; int traverseOrder = children[u][i].SE; final.PB(MP(MP(traverseOrder, reactOrder), MP(r1, r2))); } notif[u].clear(); // setUnion(u, v); ancestor[findSet(u)] = u; } color[u] = true; colored.PB(u); int numReacting = r[u].size(); for (int i = 0; i < numReacting; ++i) { int v = r[u][i].FI; if (color[v]) { int lca = ancestor[findSet(v)]; meet[lca].PB(MP(u, i)); } else { notif[v].PB(MP(u, i)); } } } // Implementation of disjoint sets and LCA (Tarjan's algo) based on Cormen - end void readInput() { scanf("%d%d%d", &n, &m, &k); REP(i, n) { int size; scanf("%d", &size); sizes.PB(size); } children.resize(n); parent.resize(n, -1); REP(i, m) { int from; int to; scanf("%d%d", &from, &to); --from; --to; children[to].PB(MP(from, i)); parent[from] = to; } r.resize(n); REP(i, k) { int s1; int s2; scanf("%d%d", &s1, &s2); --s1; --s2; r[s1].PB(MP(s2, i)); r[s2].PB(MP(s1, i)); } } int main() { readInput(); ancestor.resize(n); color.resize(n); meet.resize(n); p.resize(n); ranks.resize(n); notif.resize(n); REP(i, n) { if (parent[i] == -1) { colored.clear(); LCA(i); int numColored = colored.size(); REP(j, numColored) color[j] = false; } } sort(final.begin(), final.end()); int numFinal = final.size(); REP(i, numFinal) { int r1 = final[i].SE.FI; int r2 = final[i].SE.SE; int size1 = sizes[r1]; int size2 = sizes[r2]; int minimum = min(size1, size2); sizes[r1] -= minimum; sizes[r2] -= minimum; result += (LL)(2*minimum); } printf("%lld\n", result); return 0; } |