POJ 1679 The Unique MST
来源:互联网 发布:一战沙俄 知乎 编辑:程序博客网 时间:2024/06/06 16:56
Description
Given a connected undirected graph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V’, E’), with the following properties:
1. V’ = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E’) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E’.
【题目分析】
关于最小生成树是否唯一的问题。只需要计算一下删除任意一条原有的生成树的边,然后再跑一边最小生成树,然后比较一下是否相等就可以了。
【代码】
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct edge{ int a,b,w;}c[20000];int f[110],list[110];inline int gf(int k){ if (f[k]==k) return k; else return f[k]=gf(f[k]);}inline bool cmp(edge a,edge b){return a.w<b.w;}inline void un(int a,int b){ int fa=gf(a),fb=gf(b); if (fa==fb) return; else f[fa]=fb; return;}inline void kru(){ int n,m,ans=0,ans2=0,k2,k=0; scanf("%d%d",&n,&m); memset(c,0,sizeof c); memset(list,0,sizeof list); for (int i=1;i<=m;++i) scanf("%d%d%d",&c[i].a,&c[i].b,&c[i].w); for (int i=1;i<=n;++i) f[i]=i; sort(c+1,c+m+1,cmp); for (int i=1;i<=m&&k<n-1;++i) { int f1=gf(c[i].a),f2=gf(c[i].b); if (f1!=f2) { un(f1,f2); ++k; ans+=c[i].w; list[k]=i; } } for (int i=1;i<n;++i) { ans2=0,k2=0; for (int j=1;j<=n;++j) f[j]=j; for (int j=1;j<=m;++j) { if (j==list[i]) continue; int f1=gf(c[j].a),f2=gf(c[j].b); if (f1!=f2) { un(f1,f2); k2++; ans2+=c[j].w; } } if (k2!=n-1) continue; if (ans==ans2){ printf("Not Unique!\n"); return ; } } printf("%d\n",ans);}int main(){ int tt; scanf("%d",&tt); for (int z=1;z<=tt;++z) { kru(); }}
0 0
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679 The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- Poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679-The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- poj 1679 The Unique MST
- poj 1679-The Unique MST
- iOS中使用blend改变图片颜色
- 青年图摘0714!唯美背后
- Map输出方式二:foreach输出Map
- MySQL存储过程中的3种循环
- Matlab和C++混合编程
- POJ 1679 The Unique MST
- DataTable跳转到指定页
- 帝国cms会员查看权限设置方法
- 设计模式--原型模式
- 如何启动、关闭和设置ubuntu防火墙
- 【牛腩新闻发布系统】-HTTP错误 403.14 –Forbidden Web服务器被配置为不列出此目录的内容
- EXT4 form 表单内控件输入无效时不出现提示的原因
- scp命令的用法详解
- 语音提示