// Artur Minorczyk #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <vector> #include <list> #include <set> #include <map> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PI; typedef vector<PI> VPI; typedef long long LL; typedef vector<LL> VLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define ST first #define ND second const int INF = 1000000001; const double EPS = 10e-9; int TreeHeight(int n) { int i = 1, l = 1; while(l < n) { i++; l <<= 1; } return i; } template <class V, class E> struct Graph { struct Ed : E { int v; Ed(E p = E(), int w = 0) : E(p), v(w) { } }; struct Ve : V, vector<Ed> { }; vector<Ve> g; VVI p; int t; Graph(int n = 0) : g(n), p(TreeHeight(n), VI(n)) { t = 0; REP(x, n) { g[x].n = g[x].sp = -1; g[x].osad = 0; } } void EdgeU(int b, int e, E d = E()) { Ed eg(d, e); eg.t = t; g[b].PB(eg); eg.v = b; eg.t = t++; g[e].PB(eg); g[b].n = e; } VI spojne; void SpojnaDfs(int v, int sp) { g[v].sp = sp; FOREACH(it, g[v]) if(g[it->v].sp == -1) SpojnaDfs(it->v, sp); } void Spojna() { int spnr; REP(x, SIZE(g)) if(g[x].sp == -1) { spnr = x; while(g[spnr].n != -1) spnr = g[spnr].n; SpojnaDfs(spnr, spnr); spojne.PB(spnr); } } void DfsV(int v) { g[v].d = ++t; FOREACH(it, g[v]) if(g[it->v].d == -1) DfsV(it->v); g[v].f = ++t; } void Dfs() { t = -1; Ed eg; REP(x, SIZE(g)) FOREACH(it, g[x]) if(it->v == g[x].n) { eg = *it; g[x].erase(it); g[x].PB(eg); } Spojna(); REP(x, SIZE(g)) g[x].d = g[x].f = -1; FOREACH(it, spojne) DfsV(*it); REP(x, SIZE(g)) if(g[x].n == -1) p[0][x] = x; else p[0][x] = g[x].n; FOR(i, 1, SIZE(p) - 1) REP(j, SIZE(g)) p[i][j] = p[i - 1][p[i - 1][j]]; } bool Potomek(int u, int v) { return u == v || (g[v].d < g[u].d && g[v].f > g[u].f); } int KrawedzPotomka(int u, int v) { int p = 0, q, s; q = g[v].n == -1 ? SIZE(g[v]) - 1 : SIZE(g[v]) - 2; while(p < q) { s = (p + q) / 2; if(Potomek(u, g[v][s].v)) p = q = s; else if(g[u].f > g[g[v][s].v].f) p = s + 1; else q = s - 1; } return p; } void LCA(int v, int u) { if(g[v].sp != g[u].sp) return; int s, r; if(Potomek(u, v)) { s = KrawedzPotomka(u, v); g[v][s].l.PB(MP(v, u)); } else if(Potomek(v, u)) { s = KrawedzPotomka(v, u); g[u][s].l.PB(MP(v, u)); } else { int i = u, j = SIZE(p) - 1; while(j >= 0) { if(Potomek(v, p[j][i])) j--; else i = p[j][i]; } s = KrawedzPotomka(v, p[0][i]); r = KrawedzPotomka(u, p[0][i]); if(g[p[0][i]][s].t > g[p[0][i]][r].t) g[p[0][i]][s].l.PB(MP(v, u)); else g[p[0][i]][r].l.PB(MP(v, u)); } } struct djcmp { bool operator() (const Ed a, const Ed b) const { return a.t < b.t; } }; void Reaguj(int v, int a, int b) { int r = min(g[v].k[a], g[v].k[b]); g[v].osad += 2 * r; g[v].k[a] -= r; if(g[v].k[a] == 0) g[v].k.erase(a); g[v].k[b] -= r; if(g[v].k[b] == 0) g[v].k.erase(b); } void ComputeDfs(int v) { djcmp comparestr; FOREACH(it, g[v]) if(it->v != g[v].n) ComputeDfs(it->v); sort(ALL(g[v]), comparestr); int r = g[v].n == -1 ? SIZE(g[v]) : SIZE(g[v]) - 1; REP(x, r) { g[v].osad += g[g[v][x].v].osad; FOREACH(it, g[g[v][x].v].k) g[v].k[it->ST] = it->ND; FOREACH(it, g[v][x].l) Reaguj(v, it->ST, it->ND); } } LL Compute() { LL wynik = 0; FOREACH(it, spojne) { ComputeDfs(*it); wynik += g[*it].osad; } return wynik; } void WriteIngredients() { REP(x, SIZE(g)) { printf("%d:\n", x + 1); FOREACH(it, g[x].k) printf("%d => %d\n", it->ST + 1, it->ND); } } }; struct Ve { int t; VPI l; }; struct Vs { int n, sp, d, f; map<int, int> k; LL osad; }; int main() { int n, m, k, b, e; scanf("%d%d%d", &n, &m, &k); Graph<Vs, Ve> g(n); REP(x, n) { scanf("%d", &b); g.g[x].k[x] = b; } REP(x, m) { scanf("%d%d", &b, &e); g.EdgeU(b - 1, e - 1); } g.Dfs(); REP(x, k) { scanf("%d%d", &b, &e); g.LCA(b - 1, e - 1); } printf("%lld\n", g.Compute()); 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | // Artur Minorczyk #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <vector> #include <list> #include <set> #include <map> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PI; typedef vector<PI> VPI; typedef long long LL; typedef vector<LL> VLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define ST first #define ND second const int INF = 1000000001; const double EPS = 10e-9; int TreeHeight(int n) { int i = 1, l = 1; while(l < n) { i++; l <<= 1; } return i; } template <class V, class E> struct Graph { struct Ed : E { int v; Ed(E p = E(), int w = 0) : E(p), v(w) { } }; struct Ve : V, vector<Ed> { }; vector<Ve> g; VVI p; int t; Graph(int n = 0) : g(n), p(TreeHeight(n), VI(n)) { t = 0; REP(x, n) { g[x].n = g[x].sp = -1; g[x].osad = 0; } } void EdgeU(int b, int e, E d = E()) { Ed eg(d, e); eg.t = t; g[b].PB(eg); eg.v = b; eg.t = t++; g[e].PB(eg); g[b].n = e; } VI spojne; void SpojnaDfs(int v, int sp) { g[v].sp = sp; FOREACH(it, g[v]) if(g[it->v].sp == -1) SpojnaDfs(it->v, sp); } void Spojna() { int spnr; REP(x, SIZE(g)) if(g[x].sp == -1) { spnr = x; while(g[spnr].n != -1) spnr = g[spnr].n; SpojnaDfs(spnr, spnr); spojne.PB(spnr); } } void DfsV(int v) { g[v].d = ++t; FOREACH(it, g[v]) if(g[it->v].d == -1) DfsV(it->v); g[v].f = ++t; } void Dfs() { t = -1; Ed eg; REP(x, SIZE(g)) FOREACH(it, g[x]) if(it->v == g[x].n) { eg = *it; g[x].erase(it); g[x].PB(eg); } Spojna(); REP(x, SIZE(g)) g[x].d = g[x].f = -1; FOREACH(it, spojne) DfsV(*it); REP(x, SIZE(g)) if(g[x].n == -1) p[0][x] = x; else p[0][x] = g[x].n; FOR(i, 1, SIZE(p) - 1) REP(j, SIZE(g)) p[i][j] = p[i - 1][p[i - 1][j]]; } bool Potomek(int u, int v) { return u == v || (g[v].d < g[u].d && g[v].f > g[u].f); } int KrawedzPotomka(int u, int v) { int p = 0, q, s; q = g[v].n == -1 ? SIZE(g[v]) - 1 : SIZE(g[v]) - 2; while(p < q) { s = (p + q) / 2; if(Potomek(u, g[v][s].v)) p = q = s; else if(g[u].f > g[g[v][s].v].f) p = s + 1; else q = s - 1; } return p; } void LCA(int v, int u) { if(g[v].sp != g[u].sp) return; int s, r; if(Potomek(u, v)) { s = KrawedzPotomka(u, v); g[v][s].l.PB(MP(v, u)); } else if(Potomek(v, u)) { s = KrawedzPotomka(v, u); g[u][s].l.PB(MP(v, u)); } else { int i = u, j = SIZE(p) - 1; while(j >= 0) { if(Potomek(v, p[j][i])) j--; else i = p[j][i]; } s = KrawedzPotomka(v, p[0][i]); r = KrawedzPotomka(u, p[0][i]); if(g[p[0][i]][s].t > g[p[0][i]][r].t) g[p[0][i]][s].l.PB(MP(v, u)); else g[p[0][i]][r].l.PB(MP(v, u)); } } struct djcmp { bool operator() (const Ed a, const Ed b) const { return a.t < b.t; } }; void Reaguj(int v, int a, int b) { int r = min(g[v].k[a], g[v].k[b]); g[v].osad += 2 * r; g[v].k[a] -= r; if(g[v].k[a] == 0) g[v].k.erase(a); g[v].k[b] -= r; if(g[v].k[b] == 0) g[v].k.erase(b); } void ComputeDfs(int v) { djcmp comparestr; FOREACH(it, g[v]) if(it->v != g[v].n) ComputeDfs(it->v); sort(ALL(g[v]), comparestr); int r = g[v].n == -1 ? SIZE(g[v]) : SIZE(g[v]) - 1; REP(x, r) { g[v].osad += g[g[v][x].v].osad; FOREACH(it, g[g[v][x].v].k) g[v].k[it->ST] = it->ND; FOREACH(it, g[v][x].l) Reaguj(v, it->ST, it->ND); } } LL Compute() { LL wynik = 0; FOREACH(it, spojne) { ComputeDfs(*it); wynik += g[*it].osad; } return wynik; } void WriteIngredients() { REP(x, SIZE(g)) { printf("%d:\n", x + 1); FOREACH(it, g[x].k) printf("%d => %d\n", it->ST + 1, it->ND); } } }; struct Ve { int t; VPI l; }; struct Vs { int n, sp, d, f; map<int, int> k; LL osad; }; int main() { int n, m, k, b, e; scanf("%d%d%d", &n, &m, &k); Graph<Vs, Ve> g(n); REP(x, n) { scanf("%d", &b); g.g[x].k[x] = b; } REP(x, m) { scanf("%d%d", &b, &e); g.EdgeU(b - 1, e - 1); } g.Dfs(); REP(x, k) { scanf("%d%d", &b, &e); g.LCA(b - 1, e - 1); } printf("%lld\n", g.Compute()); return 0; } |