hdu 1679 The Unique MST (克鲁斯卡尔)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 24152 | Accepted: 8587 |
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
following m lines contains a 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!
题目链接:http://poj.org/problem?
id=1679
题目大意:n个点m条路。给出每条路以及边权。推断最小生成树是否是唯一的。
解题思路:克鲁斯卡尔,推断是否存在等效边。这题数据太弱了。我推断等效边的方法不太对,竟然过了= =
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int fa[102];
struct EG
{
int u,v,w;
}eg[5005];
void get_fa()
{
for(int i=0;i<105;i++)
fa[i]=i;
}
int find (int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int a,int b)
{
int a1=find(a);
int b1=find(b);
if(a1!=b1)
fa[a1]=b1;
}
int cmp(EG a,EG b)
{
return a.w<b.w;
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,ans=0,p=0,cnt=0;
get_fa();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&eg[i].u,&eg[i].v,&eg[i].w);
}
sort(eg,eg+m,cmp);
for(int i=0;i<m;i++)
{
if(find(eg[i].u)!=find(eg[i].v))//假设当前边须要增加且下一条边也须要增加且它们权值相等即为等效边
{
if(i+1<m&&find(eg[i+1].u)!=find(eg[i+1].v)&&eg[i].w==eg[i+1].w)
{
p=1;
break;
}
Union(eg[i].u,eg[i].v);
ans+=eg[i].w;
cnt++;
}
if(cnt>=n)
break;
}
if(!p)
printf("%d\n",ans );
else
printf("Not Unique!\n");
}
}
hdu 1679 The Unique MST (克鲁斯卡尔)的更多相关文章
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- 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 【次小生成树】【模板】
题目: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 S ...
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...
- hdu 1233 还是畅通project (克鲁斯卡尔裸题)
还是畅通project Time Limit: 4000/2000 MS (Java/Others) M ...
- 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 (最小生成树)
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: 24034 Accepted: 8535 D ...
随机推荐
- Processed foods make us fatter easily
From Business Insider Here's an experiment: sit alone in a hospital room for two weeks and eat nothi ...
- (转))iOS App上架AppStore 会遇到的坑
iOS App上架AppStore 会遇到的坑 前言:非原创 文章摘自:http://zhuanlan.zhihu.com/100000PM/20010725 相信大家一定非常「深恶痛疾」AppS ...
- python基础学习笔记——列表技巧
列表: 循环删除列表中的每⼀个元素 li = [, , , ] for e in li: li.remove(e) print(li) 结果: [, ] 分析原因: for的运⾏过程. 会有⼀个指针来 ...
- jmx_exportter+prometheus+grafana监控hadoop
0.介绍(摘录自https://www.hi-linux.com/posts/25047.html) 什么是Prometheus? Prometheus是由SoundCloud开发的开源监控报警系统和 ...
- unittest断言方法的使用
unittest框架的TestCase类提供以下方法用于测试结果的判断 方法 检查 版本 assertEqual(a, b) a ==b assertNotEqual(a, b) a !=b ...
- Leetcode 375.猜数字大小II
猜数字大小II 我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字. 每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了. 然而,当你猜了数字 x ...
- php5.3.3版本前后变化中php-v和sbin/php-fpm -v
重装php-fpm试试,遂去http://php-fpm.org/download/想下载个新版本的php-fpm, 结果发现版本大于5.3.3的PHP内部已经集成了php-fpm,不用再另行安装了. ...
- git 本地保存账号密码
用ssh连接的项目都不用输账号密码 如果https的话 每次都用输入账号密码 很繁琐 解决方法,在本地的工程文件夹的.git下打开config文件添加: [credential] he ...
- Codeforces #765D
我在这道题上花了2个小时,仍没解出.理一下当时的思路,看看症结到底在哪里. 题意 用 $[n]$ 表示集合 $\{1,2,3,\dots, n\}$ . 3个函数 $f \colon [n] \to ...
- apache kafka系列之客户端开发-java
1.依赖包 <dependency> <groupId>org.apache.kafka</groupId> <a ...