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. 部署Web API后Delete请求总是报 405(Method Not Allowed)解决办法

    WebDAV                   安装IIS的时候如果选择了WebDAV(Web Distribution Authorization Versioning) Publish,则所有的 ...

  2. [转]基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作

    基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作  转载请注明作者及来源:张峻崎,博客园 目前在.net的范围内,好的而且方便的ORM的真的不是很多,与VS集成 ...

  3. 完全卸载vs2013、vs2015的方法

    Visual Studio安装过程会安装好多组件,如果想要卸载的话会出现一些因难,在控制面板不容易卸载干净,在Linux下的命令都有--help参数来显示命令的用法,今天突发奇想,在控制台下输入vs2 ...

  4. USACO 2016 US Open Contest, Gold解题报告

    1.Splitting the Field http://usaco.org/index.php?page=viewproblem2&cpid=645 给二维坐标系中的n个点,求ans=用一个 ...

  5. bzoj1811 mea

    Description 考虑一个非递减的整数序列 S1,....Sn+1(Si<=Si+1  1<=i<=n). 序列M1...Mn是定义在序列S的基础上,关系式为 Mi=( Si ...

  6. JVM参数之-XX:+HeapDumpOnOutOfMemoryError(导出内存溢出的堆信息(hprof文件))

    当出现HeapDumpOnOutOfMemoryError错误时,我们需要分析原因,下面的程序就是模拟这个错误并导出dump文件,好让你分析 GcTest.java package gc; publi ...

  7. Redux 处理异步 Action

    redux-promise-utils What redux-promise-utils 是一个基于 redux-thunk 和 redux-actions 的工具,符合 FSA 规范,方便开发者处理 ...

  8. parentNode,parentElement,offsetParent

    offsetParent直接的将是影响元素位置的上级element,而parentElement与位置显示无关时dom中的上级element. 例如: <BODY> <div sty ...

  9. Oracle安装过程出现问题---------安装Oracle11gR2先决条件检查失败

    一.错误信息当安装到“先决条件检查” 时,提示如下图所示的错误: 二.错误原因一般情况下,由于操作系统未开启默认共享,导致Oracle无法检查环境的可用性. 三.解决方法1.在运行中(或键盘按 Win ...

  10. window.location 属性

    属性 含义 值 protocol: 协议 "http:" hostname: 服务器的名字 "b.a.com" port: 端口 "88" ...