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. 理解Android系统的进程间通信原理(一)----RPC中的代理模式

    Android系统中的进程间通信是通过一个轻量级的RPC(Remote Procedure Call远程进程调用)和AIDL(Android Interface Definination Langua ...

  2. ECharts地图详解 【转】

    $(function() { // 路径配置 require.config({ paths : { // echarts: 'http://echarts.baidu.com/build/dist' ...

  3. 在jQuery环境下制作轻巧遮罩层

    遮罩层的好处就是可以屏蔽用户对遮罩层下方元素的操作. 制作原理很简单:1设置遮罩层触发按钮 2设置遮罩层内容 3设置遮罩层背景(重点是捕获内容div的大小位置)4设置点击触发按钮遮罩层背景内容同时显示 ...

  4. python学习day5--set、函数

    1.set 无序,不重复序列 创建:与dict一样用{},区别在于dict内元素为键值对 se={"123","456,444"} print(type(se) ...

  5. java实现的身份证照片脸部识别(头像截图) 以及OCR字体识别

    断断续续地折腾了大半个月,终于把身份证照片脸部识别以及OCR字体识别功能用Java实现了,需求很简单:通过摄像头所照的一张放在黑色底板上的身份证照,识别照片上身份证里面的人名和地址(OCR中文),再截 ...

  6. Web前端开发:SQL Jsp小项目(一)

    Jsp的学习算是告一段落,针对这段时间的学习,写了一个Jsp小项目来巩固学到的知识. 框架示意图 User list process UserAdd process 需要的界面效果: 需要工具:Ecl ...

  7. NS-Date/NSDateFormatter

    // // main.m // NS-Date // // Created by qianfeng on 15/6/23. // Copyright (c) 2015年 qianfeng. All r ...

  8. (转)软件版本中的Alpha,Beta,RC,Trial是什么意思?

    版本号:V(Version):即版本,通常用数字表示版本号.(如:EVEREST Ultimate v4.20.1188 Beta )Build:用数字或日期标示版本号的一种方式.(如:VeryCD ...

  9. NodeJS服务器退出:完成任务,优雅退出

    上一篇文章,我们通过一个简单的例子,学习了NodeJS中对客户端的请求(request)对象的解析和处理,整个文件共享的功能已经完成.但是,纵观整个过程,还有两个地方明显需要改进: 首先,不能共享完毕 ...

  10. 09_rlCoachKin讲解

    在Socket.cpp中Socket::readClient()函数中就是解析读取到的内容的. 对于我们发送的2 0 1.57 0.31 0 0 1.57 0,那么就会进入如下分支: 也就是进入2号处 ...