Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
XXX 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.
Input
There 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.
Output
For 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 above mentioned, appears in the first line of each test case.
/* *********************************************** MYID : Chen Fan LANG : G++ PROG : Counting_MST_HDU4408 ************************************************ */
#include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<bitset> #define N 405 #define M 4005
usingnamespace std;
typedefstructnod { int a,b,c; } node; node edge[M];
boolop(node a,node b) { return a.c<b.c; }
int n,m,o,fa[N],ka[N]; longlong ans,mod,gk[N][N],kir[N][N];
bitset<N> flag; vector<int> gra[N];
intgetfather(int x,int father[]) { if (father[x]!=x) father[x]=getfather(father[x],father); return father[x]; }
longlongdet(longlong a[][N],int n)//Matrix-Tree定理求Kirchhoff矩阵 { for (int i=0;i<n;i++) for (int j=0;j<n;j++) a[i][j]%=mod; longlong ret=1; for (int i=1;i<n;i++) { for (int j=i+1;j<n;j++) while (a[j][i]) { longlong 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) return0; ret=ret*a[i][i]%mod; //ret%=mod; } return (ret+mod)%mod; }
voidmatrix_tree() { for (int i=1;i<=n;i++) //根据访问标记找出连通分量 if (flag[i]) { gra[getfather(i,ka)].push_back(i); flag[i]=0; } for (int i=1;i<=n;i++) if (gra[i].size()>1) //枚举连通分量 { memset(kir,0,sizeof(kir)); 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]; kir[b][a]-=gk[la][lb]; kir[a][b]=kir[b][a]; kir[a][a]+=gk[la][lb]; kir[b][b]+=gk[la][lb]; } //构造矩阵