hdu 4408 Minimum Spanning Tree
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.
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.
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.
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,mod;
int fa[],ka[];
struct node
{
int u,v,w;
}e[];
int a[][];
bool vis[];
vector<int>g[];
long long ans,C[][],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=;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=;
for(int i=;i<h;i++)
{
for(int j=i+;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 ;
}
return (s+mod)%mod;
}
void matrix_tree()
{
int len,u,v;
for(int i=;i<=n;i++)
if(vis[i])
{
g[find(i,ka)].push_back(i);
vis[i]=false;
}
for(int i=;i<=n;i++)
if(g[i].size())
{
memset(C,,sizeof(C));
len=g[i].size();
for(int j=;j<len;j++)
for(int k=j+;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()-)%mod;
for(int j=;j<len;j++) fa[g[i][j]]=i;
}
for(int i=;i<=n;i++)
{
g[i].clear();
ka[i]=find(i,fa);
}
}
void solve()
{
ans=;
int u,v;
memset(a,,sizeof(a));
for(int i=;i<=n;i++) fa[i]=ka[i]=i;
sort(e+,e+m+,cmp);
for(int i=;i<=m+;i++)
{
if(e[i].w!=e[i-].w && i!= || i==m+) 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=;i<n && flag;i++)
if(find(i,fa)!=find(i+,fa)) flag=false;
printf("%lld\n",flag ? ans%mod : );
}
int main()
{
while(init()) solve();
}
’
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/Ot ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 多校 HDU - 6614 AND Minimum Spanning Tree (二进制)
传送门 AND Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- MST(Kruskal’s Minimum Spanning Tree Algorithm)
You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
随机推荐
- Struts2文件上传带进度条,虽然不是很完美
好久没有写东西,最近在做个项目,要用到文件h 传的,以前虽然也做上传,但是总觉得不好用 ,现在和队友合作做了一个带进度条的上传,觉得还行~~和大家分享一下. 首先说一下大概是这样实现的,在我们平时的上 ...
- Sublime Text 插件推荐——for web developers
楼主向高大上的: web front-end development engineer (好吧,google就是这样翻译的 ^_^)们推荐 ST 插件,在此抛砖引玉: NO.1 :Emmet (原名: ...
- python学习笔记06:操作文件
调用内置的open函数打开文件,传递两个参数:文件路径(绝对路径或相对路径),打开模式('r':读,'r+':读写,'w':写,'b':二进制): f = open('data.txt','w') f ...
- (转)《linux性能及调优指南》 3.3 内存瓶颈
翻译:Hank (http://blog.csdn.net/fireroll)版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明.原文名称:<Linux Performance an ...
- 什么是Oracle的分区表 (转 作者 陈字文)
假设我们现在正在酝酿经营一家图书馆,最初,我们只有十本书提供给大家来阅读和购买.对于十本书而言,我们可能只需要一个书架格子将其作为保存这十本书的容器就足够了,因为任何一个人都可以很轻松的扫一眼就可以将 ...
- 用js+css3做一个小球投篮的动画(easing)
<!DOCTYPE html> <html> <head> <script src="jquery-1.11.3.min.js">& ...
- 【转载】用C#编写一个简单的记事本
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- BZOJ 1079 着色方案(DP)
如果把当前格子涂什么颜色当做转移的话,状态则是每个格子的颜色数还剩多少,以及上一步用了什么颜色,这样的状态量显然是5^15.不可取. 如果把当前格子涂颜色数还剩几个的颜色作为转移的话,状态则是每个格子 ...
- 【bzoj1727】[Usaco2006 Open]The Milk Queue 挤奶队列 贪心
题目描述 Every morning, Farmer John's N (1 <= N <= 25,000) cows all line up for milking. In an eff ...
- BZOJ4300 绝世好题(动态规划)
设f[i][j]为前i个数中所选择的最后一个数在第j位上为1时的最长序列长度,转移显然. #include<iostream> #include<cstdio> #includ ...