The Unique MST(最小生成树的唯一性判断)
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!
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
typedef long long ll;
using namespace std;
int pre[maxn],n,m,first; struct node
{
int x,y,val;
int u;
int e;
int d;
} p[maxn];
int find(int x)
{
if(pre[x]==x)
{
return x;
}
else
{
return pre[x]=find(pre[x]);
}
}
int prime()
{
int i,j,k,sum,num;
sum=;num=;
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<=m;i++) {
if(p[i].d) continue;
int fx=find(p[i].x);
int fy=find(p[i].y);
if(fx!=fy) {
num++;
pre[fx]=fy;
sum+=p[i].val;
if(first)
p[i].u=;
}
if(num==n-) break;
}
return sum;
}
bool cmp(node x,node y)
{
if(x.val<y.val)
return true;
else
return false;
}
int main()
{
int k,u,v,w,sum1,sum2;
int T;
scanf("%d",&T);
while(T--)
{
sum1=sum2=;
memset(p,,sizeof(p));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].val);
}
for(int i=;i<=m;i++) {
for(int j=i+;j<=m;j++)
{
if(p[i].val==p[j].val) p[i].e=;
}
}
sort(p+,p++m,cmp);
first=;
sum1=prime();
first=;
bool flag=false;
for(int i=;i<=m;i++)
{
if(p[i].u && p[i].e)
{ p[i].d=;
sum2=prime();
if(sum1==sum2)
{
flag=true;
printf("Not Unique!\n");
break;
}
}
}
if(!flag)
printf("%d\n",sum1);
}
}
The Unique MST(最小生成树的唯一性判断)的更多相关文章
- K - The Unique MST (最小生成树的唯一性)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- The Unique MST (判断是否存在多个最小生成树)
The Unique MST Time Limit: 10 ...
- POJ 1679 The Unique MST 【判断最小生成树是否唯一】
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Defini ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- poj1679 The Unique MST(最小生成树唯一性)
最小生成树的唯一性,部分参考了oi-wiki 如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的 同过kruskal来判断 考虑权值相等的边, ...
随机推荐
- Python分析6000家破产IT公司
前一阵有个字节跳动的程序员火了,年仅28岁实现了财务自由,宣布提前退休.最直接的原因是选择了一家发展前景很好的创业公司.当然平时我们经常能听到,某某人加入创业公司,xx年后公司上市,身价暴涨,财务自由 ...
- Python自动化办公知识点整理汇总
知乎上有人提问:用python进行办公自动化都需要学习什么知识呢? 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却 ...
- 2020-05-18:MYSQL为什么用B+树做索引结构?平时过程中怎么加的索引?
福哥答案2020-05-18:此答案来自群员:因为4.0成型那个年代,B树体系大量用于文件存储系统,甚至当年的Longhorn的winFS都是基于b树做索引,开源而且好用的也就这么个体系了.B+树的磁 ...
- C#LeetCode刷题之#434-字符串中的单词数(Number of Segments in a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3941 访问. 统计字符串中的单词个数,这里的单词指的是连续的不是 ...
- C#LeetCode刷题之#202-快乐数(Happy Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3856 访问. 编写一个算法来判断一个数是不是"快乐数& ...
- JavaScript 循环数组的时候调用方法中包含Promise的时候如何做到串行
forEach是不能阻塞的, 默认[并行]方式 const list = [1, 2, 3] const square = num => { return new Promise((resolv ...
- 支持向量机SVM介绍
SVM为了达到更好的泛化效果,会构建具有"max-margin"的分类器(如下图所示),即最大化所有类里面距离超平面最近的点到超平面的距离,数学公式表示为$$\max\limits ...
- externaltrafficpolicy的有关问题说明
环境描述 生产环境通过gitlab-running实现自动化发布业务,现需要收集客户端的真实ip,需要将externaltrafficpolicy改为lacal模式(原来是cluster模式),前天开 ...
- Golang并发编程基础
硬件 内存 作为并发编程一个基础硬件知识储备,首先要说的就是内存了,总的来说在绝大多数情况下把内存的并发增删改查模型搞清楚了其他的基本上也是异曲同工之妙. 内存芯片--即我们所知道的内存颗粒,是一堆M ...
- Debug很重要
之前做一个小功能,就是用php发送邮件,项目中已经使用了wordpress的wp_mail,所以同事建议我继续用wp_mail函数. 然而遇到了一个奇怪的情况,邮件没有发出去,也没有任何报错日志. 照 ...