/* * Uzylem algorytmu: * http://en.wikipedia.org/wiki/Tarjan%27s_off-line_lowest_common_ancestors_algorithm * */ #include <cstdio> #include <algorithm> #include <vector> #include <list> #include <stack> #include <queue> #include <cmath> #include <map> #include <string> #include <set> #include <cstring> #include <iostream> #include <sstream> #include <cassert> using namespace std; #define PB push_back #define FORE(i,t) for(typeof(t.begin())i=t.begin();i!=t.end();++i) #define SZ(x) int((x).size()) #define REP(i,n) for(int i=0,_=(n);i<_;++i) #define FOR(i,a,b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i,a,b) for(int i=(a),_=(b);i>=_;--i) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; const int N = 200002; const int K = 500002; struct Reaction { int a, b; pii priority; bool operator<(const Reaction &other) const { return priority < other.priority; } }; vi grams(N); vector<vi> children(N); vi parent(N, -1); vector<vector<pii> > queries(N); vi ancestor(N); vi fu_parent(N); vector<int> color(N); vector<vector<Reaction> > reactions(N); vector<pii> visited(N); vector<pii> sequence; void visit(int x, int &t) { visited[x].first = t++; FORE(yt, children[x]) { int y = *yt; visit(y, t); } visited[x].second = t++; } void fu_makeset(int x) { fu_parent[x] = -1; } int fu_find(int x) { if (fu_parent[x] < 0) return x; return fu_parent[x] = fu_find(fu_parent[x]); } void fu_union(int x, int y) { x = fu_find(x), y = fu_find(y); if (fu_parent[x] < fu_parent[y]) fu_parent[y] = x; else if (fu_parent[x] > fu_parent[y]) fu_parent[x] = y; else if (x != y) { fu_parent[y] = x; --fu_parent[x]; } } void tarjan(int u, int clr) { fu_makeset(u); ancestor[u] = u; FORE(vt, children[u]) { int v = *vt; tarjan(v, clr); fu_union(u, v); ancestor[fu_find(u)] = u; } color[u] = clr; FORE(vt, queries[u]) { int v = vt->first; int reaction_id = vt->second; if (color[v] == clr) { int anc = ancestor[fu_find(v)]; reactions[anc].PB((Reaction) { u, v, pii(INF, reaction_id) }); } } } int entry_time(int u, int w) { int a = 0; int b = SZ(children[u]) - 1; int w_time = visited[w].first; if (u == w) { return w_time; } while (a <= b) { int d = (a + b) / 2; int d_time = visited[children[u][d]].first; if (d_time <= w_time) { a = d + 1; } else { b = d - 1; } } return visited[children[u][b]].first; } void inline jeden() { int n, m, k; scanf("%d%d%d", &n, &m, &k); FOR(i, 1, n) { scanf("%d", &grams[i]); } REP(i, m) { int a, b; scanf("%d%d", &a, &b); children[b].PB(a); parent[a] = b; sequence.PB(pii(a, b)); } REP(i, k) { int a, b; scanf("%d%d", &a, &b); queries[a].PB(pii(b, i)); queries[b].PB(pii(a, i)); } { int visit_time = 0; FOR(i, 1, n) { if (parent[i] == -1) { tarjan(i, i); visit(i, visit_time); } } } vi final_list; REP(i, m) { int u = sequence[i].first; final_list.PB(u); } FOR(i, 1, n) { if (parent[i] == -1) { final_list.PB(i); } } ll result = 0; FORE(ut, final_list) { int u = *ut; FORE(r, reactions[u]) { r->priority.first = max( entry_time(u, r->a), entry_time(u, r->b)); } sort(reactions[u].begin(), reactions[u].end()); FORE(r, reactions[u]) { int amount = min(grams[r->a], grams[r->b]); result += 2LL * amount; grams[r->a] -= amount; grams[r->b] -= amount; } } printf("%lld\n", result); } int main() { //int z; scanf("%d", &z); while(z--) jeden(); }
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 200 201 202 203 204 | /* * Uzylem algorytmu: * http://en.wikipedia.org/wiki/Tarjan%27s_off-line_lowest_common_ancestors_algorithm * */ #include <cstdio> #include <algorithm> #include <vector> #include <list> #include <stack> #include <queue> #include <cmath> #include <map> #include <string> #include <set> #include <cstring> #include <iostream> #include <sstream> #include <cassert> using namespace std; #define PB push_back #define FORE(i,t) for(typeof(t.begin())i=t.begin();i!=t.end();++i) #define SZ(x) int((x).size()) #define REP(i,n) for(int i=0,_=(n);i<_;++i) #define FOR(i,a,b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i,a,b) for(int i=(a),_=(b);i>=_;--i) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; const int N = 200002; const int K = 500002; struct Reaction { int a, b; pii priority; bool operator<(const Reaction &other) const { return priority < other.priority; } }; vi grams(N); vector<vi> children(N); vi parent(N, -1); vector<vector<pii> > queries(N); vi ancestor(N); vi fu_parent(N); vector<int> color(N); vector<vector<Reaction> > reactions(N); vector<pii> visited(N); vector<pii> sequence; void visit(int x, int &t) { visited[x].first = t++; FORE(yt, children[x]) { int y = *yt; visit(y, t); } visited[x].second = t++; } void fu_makeset(int x) { fu_parent[x] = -1; } int fu_find(int x) { if (fu_parent[x] < 0) return x; return fu_parent[x] = fu_find(fu_parent[x]); } void fu_union(int x, int y) { x = fu_find(x), y = fu_find(y); if (fu_parent[x] < fu_parent[y]) fu_parent[y] = x; else if (fu_parent[x] > fu_parent[y]) fu_parent[x] = y; else if (x != y) { fu_parent[y] = x; --fu_parent[x]; } } void tarjan(int u, int clr) { fu_makeset(u); ancestor[u] = u; FORE(vt, children[u]) { int v = *vt; tarjan(v, clr); fu_union(u, v); ancestor[fu_find(u)] = u; } color[u] = clr; FORE(vt, queries[u]) { int v = vt->first; int reaction_id = vt->second; if (color[v] == clr) { int anc = ancestor[fu_find(v)]; reactions[anc].PB((Reaction) { u, v, pii(INF, reaction_id) }); } } } int entry_time(int u, int w) { int a = 0; int b = SZ(children[u]) - 1; int w_time = visited[w].first; if (u == w) { return w_time; } while (a <= b) { int d = (a + b) / 2; int d_time = visited[children[u][d]].first; if (d_time <= w_time) { a = d + 1; } else { b = d - 1; } } return visited[children[u][b]].first; } void inline jeden() { int n, m, k; scanf("%d%d%d", &n, &m, &k); FOR(i, 1, n) { scanf("%d", &grams[i]); } REP(i, m) { int a, b; scanf("%d%d", &a, &b); children[b].PB(a); parent[a] = b; sequence.PB(pii(a, b)); } REP(i, k) { int a, b; scanf("%d%d", &a, &b); queries[a].PB(pii(b, i)); queries[b].PB(pii(a, i)); } { int visit_time = 0; FOR(i, 1, n) { if (parent[i] == -1) { tarjan(i, i); visit(i, visit_time); } } } vi final_list; REP(i, m) { int u = sequence[i].first; final_list.PB(u); } FOR(i, 1, n) { if (parent[i] == -1) { final_list.PB(i); } } ll result = 0; FORE(ut, final_list) { int u = *ut; FORE(r, reactions[u]) { r->priority.first = max( entry_time(u, r->a), entry_time(u, r->b)); } sort(reactions[u].begin(), reactions[u].end()); FORE(r, reactions[u]) { int amount = min(grams[r->a], grams[r->b]); result += 2LL * amount; grams[r->a] -= amount; grams[r->b] -= amount; } } printf("%lld\n", result); } int main() { //int z; scanf("%d", &z); while(z--) jeden(); } |