The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24152   Accepted: 8587

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.



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

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the
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

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

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 (克鲁斯卡尔)的更多相关文章

  1. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  2. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  3. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  4. poj 1679 The Unique MST (判定最小生成树是否唯一)

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  5. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

  6. hdu 1233 还是畅通project (克鲁斯卡尔裸题)

    还是畅通project                                              Time Limit: 4000/2000 MS (Java/Others)    M ...

  7. POJ 1679 The Unique MST (最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  8. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  9. poj 1679 The Unique MST【次小生成树】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24034   Accepted: 8535 D ...

随机推荐

  1. css实现盒尺寸重置、均匀分布的子元素、截断文本

    盒尺寸重置 重置盒子模型,以便width s和height s并没有受到border 还是padding他们的影响 . CSS文字折断 css实现盒尺寸重置.均匀分布的子元素.截断文本 如何对多行文本 ...

  2. 三、Pandas速查手册中文版

    本文翻译自文章:Pandas Cheat Sheet - Python for Data Science,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重 ...

  3. nrf52810学习笔记——三

    在开发nRF52系列的蓝牙方案的时候,会用到IDE.SDK.softdevice.nrfgoStudio等开发软件,这里做一个小小的总结. 首先,下载SDK,里面有适合keil4号iar7(iar8也 ...

  4. Codeforces Round #439 (Div. 2) A. The Artful Expedient

    A. The Artful Expedient 题目链接http://codeforces.com/contest/869/problem/A 解题心得:就是一个水题,读懂题就好,题意是,(i,j)a ...

  5. python常见陷阱

    copy to https://pythonguidecn.readthedocs.io/zh/latest/writing/gotchas.html 大多数情况下,Python的目标是成为一门简洁和 ...

  6. Installing pip on CentOS 7 for Python

    nstalling pip on CentOS 7 for Python 2.x On CentOS 7, you have to install setup tools first, and the ...

  7. JavaScript onload

     The onload event occurs immediately after a page or an image is loaded.onload事件当一个页面或是一张图片加载完成时被触发. ...

  8. BZOJ 2190 [SDOI2008]仪仗队 ——Dirichlet积

    [题目分析] 考虑斜率为0和斜率不存在的两条线上只能看到3人. 其余的人能被看见,当且仅当gcd(x,y)=1 ,然后拿卷积算一算 发现就是欧拉函数的前缀和的二倍. 注意2的情况要特判. [代码] # ...

  9. Codeforces956D. Contact ATC

    $n \leq 100000$个飞机在坐标轴上,给坐标给速度,坐标速度异号,还有一个风速在$[-w,w]$区间,$w$比最小的速度绝对值要小.由于风速不知道,所以问有多少对飞机可能在原点相遇. 思维定 ...

  10. J2ME开发入门

    原文发布时间为:2008-07-31 -- 来源于本人的百度文章 [由搬家工具导入] J2ME开发入门J2ME方面开发的资料,确实是少之又少,一般给新手推荐的都是王森先生的《PDA与手机开发入门》一书 ...