Bzoj 1016 minimum Spanning tree count

Source: Internet
Author: User

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 Output8HINT Source

This question to guess a conclusion--the number of sides of the length of I is certain and the front I small edge they make up and check the set is certain, so you can 2^n Dfs (the same length of Edge <=10).

1#include <algorithm>2#include <cstdio>3#include <cstring>4#include <cstdlib>5 using namespacestd;6 7 #defineMAXN (110)8 #defineMAXM (1010)9 #defineRHL (31011)Ten  One intFATHER[MAXN],SAVE[MAXN],BAC[MAXM],ROAD[MAXM]; A intn,m,tot,ans,sum; - structe{intU,v,w;} EDGE[MAXM]; -  theInlinevoidInit () { for(inti =1; I <= n;++i) father[i] =i;} -  -InlineintFindintA) {if(Father[a]! = a) Father[a] = find (Father[a]);returnfather[a];}  -  +InlineBOOLCMP (E a,e b) {returnA.W <B.W;} -  +InlinevoidMST () A { atSort (edge+1, edge+m+1, CMP); Init (); -     inthave =0, R1,r2,pos; -      for(inti =1; I <= m;++i) -     { -R1 = Find (edge[i].u), r2 =find (EDGE[I].V); -         if(R1! =R2) in         { -FATHER[R1] = R2; ++have ; topos = Lower_bound (bac+1, bac+tot+1, EDGE[I].W)-BAC; +++Road[pos]; -         } the         if(with = = N-1) Break; *     } $     if(With < n-1) printf ("0"), Exit (0);Panax Notoginseng } -  theInlinevoidDfsintAintRintPosintcho) + { A     if(Road[pos] = =cho) the     { +++sum; -         if(Sum = =1) memcpy (Save,father,sizeof(save)); $         return; $     } -     if(A > R)return; -     if(cho+r-a+1<road[pos])return; the     intTEMP[MAXN]; -DFS (A +1, R,pos,cho);Wuyimemcpy (Temp,father,sizeof(temp)); the     intR1 = Find (edge[a].u), r2 =find (EDGE[A].V); -     if(R1! = r2) Father[r1] = R2,dfs (A +1, r,pos,cho+1); Wumemcpy (Father,temp,sizeof(temp)); - } About  $ intMain () - { -Freopen ("1016.in","R", stdin); -Freopen ("1016.out","W", stdout); Ascanf"%d%d",&n,&m); +      for(inti =1; I <= m;++i) the     { -         inta,b,c; $scanf" %d%d%d",&a,&b,&c); theEdge[i] =(E) {a,b,c}; theBac[i] =C; the     } theSort (bac+1, bac+m+1); -tot = unique (bac+1, bac+m+1)-bac-1; in MST (); theInit (); Ans =1; the      for(inti =1; I <=m;) About     { the         intj =i; the          while(J < m && edge[j+1].W = = EDGE[I].W) + +J; thesum =0; +DFS (I,j,lower_bound (bac+1, BAC+TOT,EDGE[I].W)-bac,0); -(Ans *= sum)%=RHL; thememcpy (Father,save,sizeof(save));Bayii = j+1; the     } theprintf"%d", ans); - fclose (stdin); fclose (stdout); -     return 0; the}
View Code

Bzoj 1016 minimum Spanning tree count

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.