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 ...
随机推荐
- schema举例二
schema举例二: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs=& ...
- BZOJ3772精神污染——可持久化线段树+出栈入栈序
题目描述 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区,是日本西部门户 ...
- JavaScript利用递归和循环实现阶乘
[实现方法] 1.利用while循环来做,当然for循环也可以. 2.递归 [代码内容] 偷懒,直接用onkeyup事件来限制来页面的输入 循环代码: //第一种方法 while循环 oCount.o ...
- POJ1942-Paths On a Grid-组合数学
从n+m步中挑选min(n,m)步向上走,剩下的就是向下走. 求解n+mCmin(n,m)时,要一边计算一边约分. #include <cstdio> #include <algor ...
- [ctsc2018] 混合果汁 【可持久化线段树】【二分答案】
题目分析 首先考虑到最小值最大,二分答案.假设答案为k,显然这满足单调性.如果某个k使得这个情况下选不出.那么比k大的一定也选不出,所以二分答案. 接着我们可以贪心,当我们确认了k以后,一定会优先选费 ...
- 自学Linux Shell14.3-创建临时文件
点击返回 自学Linux命令行与Shell脚本之路 14.3-创建临时文件mktemp Linux系统保留了一个特殊的目录位置,以供临时文件使用.Linux使用/tmp目录处理不需要永久保存的文件. ...
- 【BZOJ2281】[SDOI2011]黑白棋(博弈论,动态规划)
[BZOJ2281][SDOI2011]黑白棋(博弈论,动态规划) 题面 BZOJ 洛谷 题解 先看懂这题目在干什么. 首先BZOJ上面的题面没有图,换到洛谷看题就有图了. 不难发现都相邻的两个异色棋 ...
- emwin 之使用键盘数据发送函数的注意事项
@2018-08-08 小记 键盘实现时,在发送键值时, 函数 GUI_SendKeyMsg(GUI_KEY_BACKSPACE, Pressed) 的参数 Pressed 在按键按下状态的 case ...
- shelve模块(二十三)
shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写; key必须为字符串,而值可以是python所支持的数据类型 用的比较少 目的: 将字典写入文件保存起来 i ...
- java8中的stream().filter()的使用和Optional()
转: https://www.cnblogs.com/yimiyan/p/5992440.html Optional: https://www.cnblogs.com/zhangboyu/p/7580 ...