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. JavaScript中字符串运算符的使用

    字符串运算符是用于两个字符串型数据之间的运算符,它的作用是将两个字符串连接起来.在JavaScript中,可以使用+和+=运算符对两个字符串进行连接运算.其中,+运算符用于连接两个字符串,而+=运算符 ...

  2. java编程基础篇---------> 编写一个程序,从键盘输入三个整数,求三个整数中的最小值。

    编写一个程序,从键盘输入三个整数,求三个整数中的最小值. 关键:声明变量temp   与各数值比较. package Exam01; import java.util.Scanner; public ...

  3. 使用cookies查询商品详情

    易买网项目完工,把一些新知识记录下来,以便以后查阅,也方便他人借阅.介绍使用cookies查询商品详情. 第一步:建立商品实体类. 第二步:连接Oracle数据库. 第三步:使用三层架构. 效果图如下 ...

  4. 【sqli-labs】 less16 POST - Blind- Boolian/Time Based - Double quotes (基于bool型/时间延迟的双引号POST型盲注)

    ' or 1=1# -->失败 1" or 1=1# -->失败 1') or 1=1# -->失败 1") or 1=1# -->成功 判断为双引号变形注 ...

  5. matlab学习菜单控件的基本用法

    编辑菜单项 上下文菜单,即弹出菜单 然后添加一个坐标系 添加回调函数 %余弦曲线x=0:0.01:2*pi;y=cos(x);axes(handles.axes1);%将坐标系的值写进h=plot(x ...

  6. 【转载】JSP详解(四大作用域九大内置对象等)

    前面讲解了Servlet,了解了Servlet的继承结构,生命周期等,并且在其中的ServletConfig和ServletContext对象有了一些比较详细的了解,但是我们会发现在Servlet中编 ...

  7. PAT_A1143#Lowest Common Ancestor

    Source: PAT A1143 Lowest Common Ancestor (30 分) Description: The lowest common ancestor (LCA) of two ...

  8. Hbuider sass配置 webstorm scss配置

    --no-cache %FileName% ../css/%FileBaseName%.css   sass编译后保存到css目录下 webstorm scss配置 C:\Ruby22-x64\bin ...

  9. 使用for或while循环来处理处理不确定页数的网页数据爬取

    本文转载自以下网站: Python For 和 While 循环爬取不确定页数的网页  https://www.makcyun.top/web_scraping_withpython16.html 需 ...

  10. java中的replaceAll方法注意事项

    replaceAll和replace方法参数是不同的,replace的两个参数都是代表字符串,replaceAll的第一个参数是正则表达式 replaceAll中需要注意的特殊字符: \ == \\\ ...