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. NET基础(4):引用类型和值类型

    CLR支持两种类型:引用类型和值类型.虽然FCL的大多数类型都是引用类型,但程序员用的最多的还是引用类型,引用类型总是从托管堆分配,c#的new操作符返回对象内存地址-即指向对象数据的内存地址.使用引 ...

  2. C语言的概述--学习c的第二天

    以下是整理的知识点: #include <stdio.h>/* 引入stdio.h文件c的标准函数库 */ int main(void)/* 定义一个函数main(),int定义函数返回的 ...

  3. 用Netty开发中间件:高并发性能优化

    用Netty开发中间件:高并发性能优化 最近在写一个后台中间件的原型,主要是做消息的分发和透传.因为要用Java实现,所以网络通信框架的第一选择当然就是Netty了,使用的是Netty 4版本.Net ...

  4. C语言第7次作业

    1 #include<stdio.h> int main() { char name[50];int character[26]={0};int i=0,j;int length=0;wh ...

  5. C#字符串的方法

    static void Main(string[] args) { StrMethod(); } public static void StrMethod() { string myString = ...

  6. 网络请求出错:The resource could not be loaded because the App Transport

    Google后查证,iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (ATS) 新特性要求App内访问的网络必须使用H ...

  7. oracle事物

    要想解释oracle事物的工作流程,首先先解释几个小概念:       1.undo段的组成:段头.回滚块       2.事物ID:每一个事物都有一个自己的事物ID,就像身份证号一样. 在v$tra ...

  8. 在eclipse上跑hadoop的helloworld

    关于hadoop的用处什么我就不说了,在这里记录下在eclipse上第一次跑hadoop遇到的问题吧~ hadoop的安装我就不说啦,网上教程一大堆~我直接用的公司的Linux上的hadoop. ec ...

  9. 时间轴感----Allen Pike

    动画要跑在60fps下.这意味着每一帧需要花费16ms来跑完(1000ms/60=16).这是要达到原生应用般平滑体验的最基本要求.60 fps是所有的iOS的内置动画运行的速度;这就是为什么滚动在i ...

  10. Write Your software base on plugin(C/C++ ABI)

    一个软件,如果把所有的功能写进C++源码,维护,扩展,编译都特别麻烦. 共享库后缀名.Linux -> .so  Windows -> .dll 关于动态符号显示问题,具体可以看系统的AP ...