http://poj.org/problem?id=1679

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30120   Accepted: 10778

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!

Source

 
 
蒟蒻就是弱、、、
 #include <algorithm>
#include <cstdio> using namespace std; const int N();
int num,n,m;
int fa[N],cnt;
int Fir,MST;
int u,v,w,used[N];
struct Edge
{
int u,v,w;
} edge[N<<]; bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
} int Kruskal()
{
int ans=; cnt=;
sort(edge+,edge+m+,cmp);
for(int i=; i<=n; i++) fa[i]=i;
for(int i=; i<=m; i++)
{
int fx=find(edge[i].u),fy=find(edge[i].v);
if(fx!=fy)
{
fa[fx]=fy;
used[++cnt]=i;
ans+=edge[i].w;
}
if(cnt==n-) return ans;
}
return ans;
} int SecKru(int cant)
{
int ans=; cnt=;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
if(cant==i) continue;
int fx=find(edge[i].u),fy=find(edge[i].v);
if(fx!=fy)
{
cnt++;
fa[fx]=fy;
ans+=edge[i].w;
}
if(cnt==n-) return ans;
}
return 0x7fffffff;
} int main()
{
scanf("%d",&num);
for(;num--;)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
Fir=Kruskal(); MST=0x7fffffff;
for(int i=;i<n;i++)
{
MST=min(SecKru(used[i]),MST);
}
if(Fir==MST)
printf("Not Unique!\n");
else printf("%d\n",Fir);
}
return ;
}

POJ——T1679 The Unique MST的更多相关文章

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

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

  2. poj 1679 The Unique MST

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

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

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

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

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

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

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

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

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

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

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

  8. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  9. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

随机推荐

  1. rabbitMQ学习笔记(三) 消息确认与公平调度消费者

    从本节开始称Sender为生产者 , Recv为消费者   一.消息确认 为了确保消息一定被消费者处理,rabbitMQ提供了消息确认功能,就是在消费者处理完任务之后,就给服务器一个回馈,服务器就会将 ...

  2. auto_ptr的使用和注意

    参考: http://www.cnblogs.com/qytan36/archive/2010/06/28/1766555.html

  3. POJ 3737

    第一道三分题,有模板 #define eps 10e-6 double cal(){}//计算题目所需要的值 while(l+eps<r) { m1=l+(r-l)/3; m2=r-(r-l)/ ...

  4. spring cloud 中Actuator不显示更多信息的处理方式

    spring cloud 中Actuator不显示更多信息的处理方式 直接咨询了周大立,他说 management.security.enabled = false 就可以了: 学习了:http:// ...

  5. Android中XML解析,保存的三种方法

    简单介绍 在Android开发中,关于XML解析有三种方式,各自是: SAX 基于事件的解析器.解析速度快.占用内存少.非常适合在Android移动设备中使用. DOM 在内存中以树形结构存放,因此检 ...

  6. 在Kali上安装打印机

    在Kali 2.0上安装打印机 最近在玩儿渗透测试,就把自己的办公电脑做成了Kali,可是发现办公室的网络打印机没办法正常使用,上网查了一下,把整个过程简单的记录一下,省的忘记了 1.安装cups a ...

  7. sklearn中的数据预处理----good!! 标准化 归一化 在何时使用

    RESCALING attribute data to values to scale the range in [0, 1] or [−1, 1] is useful for the optimiz ...

  8. 引入jquery.js和jquery-1.10.2.min.js 发生冲突解决办法

    <html><head></head><body><body><div id = "a">a</div ...

  9. 2017第34周复习Java总结

    从上周日开始对工作中遇到的Java相关的知识进行总结整理.先是回顾了Java关键字,重点说了static关键字的用法:修饰变量.程序块.内部类.方法.还静态导包:重点说了final关键字可以修饰类.方 ...

  10. 【转】Docker基础

    一.简介 Docker是一个开源的应用容器引擎,使用Go语言开发,基于Linux内核的CGroup.Namespace.Union FS等技术实现的一种系统级虚拟化技术. 特性 更高效的利用系统资源: ...