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 ...
随机推荐
- 将CSV格式的文件导入到数据中
--创建表 create table t1( id number primary key, name ), score number, subject ) ) --创建控制文件 t1.ctl,以文本的 ...
- 百度ueditor编辑器背景不显示问题
网友办法:http://www.hongxuejing.com/hulianwang/ueditor_not_show_background-33.html 官方办法:http://fex-team. ...
- 关于ThinkRock中的Topics
thinkrock是一款非常优秀的思想管理软件 主题是用来分类思想的,从而将思想具体化 比如:个人,书籍,小孩等等 在其中红色以及灰色是不推荐使用的,因为有别的意思.
- css笔记——关于 body/html 的高度百分比
body{ height:100%; 视窗的高度 min-height:100%;文档的具体高度} 这两个百分比的具体高度在页脚永远放在文档底部非常重要,此时用min-height:100% 具体 ...
- 线程间操作无效: 从不是创建控件“label4”的线程访问它。
//主线程 public delegate void UpdateMessage(string mes); public void UpdatePortMessage(string mes) { th ...
- OOP—还原被遮掩的继承名称
1.public继承——using 声明式 class Base { private: int x; public: ; virtual void mf1(int) ; virtual void mf ...
- 《java编程思想》--多线程基础--Runnable
一.简单说下Runnable是什么 1.它是一个接口 2.只提供了run方法 3.这个接口提供了一个协议:实现这个接口的类是active的(不必成为Thread的子类) 4.run方法没有返回值 /* ...
- 文档生产工具 Doxygen
Doxygen是一种开源跨平台的,类似JavaDoc风格描述的文档系统,支持C.C++.Java.Objective-C等语言.可以从一套归档源文件开始,生成HTML,XML,pdf等不同风格的格式. ...
- [CSS]下拉菜单
原理:先让下拉菜单隐藏,鼠标移到的时候在显示出来 1>display 无动画效果,图片是秒出 2>opacity 有动画效果,我这里是1S出现,推荐配合绝对定位使用
- java的CyclicBarrier
CyclicBarrier直译叫循环屏障,作用有点像赛跑时吹哨的角色,它有2个构造方法,一个是int的arg1,另一个多了一个Runable的arg2 arg1:可以看做此次参加赛跑的人数 arg2: ...