HDU 4408 Minimum Spanning Tree 37th ACM/ICPC Jinhua division Network Competition 1009 questions (Minimum Spanning Tree count)

Source: Internet
Author: User
Minimum Spanning Tree

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 205 accepted submission (s): 65

Problem descriptionxxx is very interested in algorithm. after learning the prim algorithm and Kruskal algorithm of Minimum Spanning Tree, xxx finds that there might be multiple solutions. given an undirected weighted graph with n (1 <= n <= 100) vertexes and M (0 <= m <= 1000) edges, he wants to know the number of minimum spanning trees in the graph.

 

Inputthere are no more than 15 cases. The input ends by 0 0 0.
For each case, the first line begins with three integers --- the above mentioned n, m, and P. the meaning of P will be explained later. each the following M lines contains three integers U, V, W (1 <= W <= 10 ), which describes that there is an edge weighted W between vertex u and vertex v (all vertex are numbered for 1 to n ). it is guaranteed that there are no multiple edges and no loops in the graph.

 

Outputfor each test case, output a single integer in one line representing the number of different minimum spanning trees in the graph.
The answer may be quite large. you just need to calculate the remainder of the answer when divided by P (1 <= P <= 1000000000 ). P is abve mentioned, appears in the first line of each test case.

 

Sample input5 10 12 2 5 3 2 2 3 3 3 3 3 4 2 2 3 3 4 3 3 3 3 3 3 0 0 0

 

Sample output4

 

Source2012 ACM/ICPC Asia Regional Jinhua online

 

Recommendzhoujiaqi2010 Found onlineCode, Modified the AC... It's so bad .... Cup
# Include <map> # Include <Stack> # Include <Queue> # Include <Math. h> # Include <Vector> # Include < String > # Include <Stdio. h> # Include < String . H> # Include <Stdlib. h> # Include <Iostream> # Include <Algorithm> # Define N 405 # Define M 4005 # Define E # Define INF 0x3f3f3f # Define Dinf 1e10 # Define Linf (LL) 1 <60# Define Ll long # Define CLR (a, B) memset (a, B, sizeof ()) Using   Namespace  STD; ll MOD;  Struct  Edge {  Int  A, B, C;  Bool   Operator <( Const Edge & T) Const  {  Return C < T. C ;}} edge [m];  Int  N, m; ll ans;  Int  Fa [N], Ka [N], vis [N]; ll GK [N] [N], TMP [N] [N]; vector < Int > Gra [N];  Int Findfa ( Int A, Int B []) { Return A = B [a]? A: B [a] = Findfa (B [a], B);} ll det (ll a [] [N],  Int N ){  For ( Int I = 0 ; I <n; I ++) For ( Int J = 0 ; J <n; j ++) A [I] [J] % = MOD;  Long   Long Ret = 1  ;  For ( Int I =1 ; I <n; I ++ ){  For ( Int J = I + 1 ; J <n; j ++ )  While  (A [J] [I]) {ll t = A [I] [I]/ A [J] [I];  For ( Int K = I; k <n; k ++ ) A [I] [k] = (A [I] [k]-A [J] [k] * t) %MOD;  For ( Int K = I; k <n; k ++ ) Swap (A [I] [K], a [J] [k]); RET =- RET ;}  If (A [I] [I] = 0 ) Return   0  ; RET = RET * A [I] [I] % MOD;  //  RET % = MOD; }  Return (Ret + mod) % MOD ;}  Int  Main (){  While (Scanf ( "  % D % i64d  " , & N, & M, & mod) = 3  ){  If (N = 0 & M = 0 & Mod =0 ) Break  ; Memset (GK,  0 , Sizeof  (Gk); memset (TMP,  0 , Sizeof  (TMP); memset (FA,  0 , Sizeof  (Fa); memset (Ka,  0 , Sizeof  (Ka); memset (TMP, 0 , Sizeof  (TMP ));  For ( Int I = 0 ; I <n; I ++ ) Gra [I]. Clear ();  For ( Int I = 0 ; I <m; I ++ ) Scanf (  "  % D  " , & Edge [I]. A, & edge [I]. B ,&Edge [I]. c); sort (edge, Edge + M );  For ( Int I = 1 ; I <= N; I ++) Fa [I] = I, vis [I] = 0  ;  Int Pre =- 1  ; Ans = 1  ;  For ( Int H =0 ; H <= m; H ++ ){  If (Edge [H]. C! = Pre | H = M ){  For ( Int I = 1 ; I <= N; I ++ )  If  (Vis [I]) {  Int U = Findfa (I, Ka); gra [u]. push_back (I); vis [I] =0  ;}  For ( Int I = 1 ; I <= N; I ++ )  If (GRA [I]. Size ()> 1  ){  For ( Int A = 1 ; A <= N; A ++ ) For ( Int B = 1 ; B <= N; B ++ ) TMP [a] [B] = 0  ;  Int Len = Gra [I]. Size ();  For ( Int A = 0 ; A <Len; A ++ )  For (Int B = a + 1 ; B <Len; B ++ ){  Int La = gra [I] [a], lB = Gra [I] [B]; TMP [a] [B] = (TMP [B] [a]-= GK [la] [LB]); TMP [a] [A] + = GK [la] [LB]; TMP [B] [B] + = GK [la] [LB];}  Long   Long Ret = ( Long   Long ) Det (TMP, Len); RET % = MOD; ans = (ANS * RET % mod) % MOD;  For ( Int A = 0 ; A <Len; A ++) Fa [gra [I] [a] = I ;}  For ( Int I = 1 ; I <= N; I ++ ) {Ka [I] = Fa [I] =Findfa (I, FA); gra [I]. Clear ();}  If (H = m) Break  ; Pre = Edge [H]. C ;}  Int A = edge [H]. a, B = Edge [H]. B;  Int Pa = findfa (A, FA), Pb = Findfa (B, FA );  If (Pa = Pb) Continue  ; Vis [PA] = Vis [Pb] =1  ; Ka [findfa (Pa, Ka)] = Findfa (Pb, Ka); GK [PA] [Pb] ++; GK [Pb] [PA] ++ ;}  Int Flag = 0  ;  For ( Int I = 2 ; I <= N &&! Flag; I ++) If (Ka [I]! = Ka [I- 1 ]) Flag = 1 ; Ans % = MOD; printf (  "  % I64d \ n  " , Flag? 0  : ANS );}  Return   0  ;} 

 

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.