The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28207   Accepted: 10073

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!'.

Sample Input

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

Source

 
求一个图是否有不同的最小生成树
大家都太暴力,枚举去那条边,暴力一遍判断重复
但其实可以更快
可以参考这篇论文(注意,文章中代码我认为有错,请自行思考)
http://www.docin.com/p-806495282.html
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
typedef struct{
int to,frm,dis;
}edge;
edge gra[];
int num=,fa[];
int n,m;
int cmp(const edge &a,const edge &b){
return a.dis<b.dis;
}
int fnd(int x){
return x==fa[x]?x:fnd(fa[x]);
}
int uni(int x,int y){
int fx=fnd(x);
int fy=fnd(y);
fa[fy]=fx;
return ;
}
inline int read(){
int sum=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch<=''&&ch>=''){
sum=sum*+ch-'';
ch=getchar();
}
return sum;
}
int kru(){
int ans=;
sort(gra+,gra+m+,cmp);
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<=m;i++){
int x=gra[i].frm;
int y=gra[i].to;
int fx=fnd(x);
int fy=fnd(y);
if(fx!=fy){
int j=i+;
while(j<=m&&gra[j].dis==gra[i].dis){
int y1=gra[j].frm;
int x1=gra[j].to;
int fy1=fnd(y1);
int fx1=fnd(x1);
if((fx1==fx&&fy1==fy)||(fx1==fy&&fy1==fx))return -;
j++;
}
ans+=gra[i].dis;
uni(fx,fy);
}
}
return ans;
}
int main(){
int t;
t=read();
while(t--){
memset(gra,,sizeof(gra));
n=read(),m=read();
num=;
for(int i=;i<=m;i++){
gra[i].frm=read();
gra[i].to=read();
gra[i].dis=read();
} int ans=kru();
if(ans==-)printf("Not Unique!\n");
else printf("%d\n",ans);
}
return ;
}

[poj1679]The Unique MST(最小生成树)的更多相关文章

  1. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27141   Accepted: 9712 D ...

  2. POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)

    http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...

  3. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  4. POJ1679 The Unique MST 【次小生成树】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20421   Accepted: 7183 D ...

  5. POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29902   Accepted: 10697 ...

  6. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  7. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

  8. poj1679 The Unique MST(判定次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23180   Accepted: 8235 D ...

  9. POJ-1679.The Unique MST.(Prim求次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39561   Accepted: 14444 ...

随机推荐

  1. Design Tic-Tac Toe

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

  2. uva 147 Dollars

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. Linux 发行版本及其基于

    Independent ---> Debian Debian/Ubuntu(LTS) ---> Linux Mint ---> Linux Lite Debian/Ubuntu -- ...

  4. pod install 无限卡顿

    pod install  被墙了,请大家换成pod install --verbose --no-repo-update

  5. (一)GPIO 编程实验 LED 流水灯控制

    7个寄存器 是R1-R16.(当然,里面有很多是分几个模式的,所以总共有37个)类似于单片机的R0-R7. GPXCON,GPXDAT等等是另外的寄存器,应该叫,特殊功能寄存器,类似于单片机的P0,P ...

  6. JavaSE基础第四篇

    1.参数传递   2,方法的重载 方法的参数的个数.类型.顺序 跟修饰符.返回值无关   3.构造方法: return 表示当前方法执行结束,后面不能写任何语句   4工程导入 单个.java文件粘贴 ...

  7. Lunix 命令

    awk '{a[$1]+=1;if(a[$1]==1){print $0}}' awk -F ','  '{print $1, $6}'  IS.csv | sort -k1n -k2n | awk ...

  8. iOS 推送小记

    ios做推送功能时,最烦得就是各种证书的问题,以前自己做的时候经常要反复搞那些证书搞好几遍才能成功,现在发现归根到底都是appid这个东西搞错了,做个笔记记下来,以免忘了. 首先是程序里面注册推送的变 ...

  9. Android 控件属性介绍

    1.LinearLayout(线性布局): 可以分为水平线性:android:orientation= " horizontal " 和垂直线性:android:orientati ...

  10. JS数组操作示意图(shift,unshift,pop,push)

    shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b: ...