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 题目大 ...
随机推荐
- BZOJ 1503 郁闷的出纳员(平衡树)(NOI 2004)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作 ...
- Java学习个人备忘录之构造函数&this
构造函数 概念:构建创造对象时调用的函数. 作用:可以给对象进行初始化,创建对象都必须要通过构造函数初始化. 一个类中如果没有定义过构造函数,那么该类中会有一个默认的空参数构造函数.如果在类中定义了指 ...
- 20145214 《Java程序设计》第6周学习总结
20145214 <Java程序设计>第6周学习总结 教材学习内容总结 串流设计 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 输入串流代表对象为java. ...
- 算法与数据结构3.1 stack
★实验任务 一天,小 L 发现了一台支持一下操作的机器: IN x:将整数 x 入栈 POP:将栈顶元素出栈 ASUB:出栈两个数,将两数差的绝对值入栈 COPY:将栈顶元素(如果有的话)复制一份,入 ...
- iOS- <项目笔记>iOS6 & iOS7屏幕图片适配
1.为非视网膜\视网膜屏幕分别准备2份图片,比如: 1> 非视网膜 abc.png 2> 视网膜 abc@2x.png 程序检测视网膜屏到会自动替换@2x 2.程序启动图片 * 程序启动过 ...
- PokeCats开发者日志(十)
现在是PokeCats游戏开发的第三十三天的中午,收到了中国版权保护中心软件登记部发来的受理通知书. 上易版权看一眼,貌似离拿证不远了. 想一想还有点小激动呢!
- monaco editor 实现自定义提示(sql为例)
monaco editor :https://www.cnblogs.com/XHappyness/p/9414177.html 这里实现自己定义的提示: .vue <template> ...
- shit antd & Merry Christmas bug
shit antd & Merry Christmas bug https://github.com/ant-design/ant-design/issues/13098 antd 玩大了? ...
- Redis Cluster实现原理
一.Redis Cluster主要特性和设计 集群目标 1)高性能和线性扩展,最大可以支撑到1000个节点:Cluster架构中无Proxy层,Master与slave之间使用异步replic ...
- pascal语言中学版整理
P1:主菜单File中的Command shell选项,可以暂时退出Pascal,进入DOS提示符状态,但Pascal仍然驻留在内存中.输入命令exit即可返回Pascal. P3:Edit菜单中Un ...