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. 三段式fsm

    1.状态转移的always中CS,同步ouput的always中NS. 2.3段fsm vs 2段fsm:output逻辑是组合逻辑和同步时序逻辑(消除里不稳的和毛刺). 3.3段fsm vs 1段f ...

  2. 安装tesserocr的步骤和报错RuntimeError: Failed to init API, possibly an invalid tessdata path解决办法

    1,首先下载合适的tesseract-ocr的版本 2,然后安装到这一步注意要勾选这一项来安装OCR识别支持的语言包,这样OCR就可以识别多国语言,然后就可以一直点击下一步完成安装. 3,安装tess ...

  3. PAT Basic 1052

    1052 卖个萌 萌萌哒表情符号通常由“手”.“眼”.“口”三个主要部分组成.简单起见,我们假设一个表情符号是按下列格式输出的: [左手]([左眼][口][右眼])[右手] 现给出可选用的符号集合,请 ...

  4. Impala的分布式查询

    翻译自<Getting Started with Impala> 分布式查询 分布式查询是impala的核心.曾几何时,你需要研究并行计算,才能开始进行深奥而晦涩的操作.现在,有运行在Ha ...

  5. iOS 绕过https证书验证 请求数据

    HTTPS和HTTP: 1.https协议需要到ca申请证书,一般免费证书很少,需要交费. 2.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议. 3.http ...

  6. Wordpress无法上传图片

    当在自己新搭建的个人网站添加文章的时候,我遇到了wordpress最普遍的第一个问题——无法上传图片.     每次图片上传完成之后跳出如上图无法建立目录的警告,根据提示首先确定是否有修改文件的权限通 ...

  7. BNUOJ 6727 Bone Collector

    Bone Collector Time Limit: 1000ms Memory Limit: 32768KB   This problem will be judged on HDU. Origin ...

  8. zoj 2679 Old Bill

    Old Bill Time Limit: 2 Seconds      Memory Limit: 65536 KB Among grandfather��s papers a bill was fo ...

  9. [android开发篇]权限分类:正常权限和危险权限

    https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous 系统权限 本文内容 安全架构 ...

  10. 【管理】个人主义—>集体主义

    导读:这个月作为学术部负责人,暴露了很多问题,个人的,集体的!我需要思考的,有很多.现在,我反思图书馆丢书这件事的处理方案:我虽然站在了管理层,却做着员工干的事儿.以一种个人主义.英雄主义去做事儿.却 ...