1016: [JSOI2008] minimum spanning tree count time limit:1 Sec Memory limit:162 MB
submit:6643 solved:2711
[Submit] [Status] [Discuss] Description
A simple, non-weighted graph is now given. You are not satisfied with finding the smallest spanning tree of this graph, and want to know how many different
The minimum spanning tree. (If there is at least one edge in the two smallest spanning trees, the two minimum spanning trees are different). Because of the different niche
There may be a lot of trees, so you just need to output the number of modules to 31011.
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 1~n with a full
Number of numbers. 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,0
00. 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 Output8Hintsource
Based on the Kruskal algorithm is greedy, we can conclude that the Benquan sequence of the different minimum spanning tree is the same
We first make Kruskal to get the final Benquan sequence, and then enumerate through each edge right how many different ways of composing
The last multiplication principle can be used, pay attention to use and check the set when not with the path compression, not the DFS will not backtrack, but this will become less efficient
I'm too lazy to write two of them without any trouble. Qaq
1#include <bits/stdc++.h>2 #definell Long Long3 #defineINF 10000004 #defineEPS 1e-75 #defineMOD 310116 using namespacestd;7InlineintRead () {8 intx=0;intf=1;CharCh=GetChar ();9 while(!isdigit (CH)) {if(ch=='-') f=-1; ch=GetChar ();}Ten while(IsDigit (CH)) {x=x*Ten+ch-'0'; ch=GetChar ();} One returnx*F; A } - Const intmaxn=1e6+Ten; - structnode{ the intx,y,v; - }E[MAXN]; - structedge{ - intl,r,v; + }A[MAXN]; - intf[maxn],cnt,tot,sum; +InlineintFindintx) { A returnX==F[X]?X:find (f[x]); at } -InlineBOOLmycmp (node N,node m) { - returnn.v<m.v; - } -InlinevoidDfsintStintNowintk) { - if(now==a[st].r+1){ in if(K==A[ST].V) sum++; - return; to } + intFx=find (e[now].x);intfy=find (E[NOW].Y); - if(fx!=FY) { thef[fx]=fy; *DFS (st,now+1, K +1); $f[fx]=fx;f[fy]=fy;Panax Notoginseng } -DFS (st,now+1, k); the } + intMain () { A intN=read ();intm=read (); the for(intI=1; i<=m;i++){ +E[i].x=read (); E[i].y=read (); e[i].v=read (); - } $ for(intI=1; i<=n;i++) f[i]=i; $Sort (e+1, e+m+1, mycmp); - for(intI=1; i<=m;i++){ - if(e[i].v!=e[i-1].v) { thea[++cnt].l=i;a[cnt-1].r=i-1; - }Wuyi intFx=find (e[i].x);intfy=find (E[I].Y); the if(fx!=FY) { -a[cnt].v++;f[fx]=fy;tot++; Wu } - } AboutA[cnt].r=m; $ for(intI=1; i<=n;i++) f[i]=i; - if(tot!=n-1){ -cout<<0<<endl;return 0; - } A intans=1; + for(intI=1; i<=cnt;i++){ thesum=0; -DFS (I,A[I].L,0); $ans*=sum; theans%=MOD; the for(intj=a[i].l;j<=a[i].r;j++){ theF[find (e[j].x)]=find (E[J].Y); the } - } incout<<ans<<Endl; the return 0; the}View Code
Bzoj 1016 JSOI2008 minimum Spanning tree count