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 T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x <0) {
		putchar('-');
		x = -x;
	}
	if (x>9) pt(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
typedef long long ll;
const int N = 105;    //点的个数
const int M = 1005;  //边的个数
//点标从1-n
struct node {
	int set[N];
	void init(int n) {
		for (int i = 0; i <= n; i++) set[i] = i;
	}
	int find(int x) {
		return x == set[x] ? x : set[x] = find(set[x]);
	}
	int Union(int x, int y) {
		int xx = find(x);
		int yy = find(y);
		if (xx == yy) return -1;
		set[xx] = yy;
		return 1;
	}
}a, b, c;

struct Node {
	int u, v, dis;
}edge[M];
int edgenum;
void add(int u, int v, int d){
	Node E = { u, v, d };
	edge[++edgenum] = E;
}

bool visit[N];
vector<int> g[N];
ll p[N][N], deg[N][N];
int cmp(Node a, Node b) {
	return a.dis < b.dis;
}
ll DET(ll a[][N], int n, ll MOD)
{
	int i, j, k;
	ll temp = 1, t;
	for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[i][j] %= MOD;
	for (i = 1; i < n; i++)
	{
		for (j = i + 1; j < n; j++) while (a[j][i])
		{
			t = a[i][i] / a[j][i];
			for (k = i; k < n; k++)
			{
				a[i][k] -= a[j][k] * t;
				a[i][k] %= MOD;
			}
			for (k = i; k < n; k++)
				swap(a[i][k], a[j][k]);

			temp = -temp;
		}
		temp = temp*a[i][i] % MOD;
	}
	return (temp + MOD) % MOD;
}

ll cal_MST_count(int n, ll MOD) {
	sort(edge + 1, edge + edgenum + 1, cmp);
	int pre = edge[1].dis;
	ll ans = 1;
	a.init(n);
	b.init(n);
	memset(visit, 0, sizeof(visit));
	memset(deg, 0, sizeof(deg));
	for (int i = 0; i <= n; i++) g[i].clear();
	for (int t = 1; t <= edgenum + 1; t++)
	{
		if (edge[t].dis != pre || t == edgenum + 1)
		{
			for (int i = 1, k; i <= n; i++) if (visit[i])
			{
				k = b.find(i);
				g[k].push_back(i);
				visit[i] = 0;
			}
			for (int i = 1; i <= n; i++)
			if (g[i].size())
			{
				memset(p, 0, sizeof(p));
				for (int j = 0; j < g[i].size(); j++)
				for (int k = j + 1, x, y; k < g[i].size(); k++)
				{
					x = g[i][j];
					y = g[i][k];
					p[j][k] = p[k][j] = -deg[x][y];
					p[j][j] += deg[x][y];
					p[k][k] += deg[x][y];
				}
				ans = ans*DET(p, g[i].size(), MOD) % MOD;
				for (int j = 0; j < g[i].size(); j++) a.set[g[i][j]] = i;
			}
			memset(deg, 0, sizeof(deg));
			for (int i = 1; i <= n; i++)
			{
				b.set[i] = a.find(i);
				g[i].clear();
			}
			if (t == edgenum + 1) break;
			pre = edge[t].dis;
		}
		int x = a.find(edge[t].u);
		int y = a.find(edge[t].v);
		if (x == y) continue;
		visit[x] = visit[y] = 1;
		b.Union(x, y);
		deg[x][y]++;
		deg[y][x]++;
	}
	if (!edgenum) return 0;
	for (int i = 2; i <= n; i++)
	if (b.find(i) != b.find(1))
		return 0;
	return ans;
}
void init(){ edgenum = 0; }
int n, m, u, v, d;
ll mod;
int main(){
	while (cin >> n >> m >> mod, n + m + mod){
		init();
		while (m--){
			rd(u); rd(v); rd(d);
			add(u, v, d);
		}
		pt(cal_MST_count(n, mod)); putchar('\n');
	}
	return 0;
}
时间: 2024-10-04 19:43:26

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

题解: 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)? 这里的图当然不是我们日常说的图片或者地图.通常情况下,我们把图看成是一种由"顶点"和"边"组成的抽象网络.在各个"顶点"间可以由"边"连接起来,使两个顶点间相互关联起来.图的结构可以描述多种复杂的数据对象,

【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) vertex

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