poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 17726 | Accepted: 6150 |
Description
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
Output
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
/**
Judge Status:Accepted Memory:748K
Time:32MS Language:G++
Code Lenght:1814B Author:cj
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector> #define N 101
#define M 6000
using namespace std; struct Nod
{
int x,y,w;
}node[M]; int parent[N];
int n,m;
vector<int> vct; bool cmp(Nod a,Nod b)
{
return a.w<b.w;
} int findp(int a)
{
while(a!=parent[a])
{
a=parent[a];
}
return a;
} int merge(Nod nd) //合并
{
int x = findp(nd.x);
int y = findp(nd.y);
if(x>y)
{
parent[y]=x;
return nd.w;
}
else if(x<y)
{
parent[x]=y;
return nd.w;
}
return -;
} int kruskal(int id)
{
int i,sum=,cnt=;
for(i=;i<=n;i++) parent[i]=i;
for(i=;i<m;i++)
{
if(i!=id)
{
int temp = merge(node[i]);
if(temp!=-)
{
sum+=temp;
cnt++; //剪枝
if(id==-) vct.push_back(i); //保存第一次最小生成树的各个节点
}
if(cnt>=n-) //找到n-1条边即可以跳出了
break;
}
}
cnt = ;
for(i=;i<=n;i++) //判断是不是构成一棵树
if(parent[i]==i)
cnt++;
if(cnt==) //是
return sum;
if(id==-) //否
return ;
return -;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int i;
for(i=;i<m;i++)
{
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].w);
}
vct.clear();
sort(node,node+m,cmp);
int mins = kruskal(-); //找到第一颗最小生成树
int temp=-;
for(i=;i<vct.size();i++)
{
temp = kruskal(vct[i]); //每次去掉一个节点 再判断是否可以组成最小生成树
if(mins==temp)
break;
}
if(temp==mins) puts("Not Unique!");
else printf("%d\n",mins);
}
return ;
}
poj 1679 The Unique MST(唯一的最小生成树)的更多相关文章
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...
- POJ 1679 The Unique MST 【判断最小生成树是否唯一】
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Defini ...
- POJ 1679 The Unique MST(推断最小生成树_Kruskal)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Defini ...
- 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
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
随机推荐
- OSI七层模型:TCP/IP && HTTP && WebSocket && MQTT
OSI七层模型分为 物理层: 建立.维护.断开物理连接 处理bit流 数据链路层,将比特组合成字节进而组合成帧,用MAC地址访问介质,错误发现但不能纠正 处理数据帧 Frame 网络层,进行逻辑地址 ...
- 单击改变input的边框颜色
input边框变色 今天布局页面的时候发现设计稿多了一项要求,就是点击input框的时候要求框框变色,以前没有遇到过,平时很多时候都用的:hover这次想着一样吧,看了之后发现不是想要的效果,ho ...
- HttpClient(4.3.5) - HTTP Authentication
HttpClient provides full support for authentication schemes defined by the HTTP standard specificati ...
- asp中utf8不会出现乱码的写法
<%@ CODEPAGE=65001 %> <% Response.CodePage=65001%> <% Response.Charset="UTF-8&qu ...
- 让.NET程序会说话
在开发过程中需要用到让程序自动播放语音,如果是一个一个录则太麻烦了,在开发过程中发现.NET已经自带了该功能 Type type = Type.GetTypeFromProgID("SAPI ...
- iOS - 文件与数据(File & Data)
01 推出系统前的时间处理 --- 实现监听和处理程序退出事件的功能 //视图已经加载过时调用 - (void)viewDidLoad { [super viewDidLoad]; // Do any ...
- spring中jdbc的配置
本文中的JdbcTemplate的用法可以参看官方文档http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-referenc ...
- HTML5新增标签的汇总与详解
趁着一点闲暇时间,把HTML5的新增标签整理了一下,用了表格的形式展现,分别归纳了各标签的用法及属性分析.这样方便各位以后在运用HTML5标记遇到疑惑时,直接上来对照看下就明了了,希望对大家有帮助哦. ...
- python(四)数据持久化操作 文件存储
1.写入 导入pickle包 然后组织一个列表my_list,保存为pkl格式,可以是任意格式 在磁盘下回出现一个保存的文件 2.读取
- 使用Hibernate 拦截执行sql语句,并输出sql语句,获取sql语句
重建包名 org.hibernate.type.descriptor.sql 重建类BasicBinder 代码如下 package org.hibernate.type.descriptor.sql ...