链接:

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. Hibernate迫切左外连接和迫切内连接

    •迫切左外连接: •LEFT JOIN FETCH 关键字表示迫切左外连接检索策略. –list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee  ...

  2. docker 入門

    http://dockone.io/article/277 我的碎碎念:Docker入门指南 [编者的话]之前曾经翻译过很多Docker入门介绍的文章,之所以再翻译这篇,是因为Anders的角度很独特 ...

  3. console.log等不能打印全部数据/信息

    有时候console.log在chrome调试控制台打印不全,如下: 这个时候,我们可以点击进去:用watch 工具,添加变量,右击copy value 选项:

  4. Hive—学习笔记(一)

    主要内容: 1.Hive的基本工能机制和概念 2.hive的安装和基本使用 3.HQL 4.hive的脚本化运行使用方式 5.hive的基本语法--建表语法 6.hive的基本语法--内部表和外部表. ...

  5. django创建工程,用命令

    django创建工程的命令 >>python C:\Python33\Lib\site-packages\django\bin\django-admin.py startproject p ...

  6. oracle存储过程-获取错误信息

    dbms_output.put_line('code:' || sqlcode); dbms_output.put_line('errm:' || sqlerrm); dbms_output.put_ ...

  7. Hibernate分页查询的两个方法

    Hibernate中HQL查询语句有一个分页查询, session.setMaxResult(参数)和session.setFirstResult(参数) 如果只设置session.setMaxRes ...

  8. INI

    .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理 ...

  9. Ubuntu cd

    查看目录文件内容 ./  or filename/file.* cd 返回用户主目录 ~,,,,,/home/user cd ..不管用 cd / 返回用户根目录 root

  10. Swift 基本语法03-"if let"和"guard let"

    1. /// 如果JY_WINDOW有rootViewController, 并且rootViewController类型是AdvertisementViewController,就执行stopPla ...