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. 日常:论我的T3是如何苟掉的

    说起来都是泪啊 T3的bfs一直不对,我想死. 考试时候苟了两个小时,死活找不到错误(因为lca和离散化都码出来了,太乱) 最后把bfs单独提出来,发现在bfs里面输出的f[ly][0]都是正确的,到 ...

  2. java生成随机字符

    1.生成的字符串每个位置都有可能是str中的一个字母或数字,需要导入的包是import java.util.Random; //length用户要求产生字符串的长度 public static Str ...

  3. 教你学会Linux/Unix下的vi文本编辑器

    vi编辑器是Unix/Linux系统管理员必须学会使用的编辑器.看了不少关于vi的资料,终于得到这个总结. 首先,记住vi编辑器的两个模式:1.命令模式 2.编辑模式. 在一个UNIX/Linux的s ...

  4. java面试宝典第一弹

    object类的直接子类有哪些 Boolean Character Character.Subset Class ClassLoader Compiler Enum Math Number Packa ...

  5. mongdb数据库的操作

    一.数据库使用 1.使用mongodb服务,必须先开启服务,开启服务使用 mongod --dbpath D:mongdb    (D:mongdb  自己所创建数据库的路径, 在cmd窗口中输入) ...

  6. 简单DP内容

    1. 最长上升子序列 [题目描述] 给定N个数,求这N个数的最长上升子序列的长度. [样例输入] 7 2 5 3 4 1 7 6 [样例输出] 4 第一种解法:时间复杂度O(n^2), 状态设计:DP ...

  7. Android获取屏幕大小(Px)

    private DisplayMetrics dm = new DisplayMetrics(); TextView tv; Button bu; @Override protected void o ...

  8. 【BZOJ 2431】 [HAOI2009] 逆序对数列 (DP)

    Description 对于一个数列{ai},如果有iaj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样自然数数 ...

  9. jQuery中attr和prop方法的区别

    jQuery中attr和prop方法的区别。 http://my.oschina.net/bosscheng/blog/125833 http://www.javascript100.com/?p=8 ...

  10. unittest多线程执行用例

    前言 假设执行一条脚本(.py)用例一分钟,那么100个脚本需要100分钟,当你的用例达到一千条时需要1000分钟,也就是16个多小时... 那么如何并行运行多个.py的脚本,节省时间呢?这就用到多线 ...