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
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=50005; struct Edge { int from,to,pos; LL dist; bool operator < (const Edge& b) const { return dist<b.dist; } }; vector<Edge>E[25],edges; struct Query { int x,y; } q[maxn]; int father[maxn],M[maxn],a[maxn]; LL Ans[maxn]; int Get_Father(int x) { if(father[x]==x)return x; return father[x]=Get_Father(father[x]); } void Clear(vector<Edge>& edges) { for(auto& e:edges) { father[e.from]=e.from; father[e.to]=e.to; } } void Constraction(vector<Edge>& edges,LL& ans) { Clear(edges); sort(edges.begin(),edges.end()); static vector<Edge>tmpedges; tmpedges.clear(); for(auto& e:edges) { int fx=Get_Father(e.from),fy=Get_Father(e.to); if(fx==fy)continue; father[fx]=fy; tmpedges.push_back(e); } Clear(tmpedges); for(auto& e:tmpedges) { int fx=Get_Father(e.from),fy=Get_Father(e.to); if(e.dist!=-0x7fffffff/2&&fx!=fy) { father[fx]=fy; ans+=e.dist; } } tmpedges.clear(); for(auto& e:edges) if(Get_Father(e.from)!=Get_Father(e.to)) { tmpedges.push_back(e); M[e.pos]=tmpedges.size()-1; tmpedges.back().from=father[e.from]; tmpedges.back().to=father[e.to]; } edges=tmpedges; } void Reduction(vector<Edge>& edges) { Clear(edges); sort(edges.begin(),edges.end()); static vector<Edge>tmpedges; tmpedges.clear(); for(auto& e:edges) { int fx=Get_Father(e.from),fy=Get_Father(e.to); if(fx!=fy) { father[fx]=fy; tmpedges.push_back(e); M[e.pos]=tmpedges.size()-1; } else if(e.dist==0x7fffffff/2)tmpedges.push_back(e),M[e.pos]=tmpedges.size()-1; } edges=tmpedges; } void CDQBinary(int Left,int Right,int depth,LL ans) { if(Left==Right)a[q[Left].x]=q[Left].y; for(auto& e:E[depth])e.dist=a[e.pos]; edges=E[depth]; int tmp=0; for(auto& e:E[depth])M[e.pos]=tmp++; if(Left==Right) { Ans[Left]=ans; Clear(edges); sort(edges.begin(),edges.end()); for(auto& e:edges) { int fx=Get_Father(e.from),fy=Get_Father(e.to); if(fx==fy)continue; father[fx]=fy; Ans[Left]+=e.dist; } return; } for(int i=Left; i<=Right; i++)edges[M[q[i].x]].dist=-0x7fffffff/2; Constraction(edges,ans); for(int i=Left; i<=Right; i++)edges[M[q[i].x]].dist=0x7fffffff/2; Reduction(edges); E[depth+1]=edges; int mid=(Left+Right)>>1; CDQBinary(Left,mid,depth+1,ans); CDQBinary(mid+1,Right,depth+1,ans); } int n,m,Q; int main() { n=Get_Int(); m=Get_Int(); Q=Get_Int(); for(int i=1; i<=m; i++) { int x=Get_Int(),y=Get_Int(),v=Get_Int(); E[0].push_back((Edge) { x,y,i,v }); a[i]=v; } for(int i=1; i<=Q; i++) { q[i].x=Get_Int(); q[i].y=Get_Int(); } CDQBinary(1,Q,0,0); for(int i=1; i<=Q; i++)printf("%lld\n",Ans[i]); return 0; }
|