poj1679 The Unique MST
来源:互联网 发布:大数据行业现状 编辑:程序博客网 时间:2024/05/21 16:21
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’.Input The first line contains a single integer t (1 <= t <= 20), the
number of test cases. Each case represents a graph. It begins with a
line containing two integers n and m (1 <= n <= 100), the number of
nodes and edges. Each of the following m lines contains a triple (xi,
yi, wi), indicating that xi and yi are connected by an edge with
weight = wi. For any two nodes, there is at most one edge connecting
them.Output For each input, if the MST is unique, print the total cost of
it, or otherwise print the string ‘Not Unique!’.
先求最小生成树,然后枚举换掉某一条边,看是否仍和原来的大小相等。
就具体做法就是枚举强制不选每一条边,然后求最小生成树。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct edge{ int f,t,l; bool operator < (const edge & ee) const { return l<ee.l; }}g[10010];int n,m,fa[110],use[10010],ban[10010];bool mark;int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]);} int kru(){ int i,j,k,x,y,z,ans=0,cnt=0; for (i=1;i<=n;i++) fa[i]=i; for (i=1;cnt<n-1;i++) if (!ban[i]) { x=find(g[i].f); y=find(g[i].t); if (x==y) continue; ans+=g[i].l; cnt++; fa[x]=y; if (mark) use[i]=1; } return ans;}int main(){ int i,j,k,x,y,z,T,ans; bool flag; scanf("%d",&T); while (T--) { scanf("%d%d",&n,&m); for (i=1;i<=m;i++) use[i]=ban[i]=0; for (i=1;i<=m;i++) scanf("%d%d%d",&g[i].f,&g[i].t,&g[i].l); sort(g+1,g+m+1); mark=1; ans=kru(); mark=0; flag=0; for (i=1;i<=m;i++) if (use[i]) { ban[i]=1; if (ans==kru()) { flag=1; break; } ban[i]=0; } if (flag) printf("Not Unique!\n"); else printf("%d\n",ans); }}
- POJ1679:The Unique MST
- POJ1679 The Unique MST
- poj1679 - The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- poj1679 The Unique MST
- POJ1679------The Unique MST
- POJ1679-The Unique MST
- poj1679 The Unique MST
- POJ1679 The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- POJ1679-The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- poj1679(The Unique MST)Kruskal
- POJ1679 The Unique MST(kru)
- poj1679——The Unique MST
- [Android&iOS]改变特定文字颜色
- LightOJ 1033 Generating Palindromes
- 文章标题
- Update Nodejs on Ubuntu
- opencv学习笔记(一)安装与配置问题
- poj1679 The Unique MST
- magicleap 资料
- 输入你的邮箱账号,手动向你邮箱发邮件
- shader 基础
- NHibernate Linq查询 扩展增强 (第九篇)
- mybatis生成动态sql的常用标签
- Unity中冒泡排序
- 动手打造Bypass UAC自动化测试小工具,可绕过最新版Win10
- AngularJs 基础教程 —— 控制器