The Unique MST

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 8
Problem 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
PKU


有时候最小生成树不止一个,举个例子,某一个点以min进入当前的集合,但是进去之后又发现他与好几个在集合中的点的距离都是min,这也就说明,他连哪一个点都是一样的,所以此时的最小生成树不止一个。代码:


#include<stdio.h>
#include<string.h>
#define INF 0xfffffff
int n,m;
int map[1010][1010],dis[1010],mark[1010];
void prim()
{
int i,min,flag,j,sum=0,k,w=1;
memset(mark,0,sizeof(mark));
mark[1]=1;
for(i=2;i<=n;i++)
{
min=INF;
flag=-1;
k=0;
for(j=1;j<=n;j++)
{
if(!mark[j]&&map[1][j]<min)
{
min=map[1][j];
flag=j;
}
}
for(j=1;j<=n;j++)
{
if(mark[j]&&map[flag][j]==min)
{/*因为mark[1]已经标记过了,所以此时如果出现两个及其以上的点,
到flog的距离都是min,当然,仅限于已经连过的点,此时最小生成树就不止一种*/
k++;
}
}
//printf("%d ",k);
if(k>=1)
{
w=0;
break;
}
mark[flag]=1;
sum+=min;
for(j=1;j<=n;j++)
{
if(!mark[j]&&map[1][j]>map[flag][j])
map[1][j]=map[flag][j];
}
}
if(w)
printf("%d\n",sum);
else
printf("Not Unique!\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j;
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
map[i][j]=INF;
//map[i][i]=0;
}
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
map[a][b]=map[b][a]=c;
}
prim();
}
}

The Unique MST--hdoj的更多相关文章

  1. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  2. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  3. POJ1679The Unique MST(次小生成树)

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

  4. poj 1679 The Unique MST

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

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

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

  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(唯一的最小生成树)

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

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

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

  9. The Unique MST (判断是否存在多个最小生成树)

    The Unique MST                                                                        Time Limit: 10 ...

  10. POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

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

随机推荐

  1. windows phone控件

    常用控件: 包括: Button控件.CheckBox控件.HyperlinkButton控件.Iamege控件.ListBox控件.PasswordBox控件.ProgressBar控件.Radio ...

  2. 【PostgreSQL-9.6.3】如何实现非自动提交

    我们在使用psql工具操作数据库时,事务是自动提交的.也就是说,当我们执行完一条insert或者delete语句后,在不输入commit情况下,这条语句也是提交的.如果不想自动提交,可以使用以下两种方 ...

  3. sql server数据库可疑状态解决方法

    前段时间客户数据服务器断电,开机后发现数据库状态标记为可疑,可能是断电引起的数据库日志文件损坏,修复方法如下: 只有mdf文件,重建日志: --注:example为测试用数据库,相应的Example_ ...

  4. SiftGPU:编译SiftGPU出现问题-无法解析的外部符号 glutInit

    OpenCV出现了ORB特征和SURF的GPU版本, 参考:opencv上gpu版surf特征点与orb特征点提取及匹配实例至于使用什么并行API暂时没有探究. 但没有发现OpenCV-SIFT的GP ...

  5. Callback-回调-回呼

    很早以前看<Delphi 4从入门到精通>有这么一个概念——CallBack.然后在<Delphi 6从入门到精通>看同样的章节,翻译为“回调”,就有一个疑问了,什么是Call ...

  6. 浏览器 HTTP 协议缓存机制详解--网络缓存决策机制流程图

    1.缓存的分类 2.浏览器缓存机制详解 2.1 HTML Meta标签控制缓存 2.2 HTTP头信息控制缓存 2.2.1 浏览器请求流程 2.2.2 几个重要概念解释 3.用户行为与缓存 4.Ref ...

  7. vs2008 打开项目 无法读取项目文件

    卸载vs2015之后 出现问题 C:\Windows\SysWOW64\regedit.exe 64系统运行这个 删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MS ...

  8. Jmeter JSON断言和响应断言的区别是什么?

    假设响应数据是{"code":0,"datas":{"informationStatus":1}} 响应断言:"code" ...

  9. node、Mongo项目如何前后端分离提供接口给前端

    node接口编写,vue-cli代理接口方法  通常前端使用的MocK 数据的方法,去模拟假的数据,但是如果有node Mongodb 去写数据的话就不需要在去mock 数据了,具体的方法如下. 首先 ...

  10. 【转载】Jmeter分布式部署测试-----远程连接多台电脑做压力性能测试

    在使用Jmeter进行接口的性能测试时,由于Jmeter 是JAVA应用,对于CPU和内存的消耗比较大,所以,当需要模拟数以万计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至会引起J ...