链接:

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

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/K

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24594   Accepted: 8751

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!

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0x3f3f3f3f; int J[N][N], dist[N], pre[N], Max[N][N], n, m;
int use[N][N];
bool vis[N]; int prim()
{
int i, j, ans=;
memset(Max, , sizeof(Max));
memset(use, , sizeof(use));
memset(dist, , sizeof(dist));
memset(vis, false, sizeof(vis));
vis[]=; for(i=; i<=n; i++)
{
dist[i]=J[][i];
pre[i]=;
} for(i=; i<n; i++)
{
int index=, MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
MIN=dist[j];
index=j;
}
}
ans += MIN;
vis[index]=;
use[index][pre[index]]=use[pre[index]][index]=; for(j=; j<=n; j++)
{
if(vis[j] && j!=index)
{
Max[index][j]=Max[j][index]=max(Max[j][pre[index]], dist[index]);
}
if(!vis[j] && dist[j]>J[index][j])
{
dist[j]=J[index][j];
pre[j]=index;
}
}
}
return ans;
} int cc(int s)
{
int MIN=INF, i, j;
for(i=; i<=n; i++)
for(j=i+; j<=n; j++)
{
if(!use[i][j] && J[i][j]!=INF)
{
MIN = min(MIN, s-Max[i][j]+J[i][j]);
}
}
return MIN;
} int main ()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, j, a, b, c;
scanf("%d%d", &n, &m); for(i=; i<=n; i++)
{
J[i][i]=;
for(j=; j<i; j++)
J[i][j]=J[j][i]=INF;
} for(i=; i<=m; i++)
{
scanf("%d%d%d", &a, &b, &c);
J[a][b]=J[b][a]=c;
} int ans1=prim();
int ans2=cc(ans1);
if(ans1==ans2)
printf("Not Unique!\n");
else
printf("%d\n", ans1);
}
return ;
}

(最小生成树 次小生成树)The Unique MST -- POJ -- 1679的更多相关文章

  1. The Unique MST POJ - 1679 (次小生成树)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  2. The Unique MST POJ - 1679 次小生成树prim

    求次小生成树思路: 先把最小生成树求出来  用一个Max[i][j] 数组把  i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过   把没有使用过的一条边加 ...

  3. 次小生成树 判断 unique MST

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  4. K - The Unique MST - poj 1679

    题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...

  5. Day5 - G - The Unique MST POJ - 1679

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  6. The Unique MST POJ - 1679 最小生成树判重

    题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...

  7. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

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

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

  9. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树

    题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...

随机推荐

  1. STS或eclipse安装SVN插件

    安装sts--SVN插件 简介:sts是与eclipse类似的Java IDE开发工具(不了解的百度) 1.sts菜单栏 help->install New Software 依据大家的版本选择 ...

  2. ArcGIS案例学习笔记3_1

    ArcGIS案例学习笔记3_1 联系方式:谢老师,135_4855_4328,xiexiaokui#139.com 时间:第三天上午 内容1:ArcGIS 平台介绍 体系结构 Arcgis for d ...

  3. pandas 语句

    1.对于时间格式数据的处理 有些时候time_stamp是object格式,提取相应的日期,小时,星期等: 方法1  from datetime import datetime  user_trad[ ...

  4. 常用特殊符号的HTML代码(HTML字符实体)

    适当使用实体,对页面开发有相当大的帮助. 自己收集的一些常用的以实体代替与HTML语法相同的字符,避免浏览解析错误. 常用HTML字符实体(建议使用实体): 字符 名称 实体名 实体数 • 圆点   ...

  5. iOS - OC - 打印信息 - xcode 中文打印

    #import <Foundation/Foundation.h> @implementation NSDictionary (Log) //重写系统的方法控制输出 -(NSString ...

  6. HTML实例

    HTML内容繁多,不易记忆,故将此网址 作为查阅复习的工具http://www.w3school.com.cn/example/html_examples.asp

  7. win 下 apache 虚拟主机配置方式

    虚拟主机的配置在apache安装目录下/conf/extra/httpd-vhosts.conf文件中,需要在/conf/httpd.conf中开启. LoadModule vhost_alias_m ...

  8. js手机号码正则表达式

    function checkMobile(){ var sMobile = document.mobileform.mobile.value if(!(/^1[3|4|5|8][0-9]\d{4,8} ...

  9. mvcpager 表单提交时无法获取pageindex的值

    1.将分布页包含到form中 2.在分布页中新增一个表单hidden元素,name为pageIndex(与控制器中的pageindex保持一尺),将后台获取到的pageindex值通过viewbag初 ...

  10. Django基础教程

    实例练习1-提交数据并展示 1.app_01下的views.py info_list=[] def userInfor(req): if req.method=="POST": u ...