poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 29408 | Accepted: 10520 |
Description
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
Output
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 (判定最小生成树是否唯一)的更多相关文章
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
- poj 1679 The Unique MST 判断最小生成树是否唯一(图论)
借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...
- 【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- POJ 1679 The Unique MST(最小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
随机推荐
- css第二篇:样式的特殊性、重要性、继承和层叠
特殊性: 假设有几个不同的规则改变的都是同一个元素的值,那么哪一个规则将会胜出呢?这就得靠特殊值啦,什么是特殊值呢?特殊值的大小呢?如下图: 值越大代表越牛,如1,0,0,0永远大于0,X,X,X ...
- 第二章C++实验
2-28 (1)用if else 语句 #include<iostream> using namespace std; int main(){ char alphabet; while ( ...
- Python——免费观看全网视频小程序
说明,这个小程序是基于网站“全民解析”,调用该网站的接口,实现数据传输观看视频,若该网站凉凉,则此程序凉凉. 开始之前的分析: 进入全民解析网站,我们首先查看一下网页的html代码,发现该站观看视频的 ...
- CDH集群安装配置(七)--CDH组件的安装和配置
1. Clouder Manger页面的配置 访问主节点IP:(cdh1)192.168.80.81:7180 默认用户名和密码:admin,admin 选择一个版本 选择集群的服务器(agent), ...
- 一篇Java图片验证码生成的代码
package projectUtil; /** * @author tian * @date 2019/4/1015:58 */ import javax.imageio.ImageIO; impo ...
- php session的简单使用
创建session: session_start(); $_SESSION['name'] = $value; 获取session: session_start(); echo $_SESSION[' ...
- Java中的值传递和地址传递(传值、传引用)
首先,不要纠结于 Pass By Value 和 Pass By Reference 的字面上的意义,否则很容易陷入所谓的“一切传引用其实本质上是传值”这种并不能解决问题无意义论战中.更何况,要想知道 ...
- 【Css】Layout布局(一)
看下图: css框模型(Box Model),也有叫做盒模型的.规定了元素框处理元素内容.内边距.边框 和 外边距 的方式. element元素,也是内容的主体: padding内边距,也右称为填充的 ...
- Mysql远程连接配置
Mysql远程连接配置 环境:unbuntu 16.04 最新版本的Mysql在远程连接的配置上与老版本有了一些出入,照原先的配置已经不行了,所以在这里记录一下遇到的所有新问题. 配置远程连接的步骤如 ...
- MySQL事务实现原理
MySQL事务隔离级别的实现原理 知识储备 只有InnoDB支持事务,所以这里说的事务隔离级别是指InnoDB下的事务隔离级别 隔离级别 读未提交:一个事务可以读取到另一个事务未提交的修改.这会带来脏 ...