链接:

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. Jquery和Ajax

    jQuery 是一个 JavaScript 函数库.JavaScript 是 HTML5 以及所有现代浏览器中的默认脚本语言! jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 ...

  2. MySql PartionBy

    SELECT tableOld.*, if(@channelName = tableOld.channelName, @rank := @rank + 1, @rank := 1) AS rank, ...

  3. 如何勾选 servlet如何获取?

    1.jsp中checkbox <form action="Test"> <% for(int i = 0 ; i < 10 ; i++){ %> &l ...

  4. vim使用方法:

    vim使用方法: 模式: 编辑模式.未编辑模式.命令行模式 i 插入形式进入编辑模式 a 增加 o 下行编辑 O 上行插入 : 进入命令行模式 esc 退出编辑模式 wq 保存文件 yy 复制 p 粘 ...

  5. 第八章 高级搜索树 (b3)B-树:查找

  6. Eclipse 中 program arguments 与 VM arguments 的区别

    1. program arguments 中的值作为 主函数中的参数args[] 传入 2. VM Arguments 是设置的java虚拟机的属性,这些系统属性都以-D开头, VM argument ...

  7. [leetcode]347. Top K Frequent Elements 最高频的前K个元素

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  8. xp安装vmware10时一直停在installing packages on the system

    我估计的原因是: vm会在网上邻居(LAN或高速internet)下创建两个 vmware network adapter vmnet8 vmware network adapter vmnet1   ...

  9. Mina入门:mina版之HelloWorld[z]

    Mina入门:mina版之HelloWorld [z] 一,前言: 在完成上篇文章<Mina入门:Java NIO框架Mina.Netty.Grizzly简介与对比>之后,我们现在可以正式 ...

  10. Halcon的编程语法与数据处理——第8讲

    1.跟其他语言不完全一致的表达符号 赋值符号  := 引号      ' ' (一律是单引号) 求商求余  /   % (一个整数除以另一个数,如何使商是实型的?即浮点型) 逻辑运算  and  or ...