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. leetcode 406

    该算法题就是leetcode 406题, 题目描述: Suppose you have a random list of people standing in a queue. Each person ...

  2. 如何创建一个简单的C++同步锁框架(译)

    翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...

  3. 对点餐APP现阶段开发的问题

    团队的成立,基本是一气呵成.但是,github团队的建立却成为 第一个难题,大家对github都不熟,又刚刚好没课时间的任务,大家 已经各有安排,造成时间上的紧急.没有按时.按要求完成github的 ...

  4. 1.<%@Page%>中的Codebehind、AutoEventWireup、Inherits有何作用?

    AutoEventWireup --- 指示是否自动启用页事件. Codebehind --- 指示后台代码文件. Inherits --- 继承类. AutoEventWireup:指示该页的事件是 ...

  5. mvc中测试网络

    .//控制器层面 public string IsAnyNetworkAvailable() { try { foreach (NetworkInterface ni in NetworkInterf ...

  6. ubuntu执行sudo apt-get update提示缺少公钥

    提示信息如下: 获取:1 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease [3,192 B] 命中:2 http:/ ...

  7. AC6102 DDR2测试工程

    AC6102 DDR2测试工程 本文档介绍AC6102上DDR2存储器基于Verilog代码的测试过程.AC6102上使用了2片16bit的DDR2存储器组成了32bit的硬件总线.虽然是32bit硬 ...

  8. ip相关

    查看ip ifconfig -a 查看端口占用 ps -aux | grep tomcat

  9. ASP.net gridview之性别

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  10. js函数的传参是按值传对递

    根据js高级程序设计第三版编写,对于函数的参数只能按值传递是这样解释的: ECMAScript 中所有函数的参数都是按值传递的.也就是说,把函数外部的值复制给函数内部的参数,就和把值从一个变量复制到另 ...