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.
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.
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.
Sample Input
5 10 12
2 5 3
2 4 2
3 1 3
3 4 2
1 2 3
5 4 3
5 1 3
4 1 1
5 3 3
3 2 3
0 0 0
Sample Output
4
Source
题意:求最小生成树的数量
矩阵树定理
回想 Kruskal算法,从小到大枚举边
当边权相等时,边随意排序
那么当权值为k的边全部枚举完后,点的联通情况F是固定的
也就是说权值都为k的边无论以什么顺序枚举,得到的生成树的形态不一,但联通情况F'和F相同
如果将加入最小生成树的 权值为1——k-1的边 和 对应的点 压缩成一个点,
所有权值为k 的边 的联通情况 是独立的
根据乘法原理
我们可以枚举权值k
计算此时形成F的方案数
具体来说就是 枚举权值为k的边形成的连通块,用矩阵树定理算出每种连通块的方案数
累乘就是权值为k的边形成F的方案数
最后将所有权值为k的边的答案累乘
注意 并查集不要路径压缩
因为矩阵树定理需要每个点的度数
‘
’
#include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,mod; int fa[101],ka[101]; struct node { int u,v,w; }e[1001]; int a[101][101]; bool vis[101]; vector<int>g[101]; long long ans,C[101][101],t; bool cmp(node p,node q) { return p.w<q.w; } int find(int i,int *f) { return f[i]==i ? i : find(f[i],f); } bool init() { int u,v; scanf("%d%d%d",&n,&m,&mod); if(!n) return false; for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); return true; } long long det(int h) { long long s=1; for(int i=0;i<h;i++) { for(int j=i+1;j<h;j++) while(C[j][i]) { t=C[i][i]/C[j][i]; for(int k=i;k<h;k++) C[i][k]=(C[i][k]-C[j][k]*t+mod)%mod; for(int k=i;k<h;k++) swap(C[i][k],C[j][k]); s=-s; } s=s*C[i][i]%mod; if(!s) return 0; } return (s+mod)%mod; } void matrix_tree() { int len,u,v; for(int i=1;i<=n;i++) if(vis[i]) { g[find(i,ka)].push_back(i); vis[i]=false; } for(int i=1;i<=n;i++) if(g[i].size()) { memset(C,0,sizeof(C)); len=g[i].size(); for(int j=0;j<len;j++) for(int k=j+1;k<len;k++) { u=g[i][j]; v=g[i][k]; if(a[u][v]) { C[k][j]=(C[j][k]-=a[u][v]); C[k][k]+=a[u][v]; C[j][j]+=a[u][v]; } } ans=ans*det(g[i].size()-1)%mod; for(int j=0;j<len;j++) fa[g[i][j]]=i; } for(int i=1;i<=n;i++) { g[i].clear(); ka[i]=find(i,fa); } } void solve() { ans=1; int u,v; memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) fa[i]=ka[i]=i; sort(e+1,e+m+1,cmp); for(int i=1;i<=m+1;i++) { if(e[i].w!=e[i-1].w && i!=1 || i==m+1) matrix_tree(); u=find(e[i].u,fa); v=find(e[i].v,fa); if(u!=v) { vis[u]=vis[v]=true; ka[find(u,ka)]=find(v,ka); a[u][v]++; a[v][u]++; } } bool flag=true; for(int i=1;i<n && flag;i++) if(find(i,fa)!=find(i+1,fa)) flag=false; printf("%lld ",flag ? ans%mod : 0); } int main() { while(init()) solve(); }