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. apache 与 nginx 详解

    Apache与Nginx的优缺点比较 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apac ...

  2. 项目工程的包package与文件夹的关系

    项目工程的包package与文件夹的关系: 1. 包名与文件夹是分层关系,包名只是一个字符串而已,包名.对应的是层级的文件夹. 如,com.Immoc.Access包,只是一个字符串.但他对应的win ...

  3. HDU 1215

    由算术基本定理, 直接使用公式就好 #include <iostream> #include <cstdio> #include <algorithm> #incl ...

  4. mongodb后台执行

    默认的情况下,关闭shell,mongodb就停止执行了. 假设想在后台执行,启动时仅仅需加入 --fork函数就可以. 能够在日志路径后面加入--logappend.防止日志被删除. bin/mon ...

  5. void型指针

    void型指针,表示这个指针指向的内存中的数据的类型要由用户来指定. 比方内存分配函数malloc函数返回的指针就是void *型. 用户在使用这个指针的时候.要进行强制类型转换,也就是显式说明该指针 ...

  6. MMORPG大型游戏设计与开发(server 游戏场景 事件)

    游戏截图 场景事件 一个完整的对象一般都拥有事件,至于什么是事件在这里就不多解释了. 在场景中的事件在天龙/武侠世界中的事件包含场景初始化.场景定时器.场景退出.玩家进入场景.角色升级.角色死亡.角色 ...

  7. unknown argument: &#39;-websockets&#39;

    找到building setting找到other link flgs里把里面'-websockets删掉

  8. mybatis使用generator自己主动生成代码时的类型转换

    使用mybatis的generator自己主动生成代码,可是oracle数据库中number(6,2)总是自己主动转成BigDecimal.我想要转成的是float类型 这样就写了一个类型转换器,须要 ...

  9. 8.MATLAB数据分析

    概述: clc; clear all; p1=[ ]; y=poly2sym(p1) %由向量创建多项式 disp(y) %显示多项式 1 多项式的求值与求根 clc; clear all; p=[ ...

  10. C#调用mmpeg进行各种视频转换的类实例

    本文实例讲述了C#调用mmpeg进行各种视频转换的类.分享给大家供大家参考.具体如下: 这个C#类封装了视频转换所需的各种方法,基本上是围绕着如何通过mmpeg工具来进行视频转换 using Syst ...