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来判断 考虑权值相等的边, ...
随机推荐
- CSS-flex|gird 布局
网页布局.css *{ box-sizing: border-box; } /* flex弹性布局 */ #flex-container { display: flex; flex-direction ...
- win7(64位)使用DEBUG
win7 64位好像是不能直接打开DOS进行DEUBG的,于是查找相应解决方案 开始看其他人的帖子,写得语焉不详,后来一查,居然是抄百度的.....自己不觉得low吗... 参考百度经验的回答http ...
- 最全总结!聊聊 Python 调用 JS 的几种方式
1. 前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大 ...
- Tutte 定理与 Tutte–Berge 公式
Tutte theorem 图 \(G=(V,E)\) 有完美匹配当且仅当满足 \(\forall U\subseteq V,o(G-U)\le|U|,o(X)\) 表示 X 子图的奇连通块数. Tu ...
- 朴素贝叶斯算法java实现(多项式模型)
网上有很多对朴素贝叶斯算法的说明的文章,在对算法实现前,参考了一下几篇文章: NLP系列(2)_用朴素贝叶斯进行文本分类(上) NLP系列(3)_用朴素贝叶斯进行文本分类(下) 带你搞懂朴素贝叶斯分类 ...
- 在Springboot中写使用jsp
jsp其实可以看成一种模板语言,在Springboot中我们同样可以使用jsp.我们可以把引入jsp的过程分为三步: 第一步:POM文件加依赖: <!--引入springboot内嵌的tomca ...
- 【Java】AES加机解密工具类代码
import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypt ...
- C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4074 访问. 给定一个二叉树,判断它是否是高度平衡的二叉树. 本 ...
- C#LeetCode刷题之#896-单调数列(Monotonic Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3760 访问. 如果数组是单调递增或单调递减的,那么它是单调的. ...
- Vue Vue.use() / Vue.component / router-view
Vue.use Vue.use 的作用是安装插件 Vue.use 接收一个参数 如果这个参数是函数的话,Vue.use 直接调用这个函数注册组件 如果这个参数是对象的话,Vue.use 将调用 ins ...