http://poj.org/problem?id=1679

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17726   Accepted: 6150

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

 
【题解】:
  用kruskal,求出最小生成树,然后枚举删除最小生成树上的一条边,再求最小生成树,比较前后两次求解的大小
  【注意】:如果题目给的数据不能构成一个树需要输出0
 
【code】:
 

 /**
Judge Status:Accepted Memory:748K
Time:32MS Language:G++
Code Lenght:1814B Author:cj
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector> #define N 101
#define M 6000
using namespace std; struct Nod
{
int x,y,w;
}node[M]; int parent[N];
int n,m;
vector<int> vct; bool cmp(Nod a,Nod b)
{
return a.w<b.w;
} int findp(int a)
{
while(a!=parent[a])
{
a=parent[a];
}
return a;
} int merge(Nod nd) //合并
{
int x = findp(nd.x);
int y = findp(nd.y);
if(x>y)
{
parent[y]=x;
return nd.w;
}
else if(x<y)
{
parent[x]=y;
return nd.w;
}
return -;
} int kruskal(int id)
{
int i,sum=,cnt=;
for(i=;i<=n;i++) parent[i]=i;
for(i=;i<m;i++)
{
if(i!=id)
{
int temp = merge(node[i]);
if(temp!=-)
{
sum+=temp;
cnt++; //剪枝
if(id==-) vct.push_back(i); //保存第一次最小生成树的各个节点
}
if(cnt>=n-) //找到n-1条边即可以跳出了
break;
}
}
cnt = ;
for(i=;i<=n;i++) //判断是不是构成一棵树
if(parent[i]==i)
cnt++;
if(cnt==) //是
return sum;
if(id==-) //否
return ;
return -;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int i;
for(i=;i<m;i++)
{
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].w);
}
vct.clear();
sort(node,node+m,cmp);
int mins = kruskal(-); //找到第一颗最小生成树
int temp=-;
for(i=;i<vct.size();i++)
{
temp = kruskal(vct[i]); //每次去掉一个节点 再判断是否可以组成最小生成树
if(mins==temp)
break;
}
if(temp==mins) puts("Not Unique!");
else printf("%d\n",mins);
}
return ;
}

poj 1679 The Unique MST(唯一的最小生成树)的更多相关文章

  1. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

  2. POJ 1679 The Unique MST 【判断最小生成树是否唯一】

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique.  Defini ...

  3. POJ 1679 The Unique MST(推断最小生成树_Kruskal)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique.  Defini ...

  4. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  5. poj 1679 The Unique MST (判定最小生成树是否唯一)

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

  6. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

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

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  8. POJ 1679 The Unique MST 推断最小生成树是否唯一

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

  9. POJ 1679 The Unique MST (最小生成树)

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

随机推荐

  1. 《JavaScript模式》读书笔记

    简介 在软件开发过程中,模式是指一个通用问题的解决方案.一个模式不仅仅是一个可以用来复制粘贴的代码解决方案,更多地是提供了一个更好的实践经验.有用的抽象化表示和解决一类问题的模板. 对象有两大类: 本 ...

  2. live(),bind(),delegate()等事件绑定方法的区别及应用解析

    1 首先bind()方法是最直观的,但是也是弊端最大的. $('a').bind('click',function(){alert('that tickles!')}) 这和事件冒泡有直接关系,当我们 ...

  3. 网易新闻RSS阅读器

    首先需要分析网易RSS订阅中心的网页布局情况. 网易RSS订阅中心:http://www.163.com/rss/ 你会发现RSS文件由一个<channel>元素及其子元素组成,除了频道本 ...

  4. 北大ACM(POJ1001-Exponentiation)

    Question:http://poj.org/problem?id=1001问题点:大数运算(求幂) #include <iostream> using namespace std; # ...

  5. 补充:sql server 中的相关查询、case函数

    相关查询(在同一个表中) 相关查询类似子查询,但是又不同于子查询:子查询中的子条件可以单独查出数据,但是相关查询的子条件不能查处数据.(可以理解成C#中for的穷举法,第一个for走一个,第二个for ...

  6. 终端命令收集(关于 mac与ubuntu)

    本人曾使用ubuntu 是踩过有一些坑,以及在处理问题时学到的知识,总结一下,便于以后记忆. 1 基本命令 (1)列出文件 ls 参数 目录名 参数 -w 显示中文,-l 详细信息, -a 包括隐藏文 ...

  7. 8个应该去逛逛JQuery的学习网站

    根据国外科技网站 W3Techs 一项调查了近100万个网站数据显示,jQuery是目前最流行的 JavaScript 库.对于初学者来说,有的时候很难找到一个好的学习jQuery的网站,所以本文收集 ...

  8. The C in C++

    1 unnamed arguments in the argument list of the function definition (Page 114) In c++, an argument m ...

  9. 在Apache中开启虚拟主机

    最近在自学LAMP,在Apache中尝试着开启虚拟主机的时候,遇到了挺多麻烦的,这里也顺便总结一下,在Apache中开启虚拟主机的时候,主要有下面几个步骤: 1.新建一个文件夹作为虚拟主机,用来存储网 ...

  10. PHP 反射 ReflectionClass

    今天遇到了这样一个问题,如下代码: classA.php <?php class ClassA{ public function funcAa(){ } public function func ...