POJ - 1679_The Unique MST
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
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:
- V' = V.
- 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 <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int maxn = 105;
const int INF = 1e9+7;
int s[maxn][maxn],n,m;
int prim()
{
int i,j,MIN,k,sum = 0;
int dis[maxn],f[maxn],pre[maxn],maxx[maxn][maxn];/*maxx用来存储最小生成树的 i 到 j 的最大边权值*/
int use[maxn][maxn];
for(i=1;i<=n;i++)
{
dis[i] = s[1][i];
pre[i] = 1;
f[i] = 0;
}
memset(maxx,0,sizeof(maxx));
memset(use,0,sizeof(use));
f[1] = 1;
for(i=1;i<n;i++)
{
MIN = INF;
k = -1;
for(j=1;j<=n;j++)
if(!f[j]&&MIN>dis[j])
{
MIN = dis[j];
k = j;
}
if(MIN==INF)
return -1;
//printf("MIN %d\n",MIN);
f[k] = 1;
sum += MIN;
for(j=1;j<=n;j++)
{
if(f[j])
maxx[k][j] = maxx[j][k] = max(maxx[pre[k]][j],dis[k]);/*(发现直接等于dis[k]也行)*/
else
{
if(dis[j]>s[k][j])
{
dis[j] = s[k][j];
pre[j] = k;
}
}
}
}
//printf("%d\n",sum);
MIN = INF;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(i!=pre[j]&&j!=pre[i]&&s[i][j]!=INF)
MIN = min(MIN,sum-maxx[i][j]+s[i][j]);
}
}
//printf("%d\n",MIN);
if(MIN==sum)
return -1;
else
return sum;
}
int main()
{
int t,i,j,a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
s[i][j] = i==j?0:INF;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(s[a][b]>c)
s[a][b] = s[b][a] = c;
}
a = prim();
if(a==-1)
printf("Not Unique!\n");
else
printf("%d\n",a);
}
return 0;
}
POJ - 1679_The Unique MST的更多相关文章
- POJ 1679The Unique MST
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- POJ 1679:The Unique MST(次小生成树&&Kruskal)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19941 Accepted: 6999 D ...
随机推荐
- MyBatis框架的文件配置
第一步:log4j.properties的配置 原因:Mybatis的日志输出是依赖与log4j的,所以必须要配置 # Global logging configuration log4j.rootL ...
- HDFS应用实例
- Codeforces 360A(找性质)
反思 写一写可以发现上限不断更新 一直在想怎么判断NO,刻板拘泥于错误的模型,想要像往常一样贪心地.读入当前值就能判断会不会NO,实际上只要构造完以后,最后把所有操作重新跑一遍看会不会冲突即可判断NO ...
- bzoj 3110 [Zjoi2013]K大数查询——线段树套线段树(标记永久化)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3110 第一道线段树套线段树! 第一道标记永久化! 为什么为什么写了两个半小时啊…… 本想线段 ...
- Windows Apache httpd-vhosts.conf
<VirtualHost *:> DocumentRoot "D:\wamp\www" ServerName localhost </VirtualHost> ...
- Windows下Git的下载、安装、设置用户名和邮箱、创建版本库等
Git官网:https://git-scm.com/ 一.Git下载 官网首页下载,当前最新版本:2.24.1 本人下载的是Git for Windows版本:Git-2.24.1.2-64-bit. ...
- CentOS安装手册
CentOS6.5在VMware10中安装 1.启动VMware的画面 2.点击File--->New Virtual Machine 创建一台新虚拟机 3.在弹出框中选择典型安装 4.选择I ...
- git操作github指令
常用git命令: $ git clone //本地如果无远程代码,先做这步,不然就忽略 $ cd //定位到你blog的目录下 $ git status //查看本地自己修改了多少文件 $ git ...
- xmlns详解(转载)
我们经常会在网页中碰到形如<html xmlns=”http://www.w3.org/2001/xhtml”>这样的代码, 或在是android 编码中的main.xml中看到形如< ...
- 一个不错的插件(软件).NET开发
http://www.gcpowertools.com.cn/products/default.htm 葡萄城 先记录一下!