POJ1679 The Unique MST【次小生成树】
题意:
判断最小生成树是否唯一。
思路:
首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值看与原最小生成树的权值比较看其是否唯一。其实也可以理解成次小生成树加上最大边权的边后是否唯一。
代码:
krusual:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring> using namespace std; typedef long long ll;
const int N=;
const int M=;
int i,j,n,m,cnt,parent[N];
bool flag; struct man
{
int u,v,w;
int eq,used,del;
} edg[N]; bool cmp(man g,man h)
{
return g.w<h.w;
} void init()
{
for(i=; i<=; i++)
{
parent[i]=i;
}
} int Find(int x)
{
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}//查找并返回节点x所属集合的根节点 void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}//将两个不同集合的元素进行合并 int Kruskal()
{
init();
int sum=;
int num=;
for(i=;i<m;i++)
{
if(edg[i].del==)continue;
int u=edg[i].u;int v=edg[i].v;int w=edg[i].w;
if(Find(u)!=Find(v))
{
sum+=w;
if(!flag)edg[i].used=;
num++;
Union(u,v);
}
if(num>=n-)break;
}
return sum;
} int main()
{
int t;
cin>>t;
while(t--)
{
cnt=;
cin>>n>>m;
for(i=; i<m; i++)
{
cin>>edg[i].u>>edg[i].v>>edg[i].w;
edg[i].del=;
edg[i].used=;
edg[i].eq=;//eq没有初始化
}
for(i=;i<m;i++)
{
for(j=;j<m;j++)
{
if(i==j)continue;
if(edg[i].w==edg[j].w)edg[i].eq=;
}
}
sort(edg,edg+m,cmp);
flag=false;
cnt=Kruskal();
flag=true;
bool gg=false;
for(i=;i<m;i++)
{
if(edg[i].used==&&edg[i].eq==)
{
edg[i].del=;
int s=Kruskal();//printf("%d %d\n",i,s);
if(s==cnt)
{
gg=true;
cout<<"Not Unique!"<<endl;
break;
}
edg[i].del=;
}
}
if(!gg) cout<<cnt<<endl;
}
return ;
}
POJ1679 The Unique MST【次小生成树】的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- POJ1679 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,次小生成树模板题
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- 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: 23942 Accepted: 8492 D ...
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- 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 ...
随机推荐
- HTMl Dom操作
訪問: getElementById():帶回指定id的所有元素 getElementByTagName():帶回指定標籤名的所有的元素 getElementByClassName():帶回指定類型名 ...
- BZOJ2223[Coci 2009]PATULJCI——主席树
题目描述 输入 先输入一个数n,然后一个数表示这n个数中最大的是多少,接下来一行n个数.然后一个数m,最后m行询问每次两个数l,r. 输出 no或者yes+这个数 样例输入 10 3 1 2 1 2 ...
- codeforces 1B Spreadsheets
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is u ...
- day6 笔记
元祖 只读不可取的列表,数据不能被修改 a = (20,30) 格式同列表,只是[] 换成了 () ps:元祖在创建的时候,最好在最后的元素后面加一个逗号,比如a = ( 20,) 用法类似于列表:切 ...
- MT【16】证明无理数(2)
证明:$sin10^0$为无理数. 分析:此处用$sin$的三倍角公式,结合多项式有有理根必须满足的系数之间的关系可以证明. 评:证明$sin9^0$为无理数就不那么简单.思路:先利用$sin54^0 ...
- Android自动化测试探索
Android自动化测试探索 前言 通常来说,我们开发完成产品之后,都是由测试组或者是我们自己点一点,基本上没有问题了就开始上线.但是,随着时间的堆叠,一款产品的功能也越来越多.这时,我们为了保证产品 ...
- 【转】19个必须知道的Visual Studio快捷键
本文将为大家列出在Visual Studio中常用的快捷键,正确熟练地使用快捷键,将大大提高你的编程工作效率. 项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt ...
- hdu 3038(扩展并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:给出区间[1,n],下面有m组数据,l r v区间[l,r]之和为v,每输入一组数据,判断 ...
- bzoj3451 Normal
题意:点分治每次随机选重心,求期望复杂度. 发现一次点分治的复杂度就是点分树上每个节点的子树大小之和.(并没有发现......) 看这个. 注意这个写法有问题,随便来个菊花图就是n2了. 每一层点分治 ...
- A1069. The Black Hole of Numbers
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...