【HDU 4408】Minimum Spanning Tree(最小生成树计数)

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.

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

2012 ACM/ICPC Asia Regional Jinhua Online

Recommend

zhoujiaqi2010   |   We have carefully selected several similar problems for you:  5831 5830 5829 5828 5827

题意是给定n个点,m条边的无向图,求最小生成树的个数对p取模。

用kruscal计算最小生成树时,每次取连接了两个不同联通块的最小的边。也就是先处理d1条c1长度的边,再处理d2条c2长度的边。长度相同的边无论怎么选,最大联通情况都是固定的。
分别对ci长度的边产生的几个联通块计算生成树数量再乘起来,然后把这些联通块缩点,再计算ci+1长度的边。

生成树计数用Matrix-Tree定理,上一篇是无重边的,这题的缩点后是会产生重边的,Matrix-tree也适用:

Kirchhoff矩阵任意n-1阶子矩阵的行列式的绝对值就是无向图的生成树的数量。

Kirchhoff矩阵的定义是度数矩阵-邻接矩阵。

1、G的度数矩阵D[G]:n*n的矩阵,Dii等于Vi的度数,其余为0。
2、G的邻接矩阵A[G]:n*n的矩阵, Vi、Vj之间有边直接相连,则 Aij=ij之间的边数,否则为0。

并查集fa[i]是当前长度之前,节点所属的联通块,ka[i]是当前长度的边连接后它在的联通块。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int N=101;
const int M=1001;
ll n,m,p,ans;
vector<int>gra[N];
struct edge{
	int u,v,w;
}e[M];
int cmp(edge a,edge b){
	return a.w<b.w;
}
ll mat[N][N],g[N][N];
ll fa[N],ka[N],vis[N];
ll det(ll c[][N],ll n){
	ll i,j,k,t,ret=1;
	for(i=0;i<n;i++)
	for(j=0;j<n;j++) c[i][j]%=p;
	for(i=0; i<n; i++){
		for(j=i+1; j<n; j++)
			while(c[j][i]){
				t=c[i][i]/c[j][i];
				for(k=i; k<n; k++)
					c[i][k]=(c[i][k]-c[j][k]*t)%p;
				swap(c[i],c[j]);
				ret=-ret;
			}
		if(c[i][i]==0)
			return 0L;
		ret=ret*c[i][i]%p;
	}
	return (ret+p)%p;
}
ll find(ll a,ll f[]){
	return f[a]==a?a:find(f[a],f);
}
void matrix_tree(){//对当前长度的边连接的每个联通块计算生成树个数
	for(int i=0;i<n;i++)if(vis[i]){//当前长度的边连接了i节点
		gra[find(i,ka)].push_back(i);//将i节点压入所属的联通块
		vis[i]=0;//一边清空vis数组
	}
	for(int i=0;i<n;i++)
	if(gra[i].size()>1){//联通块的点数为1时生成树数量是1
		memset(mat,0,sizeof mat);//清空矩阵
		int len=gra[i].size();
		for(int j=0;j<len;j++)
		for(int k=j+1;k<len;k++){//构造这个联通块的矩阵(有重边)
			int u=gra[i][j],v=gra[i][k];
			if(g[u][v]){
				mat[k][j]=(mat[j][k]-=g[u][v]);
				mat[k][k]+=g[u][v];mat[j][j]+=g[u][v];
			}
		}
		ans=ans*det(mat,gra[i].size()-1)%p;
		for(int j=0;j<len;j++)fa[gra[i][j]]=i;//缩点
	}
	for(int i=0;i<n;i++)
	{
		gra[i].clear();
		ka[i]=fa[i]=find(i,fa);
	}
}
int main(){
	while(scanf("%lld%lld%lld",&n,&m,&p),n){
		for(int i=0;i<m;i++){
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			u--;v--;
			e[i]=(edge){u,v,w};
		}
		sort(e,e+m,cmp);
		memset(g,0,sizeof g);
		ans=1;
		for(ll i=0;i<n;i++)ka[i]=fa[i]=i;
		for(ll i=0;i<=m;i++){//边从小到大加入
			if(i&&e[i].w!=e[i-1].w||i==m)//处理完长度为e[i-1].w的所有边
				matrix_tree();//计算生成树
			ll u=find(e[i].u,fa),v=find(e[i].v,fa);//连的两个缩点后的点
			if(u!=v)//如果不是一个
			{
				vis[v]=vis[u]=1;
				ka[find(u,ka)]=find(v,ka);//两个分量在一个联通块里。
				g[u][v]++,g[v][u]++;//邻接矩阵
			}
		}
		int flag=1;
		for(int i=1;i<n;i++)if(fa[i]!=fa[i-1])flag=0;
		printf("%lld\n",flag?ans%p:0);//注意p可能为1,这样m=0时如果ans不%p就会输出1
	}
}

  

时间: 08-12

【HDU 4408】Minimum Spanning Tree(最小生成树计数)的相关文章

HDU 4408 Minimum Spanning Tree 最小生成树计数

Minimum Spanning Tree 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

HDU 4408 Minimum Spanning Tree 最小生成树计数裸题

题意: 给定n个点m条无向边 答案取模 MOD 问: 有多少个最小生成树 DET模版: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> template <class

HDU 4408 - Minimum Spanning Tree

题解: Kruskal 算法的基本思想是,按照边长排序,然后不断将短边加入集合,最终一步如果能成功把 n-1 条边都加入同一个集合,则找到了最小生成树.在维护集合时,可以使用并查集来快速处理. 如果把 Kruskal 的过程按照边长划分成多个阶段,实际上是处理了所有短边的连通性之后继续处理下一个长度的边的连通性,并依次继续处理剩下边的连通性. 然后我们可以发现,不同长度的边之间的连通性互不影响. 假设存在 n1 条长度为 c1 的边,n2 条长度为 c2 的边… 则 Kruskal 首先处理 c

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解

喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情况下,我们把图看成是一种由"顶点"和"边"组成的抽象网络.在各个"顶点"间可以由"边"连接起来,使两个顶点间相互关联起来.图的结构可以描述多种复杂的数据对象,

Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树

寻找图中最小连通的路径,图如下: 算法步骤: 1. Sort all the edges in non-decreasing order of their weight. 2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it. 3. Repeat st

HDU 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在

CF609E. Minimum spanning tree for each edge

题解:随便构造一颗最小生成树 然后对于其他不在树上的边  考虑到 删除这条链上的最大值在把这条边加上去 能得到这条边所在的最小生成树 可以LCT维护 但是明显这个题是静态的树就没必要LCT 当然我觉得最优的是树剖以后ST nlogn的的复杂度 也可以树剖+线段树nlog^2的复杂度 #include <bits/stdc++.h> const int MAXN=2e5+10; #define ll long long using namespace std; ll read(){ ll x=0

[思维]Minimum Spanning Tree

题目描述 In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph G is another simple undirected weighted graph L(G) that represents the adjacency between every two edges in G. Precisely speaking, for an undire