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
| #include <bits/stdc++.h> #define fir first #define sec second #define MAX_N 20000 #define MAX_M 50000 #define INF 0x3f3f3f3f #define mid ((s+t)>>1) using namespace std; typedef long long lnt; typedef pair<int,int> pii; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, q; lnt ans[MAX_M+5]; int fa[MAX_N+5], w[MAX_M+5], pos[MAX_M+5]; struct edge {int u, v, c, id;} ; pii qry[MAX_M+5]; bool cmp (const edge &x, const edge &y) {return x.c < y.c;} int getf(int x) {return fa[x] == x ? x : fa[x] = getf(fa[x]);} lnt contraction(vector <edge> &E) { vector <edge> e; lnt ret = 0; sort(E.begin(), E.end(), cmp); for (int i = 0, x, y; i < (int)E.size(); i++) if ((x = getf(E[i].u)) ^ (y = getf(E[i].v))) fa[y] = x, e.push_back(E[i]); for (int i = 0; i < (int)e.size(); i++) fa[e[i].u] = e[i].u, fa[e[i].v] = e[i].v; for (int i = 0; i < (int)e.size(); i++) if (e[i].c > -INF && (getf(e[i].u) ^ getf(e[i].v))) fa[getf(e[i].v)] = getf(e[i].u), ret += e[i].c; e.clear(); for (int i = 0; i < (int)E.size(); i++) if (getf(E[i].u) ^ getf(E[i].v)) pos[E[i].id] = (int)e.size(), e.push_back(E[i]), e[e.size()-1].u = fa[E[i].u], e[e.size()-1].v = fa[E[i].v]; for (int i = 0; i < (int)E.size(); i++) fa[E[i].u] = E[i].u, fa[E[i].v] = E[i].v; E = e; return ret; } void reduction(vector <edge> &E) { vector <edge> e; sort(E.begin(), E.end(), cmp); for (int i = 0, x, y; i < (int)E.size(); i++) if ((x = getf(E[i].u)) ^ (y = getf(E[i].v))) fa[y] = x, e.push_back(E[i]); else if (E[i].c == INF) e.push_back(E[i]); for (int i = 0; i < (int)E.size(); i++) fa[E[i].u] = E[i].u, fa[E[i].v] = E[i].v; E = e; } void bi_solve(vector <edge> E, int s, int t, lnt sum) { if (s == t) w[qry[s].fir] = qry[s].sec; for (int i = 0; i < (int)E.size(); i++) E[i].c = w[E[i].id], pos[E[i].id] = i; if (s == t) { sort(E.begin(), E.end(), cmp); for (int i = 0, x, y; i < (int)E.size(); i++) if ((x = getf(E[i].u)) ^ (y = getf(E[i].v))) fa[y] = x, sum += E[i].c; for (int i = 0; i < (int)E.size(); i++) fa[E[i].u] = E[i].u, fa[E[i].v] = E[i].v; ans[s] = sum; return; } for (int i = s; i <= t; i++) E[pos[qry[i].fir]].c = -INF; sum += contraction(E); for (int i = s; i <= t; i++) E[pos[qry[i].fir]].c = INF; reduction(E); bi_solve(E, s, mid, sum); bi_solve(E, mid+1, t, sum); } int main() { read(n), read(m), read(q); vector <edge> E; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1, u, v, c; i <= m; i++) read(u), read(v), read(c), w[i] = c, E.push_back((edge){u, v, c, i}); for (int i = 1; i <= q; i++) read(qry[i].fir), read(qry[i].sec); bi_solve(E, 1, q, 0); for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]); return 0; }
|