POJ 1679 The Unique MST(推断最小生成树_Kruskal)
Description
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
triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int father[111],n,m,first; struct node
{
int u,v,w;
int used;
int equal;
int del;
} a[11111]; bool cmp(node x,node y)
{
if(x.w<y.w) return true;
return false;
} int find(int x)
{
int r=x;
while(father[r]!=r) r=father[r];
int i=x,j;
while(i!=r) {
j=father[i];
father[i]=r;
i=j;
}
return r;
} int prime()
{
int i,j,k,sum,num;
sum=0;num=0;
for(i=1;i<=n;i++) father[i]=i;
for(i=1;i<=m;i++) {
if(a[i].del) continue;
int fx=find(a[i].u);
int fy=find(a[i].v);
if(fx!=fy) {
num++;
father[fx]=fy;
sum+=a[i].w;
if(first) a[i].used=1;
}
if(num==n-1) break;
}
return sum;
} int main()
{
int i,j,k,u,v,w,sum1,sum2;
int t;
scanf("%d",&t);
while(t--) {
sum1=sum2=0;
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++) {
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
}
for(i=1;i<=m;i++) {
for(j=i+1;j<=m;j++) {
if(a[i].w==a[j].w) a[i].equal=1;
}
}
sort(a+1,a+1+m,cmp);
first=1;
sum1=prime();
first=0;
for(i=1;i<=m;i++) {
if(a[i].used && a[i].equal) {
a[i].del=1;
sum2=prime();
if(sum1==sum2) {
printf("Not Unique!\n");
break;
}
}
}
if(i==m+1) printf("%d\n",sum1);
}
}
POJ 1679 The Unique MST(推断最小生成树_Kruskal)的更多相关文章
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
- poj 1679 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 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- 【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...
- 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 判断最小生成树是否唯一(图论)
借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
随机推荐
- Jsp页面报错状态码含义
原来,全部在HttpServletResponse接口的字段里 状态码 (),表示一个请求已经被接受处理,但还没有完成. 状态码 (),表明HTTP服务器从一个服务器收到了一个无效的响应,当其作为一 ...
- Lua Time
-- local getTime = os.date(“%c”); -- %a abbreviated weekday name (e.g., Wed)-- %A full weekday name ...
- codechef MAY18 div2 部分题解
T1 https://www.codechef.com/MAY18B/problems/RD19 刚开始zz了,其实很简单. 删除一个数不会使gcd变小,于是就只有0/1两种情况 T2 https:/ ...
- (转)vuex2.0 基本使用(3) --- getter
有的组件中获取到 store 中的state, 需要对进行加工才能使用,computed 属性中就需要写操作函数,如果有多个组件中都需要进行这个操作,那么在各个组件中都写相同的函数,那就非常麻烦,这 ...
- VMWare虚拟机移动
1. 背景: 虚拟机:VM3 原安装路径:C:\Users\Administrator\Documents\Virtual Machines 移动到目标路径:D:\Virtual Machines ...
- AI:模式识别的数学表示(集合—函数观点)
前言: 模式识别的定义,参考:模式识别两种方法:知识和数据 .百科定义:模式识别(英语:Pattern Recognition),就是通过计算机用数学技术方法来研究模式的自动处理和判读.我们把环境与客 ...
- 安卓JNI使用C++类
安卓JNI使用C++类,同时可使用C++的类成员变量,这就必须保证程序持续保存Native状态,即长期维护C++对象的地址.完成初始化之后,需要使用对象成员的java层函数需要传入对象的地址. 一.N ...
- msmq消息队列使用场景
MSMQ全称是Microsoft Message Queue——微软消息队列. MSMQ是一种通信的机制,因为是一种中间件技术,所以它能够支持多种类型的语言开发,同时也是跨平台的通信机制,也就是说MQ ...
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子
- (转)基于MVC4+EasyUI的Web开发框架经验总结(10)--在Web界面上实现数据的导入和导出
http://www.cnblogs.com/wuhuacong/p/3873498.html 数据的导入导出,在很多系统里面都比较常见,这个导入导出的操作,在Winform里面比较容易实现,我曾经在 ...