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. Tomcat 7 可以修改 Session 默认的 Cookie 名 JSESSIONID 了

    Tomcat 7 可以修改 Session 默认的 Cookie 名 JSESSIONID 了       程序员必上的开发者服务平台 —— DevStore 看看下面这个配置: <Contex ...

  2. GNU Radio: Overview of the GNU Radio Scheduler

    Scetion 1: The Flowgraph The flowgraph moves data from sources into sinks. 一个流图由多个模块组成,其中一般包括信源(Sour ...

  3. 视频支持拖动进度条播放的实现(基于nginx)

    http协议下的flv/mp4流式播放支持的三个要点: 1 服务器端要支持flv/mp4流式播放,现在nginx或者lighttpd都是支持这样的应用的,还支持mp4的流式播放(默认编译版本一般都是打 ...

  4. MapBuilder,操作集合工具类

    public class MapBuilder { /** * Creates an instance of {@code HashMap} */ public static <K, V> ...

  5. 学习笔记之LeetCode

    LeetCode Online Judge https://leetcode.com/ Leetcode:在线编程网站-各大IT公司的笔试面试题 http://blog.csdn.net/huixin ...

  6. Keras 入门

    “sample”“batch”“epoch” Sample:样本,比如:一张图像是一个样本,一段音频也是一个样本. Batch:批,含有N个样本的集合.每一个batch的样本都是独立的并行处理.在训练 ...

  7. Jquery 插件PrintArea 打印指定的网页区域

    Jquery 插件PrintArea 打印指定的网页区域 需要下载jquery 和printarea.js插件 PrintArea.Js插件,可以打印整个网页中某个指定的区域. $("打印区 ...

  8. pm无力的话

    1. 先这样做吧, 等不行再改 2. 用户的需求不明确, 他们对于自己的业务也不明白, 现在是我们在帮助他们缕清自己的业务, 这个迭代的过程中,有很多问题,我们程序员既不能参与到业务, 也不能猜测业务 ...

  9. Selenium2+python自动化62-jenkins持续集成环境搭建

    前言 selenium脚本写完之后,一般是集成到jenkins环境了,方便一键执行. 一.环境准备 小编环境: 1.win10 64位 2.JDK 1.8.0_66 3.tomcat 9.0.0.M4 ...

  10. mysql 百万级数据库优化方案

    https://blog.csdn.net/Kaitiren/article/details/80307828 一.百万级数据库优化方案 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 wher ...