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 ...
随机推荐
- Lodop打印控件输出页码(超文本和纯文本页码)
Lodop打印控件打印超文本既可以手动分页,也可以自动分页,自动分页可阅读本博客的另一篇博文:Lodop打印控件 超文本自动分页 自动分页的时候,往往是不知道需要打印的内容到底分了几页,也就不可能预先 ...
- docker --swarm启动2375端口监听
首先要下载swarm docker pull swarm 然后停掉docker服务: service docker stop 然后启动deamon: sudo dockerd -H tcp://0.0 ...
- BZOJ2144跳跳棋——LCA+二分
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的 游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...
- Threed.sleep是不会释放锁,而wait是释放锁的(对象锁)
实战分析 一直都说,Threed.sleep是不会释放锁,而wait是释放锁的(对象锁),现理论上来分析一下啊. v package thread.concurrent; public class D ...
- Huawei运维记录
Huawei运维记录 01 Huawei运维记录-AC6005-8AP设备启动界面 02 Huawei运维记录-AC6005-8AP添加授权码 03 Huawei运维记录-AC6005版本升级步骤
- SharePoint “File not found” 错误
Troubleshooting the SharePoint "File not found" Error Have you ever come across a "Fi ...
- 【hdu5306】 Gorgeous Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5306 (题目链接) 题意 区间取$min$操作,区间求和操作,区间求最值操作. Solution 乱搞一通竟然A ...
- centos6.5修改主机名
centos 修改主机名 0.说明 系统安装后,系统默认的主机名称是localhost,现在想要修改为master.操作需要root权限. 1.方案一:仅当前登录有效,重启后失效 直接在命令行执行命令 ...
- 2019.3.18考试&2019.3.19考试&2019.3.21考试
2019.3.18 C O D E T1 树上直接贪心,环上for一遍贪心 哇说的简单,码了将近一下午终于码出来了 感觉自己码力/写题策略太糟糕了,先是搞了一个细节太多的写法最后不得不弃疗了,然后第二 ...
- 快速幂&快速乘法
尽管快速幂与快速乘法好像扯不上什么关系,但是东西不是很多,就一起整理到这里吧 快速幂思想就是将ax看作x个a相乘,用now记录当前答案,然后将指数每次除以2,然后将当前答案平方,如果x的2进制最后一位 ...