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来判断 考虑权值相等的边, ...
随机推荐
- __getattribute__和item系列
# class Foo: # def __init__(self,x): # self.x=x # # def __getattr__(self, item): # print('执行的是我') # ...
- python8.1多线程
import threadingimport time def run1 (name,sex): print(name,sex,"执行线程1") time.sleep(3)def ...
- 小伙子自从学会用Python爬取岛国“动作”电影,身体一天不如一天
在互联网的世界里,正确的使用VPN看看外面的世界,多了解了解世界的发展.肉身翻墙后,感受一下外面的肮脏世界.墙内的朋友叫苦不迭,由于某些原因,VPN能用的越来越少.上周我的好朋友狗子和我哭诉说自己常用 ...
- 精讲RestTemplate第3篇-GET请求使用方法详解
本文是精讲RestTemplate第3篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- 这几个冷门却实用的 Python 库,我爱了!
- SpringBoot进阶教程(六十三)Jasypt配置文件加密
数据库密码直接明文写在配置中,对安全来说,是一个很大的挑战.一旦密码泄漏,将会带来很大的安全隐患.尤其在一些企业对安全性要求很高,因此我们就考虑如何对密码进行加密.本文着重介绍Jasypt对Sprin ...
- C++实现哈夫曼编码/译码器(数据结构)
设计一个哈夫曼编码.译码系统.对一个ASCII编码的文本文件中的字符进行哈夫曼编码,生成编码文件:反过来,可将编码文件译码还原为一个文本文件.(1) 从文件中读入任意一篇英文短文(文件为ASCII编码 ...
- C#设计模式之5-单例模式
单例模式(Singleton Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/397 访问. 单例模式属 ...
- C#算法设计查找篇之04-斐波那契查找
斐波那契查找(Fibonacci Search) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/704 访问. 斐波那契 ...
- CSS动画实例:旋转的圆角正方形
在页面中放置一个类名为container的层作为效果呈现容器,在该层中再定义十个名为shape的层层嵌套的子层,HTML代码描述如下: <div class="container&qu ...