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

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


————————————————————————————————————
题目意思是判最小生成树是否唯一,唯一输出最小生成树,否则输出提示语
思路:找出次小生成树,判断和最小生成树是否一样。找次小生成树的时候可以枚举去掉最小生成树上的每一条边,在在剩下的边找最小生成树。

#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstring>
using namespace std;
#define LL long long struct node
{
int u,v,w,flag;
} p[100005]; int n,m,cnt,tot,pre[1005],f[100005]; bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<1004; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} int kruskal(int xx)
{ init();
int cost=0;
int ans=0;
int flg=0;
for(int i=0; i<cnt; i++)
{
if(f[i])
continue;
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
if(xx==1)
p[i].flag=1;
}
}
if(ans==n-1)
return cost;
else
return -1;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(p,0,sizeof p);
memset(f,0,sizeof f);
scanf("%d%d",&n,&cnt);
for(int i=0; i<cnt; i++)
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
sort(p,p+cnt,cmp);
int mintree=kruskal(1);
int sectree=999999999;
for(int i=0; i<cnt; i++)
{
if(p[i].flag==1)
{
f[i]=1;
int ans=kruskal(0);
if(ans!=-1)
sectree=min(sectree,ans);
f[i]=0;
}
}
if(sectree==mintree)
printf("Not Unique!\n");
else
printf("%d\n",mintree);
}
return 0;
}


POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏的更多相关文章

  1. Codeforces761B Dasha and friends 2017-02-05 23:34 162人阅读 评论(0) 收藏

    B. Dasha and friends time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. HDU2033 人见人爱A+B 分类: ACM 2015-06-21 23:05 13人阅读 评论(0) 收藏

    人见人爱A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  3. PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏

    1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...

  4. ZOJ2481 Unique Ascending Array 2017-04-18 23:08 33人阅读 评论(0) 收藏

    Unique Ascending Array Time Limit: 2 Seconds      Memory Limit: 65536 KB Given an array of integers ...

  5. PAT甲 1041. Be Unique (20) 2016-09-09 23:14 33人阅读 评论(0) 收藏

    1041. Be Unique (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Being uniqu ...

  6. Uniform Generator 分类: HDU 2015-06-19 23:26 11人阅读 评论(0) 收藏

    Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  7. pascal矩阵 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏

    帕斯卡矩阵 1.定义       帕斯卡矩阵:由杨辉三角形表组成的矩阵称为帕斯卡(Pascal)矩阵. 杨辉三角形表是二次项 (x+y)^n 展开后的系数随自然数 n 的增大组成的一个三角形表. 如4 ...

  8. MATLAB格式化输出控制 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏

    MATLAB格式化输出控制 format 默认格式 format short 5字长定点数 format long 15字长定点数 format short e 5字长浮点数 format long ...

  9. NYOJ-235 zb的生日 AC 分类: NYOJ 2013-12-30 23:10 183人阅读 评论(0) 收藏

    DFS算法: #include<stdio.h> #include<math.h> void find(int k,int w); int num[23]={0}; int m ...

随机推荐

  1. PhoneGap 获得APP的VersionName

    1.首先安装cordova-plugin-app-version cordova plugin add cordova-plugin-app-version 2. 调用方法如下 function ge ...

  2. PHPstorm配置PHPunit对composer引入的php代码进行单元测试

    1. 如何安装PHPunit,这里不展述(如需打断点debug测试,安装PHP的xdebug扩展方法也不展开说了 https://xdebug.org/) 2.如何进行配置 以 PHP设计模式的代码为 ...

  3. 刷新SQL Server所有视图、函数、存储过程 更多 sql 此脚本用于在删除或添加字段时刷新相关视图,并检查视图、函数、存储过程有效性。 [SQL]代码 --视图、存储过程、函数名称 DECLARE @NAME NVARCHAR(255); --局部游标 DECLARE @CUR CURSOR --自动修改未上状态为旷课 SET @CUR=CURSOR SCROLL DYNAMIC FO

    刷新SQL Server所有视图.函数.存储过程 更多   sql   此脚本用于在删除或添加字段时刷新相关视图,并检查视图.函数.存储过程有效性. [SQL]代码 --视图.存储过程.函数名称 DE ...

  4. memcached内存管理机制分析

    memached是高性能分布式内存对象系统,通过在内存中存储数据对象来减少对磁盘的数据读取次数,提高服务速度. 从业务需求出发.我们通过一条命令(如set)将一条键值对(key,value)插入mem ...

  5. 转 shell 命令 http://www.cnblogs.com/me115/p/3427319.html

    http://www.cnblogs.com/me115/p/3427319.html 本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq. ...

  6. (转)powerdesigner 生成sql脚本使用的设置

    本文转载自:http://blog.163.com/lizhihaoo@126/blog/static/103121661201036171115/ 1. 生成sql脚本的时候,提示"con ...

  7. Y480&Y580 刷slic2.1全自动教程

    由于之前时间赶,写得比较乱,现在我重新把我.安装UltraISO.2.插上一个U盘.3.用UltraISO打开setup98.img镜像4.在UltraISO的“启动”的下拉菜单里选择“写入硬盘镜像” ...

  8. python爬虫----基本操作

    一.爬虫基本操作 有些网站和其他网站是有关系(链接),全球的网站就相当于一个蜘蛛网,我们放一只蜘蛛在上面爬,一定能够把网爬个遍.那么如果我们要爬取互联网上内容我们就相当于放一只蜘蛛在上面. 爬虫分为 ...

  9. c#二维码建立与识别

    QrCodeEncodingOptions options = new QrCodeEncodingOptions(); options.CharacterSet = "UTF-8" ...

  10. .net 架构

    .net Webservice 三层架构,BLL(业务逻辑层),DAL(数据访问层)sql语句.MODEL模型层也就是实体层Entity(数据库字段和类的定义的映射). UI层(Web/Form)界面 ...