Idea: The minimum spanning tree count simply adds an expectation that, due to the linear nature of the expectation, it can be converted to the sum of the expectations of each edge, so what is the expectation of an edge, and in the minimum spanning tree counts, the same edge right is processed together, and then the point shrinks within the connected block, that is, An edge can only contribute to the answer within the connected block it belongs to, and then because the indentation does not affect the answer, the expectation of an edge is equal to the number of spanning trees within the connected block to which it belongs, divided by the number of spanning trees of that connected block, And the number of spanning tree containing this edge is the number of spanning tree in the connected block minus the spanning tree that does not contain this edge, and then using the Matrix-tree theorem to statistic the answer, because this problem to enumerate edges, so it is best to write two and check set, anyway, I can not write the DFS before.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <vector>using namespace std; #define MAXM 200005#define maxn 10005const long double eps=1e-9; int N,m,cnt,top;int pos[maxn],stack[maxn];bool Instack[maxn];long long Tot;long double ans,k[1000][1000],t[1000][1000 ]; Vector<int> V[MAXN]; struct edge{int from,to,dis,val; BOOL operator < (const edge &a) Const{return Dis<a.dis;}} E[MAXM]; inline int read () {int X=0;char ch=getchar (); for (;ch< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()); for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x;} struct union_find_set{int FA[MAXN]; int find (int x) {return fa[x]==x?x:fa[x]=find (fa[x]);}} U1,U2; Long Long Gauss () {int T,n=cnt-1,f=1;long double Ans=1; for (int i=1;i<n;i++) {for (t=i;t<=n;t++) if (Fabs (K[t][i]) >eps) break;if (t>n) return 0; if (t!=i) {for (int j=1;j<=n;j++) swap (k[i][j],k[t][j]);F=-f;} for (int j=i+1;j<=n;j++) if (Fabs (k[j][i)) >eps) {long double t=k[j][i]/k[i][i]; for (int k=i;k<=n;k++) k[j][k]-=k[i][k]*t; }} for (int i=1;i<=n;i++) ans=ans*k[i][i]; Return round (ans*f);} void Add (int x,int y,int val) {k[x][y]-=val,k[y][x]-=val; K[x][x]+=val,k[y][y]+=val;} int main () {N=read (), M=read (); for (int i=1;i<=m;i++) E[i].from=read (), E[i].to=read (), E[i].dis=read (), E[i].val=read (); for (int i=1;i<=n;i++) u1.fa[i]=u2.fa[i]=i; Sort (e+1,e+m+1); for (int i=1,l=1;i<=m+1;i++) {int X=u1.find (e[i].from), Y=u1.find (e[i].to); if (x!=y) {int u=u2.find (x), V=u2.find (y), if (U!=v) u2.fa[u]=v;} if (E[i].dis!=e[i+1].dis) {for (int j=l;j<=i;j++) {int X=u1.find (e[j].from), Y=u1.find (e[j].to ); if (x==y) continue; int U=u2.find (x); if (!instack[u]) stack[++top]=u,instack[u]=1; } while (top) {instack[stack[top]]=0,cnt=0; for (int j=l;j<=i;j++) {int X=u1.find (e[j].from), Y=u1.find (e[j].to); if (x==y) continue; int U=u2.find (x); if (U==stack[top]) {if (!pos[x]) pos[x]=++cnt; if (!pos[y]) pos[y]=++cnt; Add (pos[x],pos[y],1); }} for (int a=1;a<=cnt;a++) for (int b=1;b<=cnt;b++) T[A][B]=K[A][B]; Tot=gauss (); for (int. a=1;a<=cnt;a++) for (int b=1;b<=cnt;b++) k[a][b]=t[a][b]; for (int j=l;j<=i;j++) {int X=u1.find (e[j].from), Y=u1.find (e[j].to); if (x==y) continue; int U=u2.find (x); if (U==stack[top]) {for (int a=1;a<=cnt;a++) for (int b=1;b<=cnt;b++) t[a][b]=k[a][b]; Add (pos[x],pos[y],-1); Long long Tmp=gauss (); for (int a=1;a<=cnt;a++) for (int b=1;b<=cnt;b++) k[a][b]= T[A][B]; ans+=1.0* (tot-tmp)/tot*e[j].val; }} for (int j=l;j<=i;j++) {int X=u1.find (e[j].from), Y=u1.find (e[j].to ); if (x==y) continue;pos[x]=pos[y]=0; } for (int j=1;j<=cnt;j++) for (int k=1;k<=cnt;k++) k[j][k] = 0; top--; } for (int j=l;j<=i;j++) {int X=u1.find (e[j].from), Y=u1.find (e[j].to); if (x==y) continue;u1.fa[x]=y; } l=i+1; }} printf ("%.5lf", (double) ans); return 0;}
bzoj4637: Expectations