Description
A simple, non-weighted graph is now given. You are not satisfied with finding the smallest spanning tree of the graph, but want to know how many different minimum spanning trees are in the graph. (If there is at least one edge in the two smallest spanning trees, the two minimum spanning trees are different). Because the different minimum spanning tree may be many, you only need to output the scheme number to 31011 modulo.
Input
The first line contains two numbers, N and M, where 1<=n<=100; 1<=m<=1000; Represents the number of nodes and sides of the graph. Each node is numbered with an integer of 1~n. The next M-line, each line contains two integers: A, B, C, the weight of the edge between Node A and B is C, where 1<=c<=1,000,000,000. Data guarantees that no self-back and heavy edges are present. Note: An edge with the same weight will not exceed 10 bars.
Output
How many of the different minimum spanning trees are output. You just need to output a number of 31011 modulo.
Sample Input4 6
1 2 1
1 3 1
1 4 1
2 3 2
2 4 1
3 4 1Sample Output8This problem seems to be the classic problem of each length of the number of edges, it is obvious that the scheme with these edges to make MST and other edges are irrelevant, such as the current MST only 1, 2, 5, from the length of all sides of the K set two, together into 1, 2, 3, 4, 5, then the selection of these two and the length of the side is not the k is irrelevant, so directly explode the edge of each length, and then you can directly ride up
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include < algorithm> #include <cmath> #include <queue> #include <deque> #include <set> #include <map > #include <ctime> #define LL long long#define INF 0x7ffffff#define pa pair<int,int> #define MOD 31011using Namespace Std;inline ll read () {ll x=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct Edge{int x, y, Z;} e[100010];inline BOOL operator < (const edge &a,const edge &b) {return a.z<b.z;} struct Seg{int l,r,v;} A[100010];int n,m,cnt,tot,sum,ans=1;int fa[100010];inline int GETFA (int x) {return FA[X]==X?X:GETFA (fa[x]);} inline void dfs (int x,int now,int k) {if (now==a[x].r+1) {if (K==A[X].V) Sum++;return;} int FX=GETFA (e[now].x), FY=GETFA (E[NOW].Y), if (fx!=fy) {Fa[fx]=fy;dfs (x,now+1,k+1); fa[fx]=fx;fa[fy]=fy;} DFS (X,NOW+1,K);} int main ({N=read (); M=read (); for (int i=1;i<=m;i++) {e[i].x=read (); E[i].y=read (); E[i].z=read ();} Sort (e+1,e+m+1), for (int i=1;i<=n;i++) fa[i]=i;for (int i=1;i<=m;i++) {if (e[i].z!=e[i-1].z) {a[++cnt].l=i;a[ Cnt-1].r=i-1;} int FX=GETFA (e[i].x), FY=GETFA (E[I].Y), if (fx!=fy) {a[cnt].v++;fa[fx]=fy;tot++;}} A[cnt].r=m;if (tot!=n-1) {printf ("0"); return 0;} for (int i=1;i<=n;i++) fa[i]=i;for (int i=1;i<=cnt;i++) {Sum=0;dfs (i,a[i].l,0); ans= (ans*sum)%mod;for (int k=a[i] . l;k<=a[i].r;k++) {int FX=GETFA (e[k].x), FY=GETFA (E[K].Y), if (Fx!=fy) fa[fx]=fy;}} printf ("%d\n", ans);}
bzoj1016 [JSOI2008] minimum spanning tree count