题目链接:http://poj.org/problem?id=1679

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29408   Accepted: 10520

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! 题目大意: 给定一个连通无向网,判定它的最小生成树是否唯一。 解题思路: http://www.cnblogs.com/yoke/p/6527300.html AC代码:
 #include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; struct point
{
int u,v,w;
int equal; // 标记,1表示存在其他边权值跟该边一样,0表示不存在
int used; // 在第一次求得的MST中,是否包含该边,1包含,0不包含
int del; // 边是否删除 0不删除 1删除
}p[]; // 存边的数组
int n,m;
int first; // 表示第一次求MST的标记变量
int parent[];
bool cmp(point a, point b)
{
return a.w < b.w;
}
int find (int x)
{
int s,tmp;
for (s = x; parent[s] >= ; s = parent[s]);
while (s != x)
{
tmp = parent[x];
parent[x] = s;
x = tmp;
}
return s;
}
void Union (int A, int B)
{
int a = find(A), b = find(B);
int tmp = parent[a]+parent[b];
if (parent[a] > parent[b])
{
parent[a] = b;
parent[b] = tmp;
}
else
{
parent[b] = a;
parent[a] = tmp;
}
}
int kruskal()
{
int sum = ,num = ;
memset(parent,-,sizeof(parent));
for (int i = ; i < m; i ++)
{
if (p[i].del) continue; // 忽略去掉的边
int u = p[i].u, v = p[i].v;
if (find(u) != find(v))
{
if (first) p[i].used = ;
sum += p[i].w;
Union(u,v);
num ++;
}
if (num == n-) break;
}
return sum;
}
int main ()
{
int t,i,j,u,v,w;
scanf("%d",&t);
while (t --)
{
scanf("%d%d",&n,&m);
for (i = ; i < m; i ++)
{
scanf("%d%d%d",&u,&v,&w);
p[i].u = u; p[i].v = v; p[i].w = w;
p[i].equal = ; p[i].used = ; p[i].del = ;
}
for (i = ; i < m; i ++) // 标记相同权值的边
for (j = i+; j < m; j ++)
if (p[i].w == p[j].w)
p[i].equal = ;
first = ;
sort(p,p+m,cmp);
int sum = kruskal(), sum1; // 第一次求MST
first = ;
for (i = ; i < m; i ++)
{
if (p[i].equal && p[i].used) // 依次去掉原MST中相同权值的边
{
p[i].del = ;
sum1 = kruskal();
if (sum == sum1)
{
printf("Not Unique!\n");
break;
}
p[i].del = ;
}
}
if (i == m)
printf("%d\n",sum);
}
return ;
}

poj 1679 The Unique MST (判定最小生成树是否唯一)的更多相关文章

  1. POJ 1679 The Unique MST 推断最小生成树是否唯一

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

  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. Unity 行为树-中断机制

    一.中断类型 设置了中断之后,行为树会检测执行过的子条件节点,当条件节点的状态发生变化时,会中断正在执行的Running节点,转而立即执行该条件节点. 行为树的打断类型有4种: None Self L ...

  2. py文件打包成exe程序

    C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts https://blog.csdn.net/lqzdream ...

  3. 基础篇:3.4)3d模型绘制的好坏会影响产品合格率(注意点)

    本章目的:为了量产品的产能与合格率,重视3d图纸. 1.前言 作者希望本文能引起重视,是那些刚入行业的菜鸟: 还有只用2d图纸,便能绘制出能量产合格品的前辈大牛工程师. 2.3d图纸不合格的现状及典型 ...

  4. vscode用yuml画类图

    vscode用yuml画类图 最近在找画类图的工具,发现vscode一款插件很好用,还支持markdown.vscode插件中直接搜索yuml安装即可. 文件后缀.yuml. 文件开头第一行这样写// ...

  5. 搭建github静态博客

    github设置 建立新的repository,命名为OwnerName.github.io,例如gotochenglong.github.io git管理 设置ssh密匙 使用命令ssh-keyge ...

  6. Mongodb利用aggregation实现抽样查询(按记录数和时间)

    之前对mongodb不熟,但是项目要用,因为数据量比较大,并且领导要实现抽样查询,控制数据流量,所以自己研究了下,亲测可用,分享一下! 话不多说,上代码: 第一种方案:加自增主键,实现按记录数抽样 1 ...

  7. Linux多个机器配置ssh免登陆

    多机器ssh免密码登录的教程,网上有很多,多的数不过来,但是我的安装过程不是很顺利,因为刚开始使用的是普通的user,虽然配置了sudo权限,但是没有root权限,导致了无论如何配置都不能实现免密码登 ...

  8. Eclipse取消或者关闭tomcat所有自动发布(部署)方法

    1.设置publishing为Never publish automaticallu 2.modules->edit->auto reloading enabled 3.Windows & ...

  9. 虚拟机非正常关闭,里面的服务器重启报错:Error, some other host already uses address

    解决办法: vi /etc/sysconfig/network-scripts/ifup-eth ###########注销下面的三行内容############ # if ! /sbin/arpin ...

  10. php中array_walk() 和 array_map()两个函数区别

    两个函数的共性和区别: 1.传入这两个函数的 $value,就是数组中的单一个元素. 2.array_walk() 仅返回true或者false,array_map() 返回处理后的数组: 3.要得到 ...