Hdoj topic 4408 Minimum Spanning Tree (kruskal+matrix_tree)

Source: Internet
Author: User

Minimum Spanning TreeTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1408 Accepted Submission (s): 450


Problem Descriptionxxx is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds this there might be multiple s Olutions. 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 is no more than 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 would be explained later. Each of the following m lines contains three integers u, V, W (1<=w<=10), which describes that there was an edge Weighte D W between vertex u and Vertex v (all vertex is numbered for 1 to N). It is guaranteed This there is no multiple edges and no loops in the graph.

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

Sample Input
5 10 122 5 32 4 23 1 33 4 21 2 35 4 35 1 34 1 15 3 33 2 30 0 0

Sample Output
4

Source2012 ACM/ICPC Asia Regional Jinhua Online
Recommendzhoujiaqi2010   |   we has carefully selected several similar problems for you :  5379 5378 5377 5376 5375   can the number of species be negative? Always WA, remove the DET function inside less than 0 o'clock, Ret-=ret over the AC code
problem:4408 (Minimum Spanning Tree) Judge status:acceptedrunid:14484663 language:c++ author:lwj1994 Code render status:rendered by hdoj C + + Code Render Version 0.01 beta#include<stdio.h> #include <string.h> #inc lude<algorithm> #include <iostream> #include <vector> #include <stdlib.h>using namespace std; struct s{int u,v,w;} Edge[5010];int cmp (const void *a,const void *b) {return (* (struct S *) a). W (* (struct S *) b). int N,m;long long Mod;int pre[1010],f[1010],vis[1010];long long g[1010][1010],c[1010][1010];int find (int x,int *pre) {i    F (pre[x]==x) return x; Return Pre[x]=find (Pre[x],pre);}    Vector<int>vt[1010];void init () {int i;        for (i=0;i<=n;i++) {f[i]=i;    vis[i]=0;    Vt[i].clear (); } for (i=0;i<=500;i++) vt[i].clear ();}    Long Long det (long long a[][1010],int n) {int i,j,k;     for (i=0;i<n;i++) {for (j=0;j<n;j++) {a[i][j]%=mod;   }} long long ret=1; for (i=1;i<n;i++) {to (j=i+1;j<n;j++) {while (a[j][i]) {int T                =a[i][i]/a[j][i];                for (k=i;k<n;k++) {a[i][k]= (a[i][k]-a[j][k]*t)%mod;                } for (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;    }//if (ret<0)//Ret=-ret; Return (Ret+mod)%mod;}            int main () {while (scanf ("%d%d%lld", &n,&m,&mod)!=eof) {if (n==0&&m==0&&mod==0)        Break        int i,j,k;        memset (g,0,sizeof (G));        memset (vis,0,sizeof (VIS));        memset (c,0,sizeof (C));        memset (pre,0,sizeof (pre));        memset (F,0,sizeof (f)); for (i=0;i<m;i++) {scanf ("%d%d%d", &AMP;EDGE[I].U,&AMP;EDGE[I].V,&AMP;EDGE[I].W);            } if (m==0) {printf ("0\n");        Continue        } qsort (Edge,m,sizeof (edge[0]), CMP);        Init ();        int w=-1,a,b;        Long Long Ans=1; for (k=0;k<=m;k++) {if (edge[k].w!=w| |                         K==m) {for (i=1;i<=n;i++) {if (Vis[i]) {                        int U=find (I,PRE);                        Vt[u].push_back (i);                    vis[i]=0;                    }} for (i=1;i<=n;i++) {if (Vt[i].size () >1)                    {int len=vt[i].size ();                        memset (c,0,sizeof (C));                        for (a=1;a<=n;a++) for (b=1;b<=n;b++) c[a][b]=0;                       for (a=0;a<len;a++) {     for (b=a+1;b<len;b++) {int a1=vt[i][a];                                int b1=vt[i][b];                                C[B][A]-=G[A1][B1];                                C[a][b]=c[b][a];                                C[A][A]+=G[A1][B1];                            C[B][B]+=G[A1][B1];                }} int Ret=det (C,len);                        printf ("+++++%d\n", ret);                        Ans= (Ans*ret)%mod;                        for (a=0;a<len;a++) {f[vt[i][a]]=i; }}} for (i=1;i<=n;i++) {pre[i]=f[i                    ]=find (I,F);                Vt[i].clear ();                } if (k==m) break;            W=EDGE[K].W;            } int a=edge[k].u; int b=EDGE[K].V;            int Fa=find (A,F);                        int Fb=find (B,F);                if (FA!=FB) {int Fx=find (fa,pre), Fy=find (Fb,pre);                Vis[fa]=vis[fb]=1;                Pre[fx]=fy;                g[fa][fb]++;            g[fb][fa]++;        }}//printf ("%d\n", ans);        int flag=0;            for (i=2;i<=n;i++) {if (flag) break;        if (pre[i]!=pre[i-1]) flag=1;        } if (!flag) printf ("%lld\n", ans%mod);    else printf ("0\n"); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj topic 4408 Minimum Spanning Tree (kruskal+matrix_tree)

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.