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

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!

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 10090
using namespace std; struct Node
{
int a,b,c;
bool same,used,del;
}f[N];
int n,m;
int fa[N]; int findfa(int x)
{
if(x!=fa[x])
fa[x]=findfa(fa[x]); return fa[x];
} void init()
{
for(int i=0;i<200;i++)
fa[i]=i;
} int cmp(Node a,Node b)
{
return a.c<b.c;
} bool first; void make_same(int m)
{
for(int i=1;i<m;i++)
if(f[i].c==f[i-1].c)
f[i-1].same=true;
} int kruscal(int m)
{
int ans=0;
for(int i=0;i<m;i++)
{
if(f[i].del)continue; int x=findfa(f[i].a);
int y=findfa(f[i].b); if(x==y)
continue;
else
{
fa[x]=y; ans+=f[i].c;
if(first)
f[i].used=true; }
}
return ans;
} int main()
{
int ca=1;
scanf("%d",&ca); while(ca--)
{
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&f[i].a,&f[i].b,&f[i].c);
f[i].del=false;f[i].same=false;f[i].used=false;
} sort(f,f+m,cmp); first=true;
init(); int ans1=kruscal(m);
first=false; make_same(m); int flag=0; for(int i=0;i<m;i++)
{
if(f[i].used && f[i].same)//used表示在第一次求出的最小生成树中加入过的边
{//same表示在存在和已加入边权值同样的边,此时标记删除该边在推断是否ans相等 f[i].del=true;
init(); int ans2=kruscal(m); //cout<<"ans2="<<ans2<<endl;
if(ans1==ans2)
{
puts("Not Unique!");
flag=1;
break;
}
f[i].del=false;
}
} if(flag==0)
printf("%d\n",ans1); } return 0;
}

POJ 1679 The Unique MST 推断最小生成树是否唯一的更多相关文章

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

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

  2. poj 1679 The Unique MST 判断最小生成树是否唯一(图论)

    借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...

  3. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

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

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

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

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

  6. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

  7. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

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

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

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

  9. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

随机推荐

  1. vmware linux虚拟机与本地物理机共享文件夹

    cd /mnt/hgfs 使用Vmware安装了linux虚拟机后,开发时,为了方便文件的传输等,因此需要使用共享文件夹,减少工作量.共享文件夹需要用到vmware提供的vmware tools工具, ...

  2. c++内联函数解析(inline)

    一.基本定义 inline是C++语言中的一个关键字,可以用于程序中定义内联函数,inline的引进使内联函数的定义更加简单.说到内联函数,这里给出比较常见的定义,内联函数是C++中的一种特殊函数,它 ...

  3. 剑指Offer(书):数组中重复的数字

    题目:找出数组中重复的数字. 说明:在一个长度为n的数组里的所有数字都在0~n-1的范围内,数组中某些数字是重复的,但是不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...

  4. 【HIHOCODER 1513】 小Hi的烦恼 (BitSet)

    描述 小Hi从小的一大兴趣爱好就是学习,但是他发现尽管他认真学习,依旧有学神考的比他好. 小Hi在高中期间参加了市里的期末考试,一共五门:语文.数学.英语.物理.化学. 成绩出来之后,小Hi发现有些同 ...

  5. python3--算法基础:二维数组转90度

    python3--算法基础:二维数组转90度 [0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3] 二维数组转90度 [0, 0, 0, 0][1, 1, ...

  6. python023 Python3 标准库概览

    Python3 标准库概览 操作系统接口 os模块提供了不少与操作系统相关联的函数. >>> import os >>> os.getcwd() # 返回当前的工作 ...

  7. 【MVC 2】MVC+EF框架结构实例:注册ID号验证

    导读:本篇博客,将通过一个实例,详细介绍MVC+EF的应用.原理性的东西或者说是进一步的解耦和,请看博客: [框架结构 3]MVC+EF实体框架-原理解析.在这里,仅用MVC框架和一个EF生成的Mod ...

  8. hdu 4539

    #include<stdio.h> #include<string.h> ]; int s]; int main() { int i,j,n,m; int ch; while( ...

  9. [转]使用fdisk磁盘分区和 Linux 文件系统

    概述 在本文中,学习磁盘分区和 Linux 文件系统相关内容.学习: 创建分区 使用 mkfs 命令来设置 ext2.ext3.ext4.xfs.Reiser v3 和 vfat 文件系统 创建和管理 ...

  10. 【2018 Multi-University Training Contest 4】

    01: 02:https://www.cnblogs.com/myx12345/p/9407551.html 03: 04:https://www.cnblogs.com/myx12345/p/940 ...