POJ 1679 The Unique MST --Kruskal应用
这题可以用次小生成树解,这里用Kruskal算法来做。每条边除维护u,v,w外,还维护:
used:表示这条边是否加过
eq:表示有没有与这条边相等的边
del:删除标记,以便删边之用
如果对于一个最小生成树的的边A,有一条与之权值相等的边B,则考虑把A删掉,再求一次最小生成树,看求出的总权值是否与前一个最小生成树的总权值相等。如果相等,则不唯一,如果找遍了这些权值相等的边都没找到,就说明唯一(注意每次不相等的话要把A重新加进来)。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 107 struct Edge
{
int u,v,w;
bool eq,del,used;
}edge[N*N]; int cmp(Edge ka,Edge kb)
{
return ka.w < kb.w;
} int fa[N],n,m,first; int findset(int x)
{
if(x != fa[x])
fa[x] = findset(fa[x]);
return fa[x];
} int Kruskal()
{
int i,j,k = ;
int sum = ;
for(i=;i<=n;i++)
fa[i] = i;
for(i=;i<=m;i++)
{
if(edge[i].del)
continue;
int u = edge[i].u;
int v = edge[i].v;
int fx = findset(u);
int fy = findset(v);
if(fx != fy)
{
sum += edge[i].w;
fa[fx] = fy;
if(first)
edge[i].used = ;
k++;
}
if(k >= 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);
edge[i].u = u;
edge[i].v = v;
edge[i].w = w;
edge[i].del = edge[i].used = edge[i].eq = ;
}
for(i=;i<=m;i++)
{
for(j=;j<=m;j++)
{
if(i == j)
continue;
if(edge[i].w == edge[j].w)
edge[i].eq = ;
}
}
sort(edge+,edge+m+,cmp);
first = ;
int w1 = Kruskal();
int w2;
first = ;
for(i=;i<=m;i++)
{
if(edge[i].used && edge[i].eq)
{
edge[i].del = ;
w2 = Kruskal();
if(w1 == w2)
{
puts("Not Unique!");
break;
}
edge[i].del = ;
}
}
if(i > m)
printf("%d\n",w1);
}
return ;
}
POJ 1679 The Unique MST --Kruskal应用的更多相关文章
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
- POJ 1679 The Unique MST:次小生成树【倍增】
题目链接:http://poj.org/problem?id=1679 题意: 给你一个图,问你这个图的最小生成树是否唯一. 题解: 求这个图的最小生成树和次小生成树.如果相等,则说明不唯一. 次小生 ...
- 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 (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
随机推荐
- JAVA书写规范
java程序书写规范 命名规范 1.一般概念 1.尽量使用完整的英文描述符 2.采用适用于相关领域的术语 3.采用大小写混合使名字可读 4 ...
- 使用WCF对外提供接口
本篇将通过WCF以webservices的方式对外提供接口.同时使用NUnit对webservices中的方法进行单元测试. 开发契约 contract Contract项目为类库项目,该项目下会包含 ...
- jQuery $.extend()用法总结
Query开发插件的两个方法 1.jQuery.extend(object);为扩展jQuery类本身.为类添加新的方法. 2.jQuery.fn.extend(object);给jQuery对象添加 ...
- 优化ABAP性能(摘录)
1.使用where语句不推荐Select * from zflight.Check : zflight-airln = ‘LF’ and zflight-fligh = ‘BW222’.Endsele ...
- .NET破解之轻量万能自定义信息管理系统
一般敢说万能的莫非真有两把刷子.今天来破解试试,看效果好用不. 下载:http://down.chinaz.com/soft/36780.htm 补丁: http://www.t00y.com/fil ...
- sharepoint 中waiting screen dialog的使用方法(JSOM)
sharepoint中有一个种wait screen的弹出框,其实就是一直转圈,告诉你等待一会儿时间.用法如下: 弹出: var watiDialog = SP.UI.ModalDialog.show ...
- 2015年第5本(英文第4本):Death on the Nile尼罗河上的惨案
书名:Death on the Nile 作者: Agatha Christie 单词数:7.9万(读完后发现网上还有一个版本,总共2.7万单词,孩子都能读懂,看来是简写版) 词汇量:6700 首万词 ...
- android 开源和一些博客总结
记录一些开源的android库 (1)Http请求库封装 https://github.com/kevinsawicki/http-request (2)浮动组件,定制化 toast http://f ...
- C语言泛型编程--抽象数据类型
一.数据类型: 在任何编程语言中,数据类型作为一个整体,ANSI-C包含的类型为:int.double.char……,程序员很少满意语言本身提供的数据类型,一个简单的办法就是构造类似:array.st ...
- C++pair类型
标准库类型--pair类型定义在utility头文件中定义 本文地址:http://www.cnblogs.com/archimedes/p/cpp-pair.html,转载请注明源地址. 1.pai ...