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 题目大 ...
随机推荐
- NSValue的valueWithBytes:objCType:方法
+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type; NSValue的valueWithBytes: ...
- Thunder团队第五周 - Scrum会议6
Scrum会议6 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 宋雨同学在拍照,所以不在照片内. 参会成员: 王航:http://www.cnblogs ...
- 20145214《Java程序设计》课程总结
20145214<Java程序设计>课程总结 每周读书笔记链接汇总 第一周读书笔记 第二周读书笔记 第三周读书笔记 第四周读书笔记 第五周读书笔记 第六周读书笔记 第七周读书笔记 第八周读 ...
- Spring Security 快速了解
在Spring Security之前 我曾经使用 Interceptor 实现了一个简单网站Demo的登录拦截和Session处理工作,虽然能够实现相应的功能,但是无疑Spring Security提 ...
- Median of Two Sorted Arrays(hard)
题目要求: 有两个排序的数组nums1和nums2分别为m和n大小. 找到两个排序数组的中位数.整体运行时间复杂度应为O(log(m + n)). 示例: 我的方法: 分别逐个读取两个数组的数,放到一 ...
- 项目UML设计--日不落战队
[团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...
- JS 书籍拓展内容
一.面向对象
- ipython matplotlib
matplotlib实际上是一套面向对象的绘图库,它所绘制的图表中的每个绘图元素,例如线条Line2D.文字Text.刻度等在内存中都有一个对象与之对应.为了方便快速绘图matplotlib通过pyp ...
- java基础简介
一.软件开发 软件:是由数据和指令组成的(例:计算器) 如何实现软件开发呢? 就是使用开发工具和计算机语言做出东西来 二.常用dos命令 d: 回车 盘符切换 dir(directory):列出 ...
- fzu1686-神龙的难题
给出一个n\times m的01矩阵,以及\(h,w\),表示一次可以把矩阵的一个\(h\times w\)的小矩阵变为全0,问至少要多少次可以把整个矩阵变为全0.\(n,m\le 15\). 分析 ...