poj 1679 The Unique MST
来源:互联网 发布:免费洗车的软件 编辑:程序博客网 时间:2024/05/24 07:38
点击打开链接
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 31493 Accepted: 11369
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'.
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!'.
Sample Input
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not Unique!判断是否只有一个最小生成树,如果只有一个,输出最小权值,如果不是,输出Not Unique 。
这是我第一次做这种题,看了一个博客,好不容易想明白的,先找出最小生成树,接下来遍历一遍,当去掉一条线,判断其他的能不能再组成权值相等的最小生成树,如果能,那就说明这不是唯一。
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;struct node{ int u,v,w; int eq,used,del;} edg[10010];int n,m,cnt,fa[10010];bool flag;bool comp(node x,node y){ return x.w<y.w;}void init()//初始化{ for(int i=0; i<=10000; i++) fa[i]=i;}int find(int x)//查找{ return x==fa[x]?x:fa[x]=find(fa[x]);}void Union(int x,int y)//并查集{ x=find(x); y=find(y); fa[y]=x;}int Kruskal()//最小生成树{ init(); int sum=0,num=0; for(int i=0; i<m; i++) { if(edg[i].del==1) continue; int u=edg[i].u,v=edg[i].v,w=edg[i].w; if(find(u)!=find(v)) { if(!flag) edg[i].used=1; Union(u,v); num++; sum+=w; } if(num>=n-1) break; } return sum;}int main(){ int t; cin>>t; while(t--) { cnt=0; cin>>n>>m; for(int i=0; i<m; i++) { cin>>edg[i].u>>edg[i].v>>edg[i].w; edg[i].eq=0; edg[i].used=0; edg[i].del=0; } for(int i=0; i<m; i++) //找出相同权值的点 { for(int j=0; j<m; j++) { if(i==j) continue; if(edg[i].w==edg[j].w) edg[i].eq=1; } } flag=false; sort(edg,edg+m,comp); cnt=Kruskal(); flag=true; bool gg=false; for(int i=0; i<m; i++) //遍历判断 { if(edg[i].used==1&&edg[i].eq==1) { edg[i].del=1; int s=Kruskal(); if(s==cnt) { gg=true; printf("Not Unique!\n"); break; } } edg[i].del=0; } if(!gg) printf("%d\n",cnt); } return 0;}
阅读全文
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
- 剑指offer-18:判断树的子结构
- logback配置
- 直接插入排序(Insertion Sort)
- 从collection中取op
- JS 字符串 常用操作
- poj 1679 The Unique MST
- 【cocos2d-x 源码剖析】启动窗口
- 【C++】G++命令简介
- eclispe 设置自动提示
- MySQL 触发器
- logback属性
- 网卡混杂模式Promiscuous与linux上混杂模式的设置
- 艾灸神阙穴的神奇疗效
- 鼠标滑轮成了页面缩放的解决方法