POJ 1679 The Unique MST(判断最小生成树_Kruskal)
来源:互联网 发布:陕西大数据集团前景 编辑:程序博客网 时间:2024/06/06 14:02
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!
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int father[111],n,m,first;struct node{int u,v,w;int used;int equal;int del;} a[11111];bool cmp(node x,node y){if(x.w<y.w) return true;return false;}int find(int x){int r=x;while(father[r]!=r) r=father[r];int i=x,j;while(i!=r) {j=father[i];father[i]=r;i=j;}return r;}int prime(){int i,j,k,sum,num;sum=0;num=0;for(i=1;i<=n;i++) father[i]=i;for(i=1;i<=m;i++) {if(a[i].del) continue;int fx=find(a[i].u);int fy=find(a[i].v);if(fx!=fy) {num++;father[fx]=fy;sum+=a[i].w;if(first) a[i].used=1;}if(num==n-1) break;}return sum;}int main(){int i,j,k,u,v,w,sum1,sum2;int t;scanf("%d",&t);while(t--) {sum1=sum2=0;memset(a,0,sizeof(a));scanf("%d%d",&n,&m);for(i=1;i<=m;i++) {scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);}for(i=1;i<=m;i++) {for(j=i+1;j<=m;j++) {if(a[i].w==a[j].w) a[i].equal=1;}}sort(a+1,a+1+m,cmp);first=1;sum1=prime();first=0;for(i=1;i<=m;i++) {if(a[i].used && a[i].equal) {a[i].del=1;sum2=prime();if(sum1==sum2) {printf("Not Unique!\n");break;}}}if(i==m+1) printf("%d\n",sum1);}}
0 0
- POJ 1679 The Unique MST(判断最小生成树_Kruskal)
- poj 1679 The Unique MST (最小生成树)
- POJ:1679 The Unique MST(判断最小生成树是否唯一)
- POJ 1679 The Unique MST (判断最小生成树是否唯一)
- poj 1679 The Unique MST (判断最小生成树是否唯一)
- POJ 题目1679The 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(判断最小生成树是否唯一)
- [Android]shape和selector的结合使用
- python setup.py install如何卸载
- [转载]MATLAB中FFT的使用方法
- 解析 json 数组
- 划分树的学习(求区间第k大的数字)&&分块求区间第k大
- POJ 1679 The Unique MST(判断最小生成树_Kruskal)
- java web前端学习网站
- Elasticsearch 索引优化
- MDK如何生成bin文件?
- Lowest Common Ancestor of a Binary Tree
- Spring学习笔记(七):IOC控制反转
- CentOS 7安装Nginx
- 【对象序列化】pickle.dumps()--python对象序列化/反序列化
- POJ 3225 Help with Intervals (线段树 成段更新, 懒惰标记*2)