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 ...
随机推荐
- AngularJS学习手册
看书和视频结合是学习的最高效方式,看了这本书之后对angularjs才算是有一定的理解了.这本书以搭建一个博客为线索讲解了angularjs的知识点和实际项目开发流程.非常适合初学者!下面是我的读书笔 ...
- CSS3—三角形
话不多说看效果:演示效果,runjs 1.加了宽高和border,边用不同颜色显示,每条边都是一个梯形 2.去掉宽高,每条边都是三角形 3.只显示其中一条边就是不同的三角形了,是不是很简单,改变bor ...
- AngularJS 整理资料
AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足,其通过使用指令(directives)结构来扩展HTML词汇,使开发者可以使用HTML来 ...
- Linux 命令 - head: 打印文件的开头部分
命令格式 head [OPTION]... [FILE]... 命令参数 -c, --bytes=[-]K 显示每个文件的前 K 字节内容. -n, --lines=[-]K 显示每个文件的前 K 行 ...
- sql中的case when语句
1.在where子句中: CREATE TABLE `hello`.`sometbl` ( `id` INT NOT NULL AUTO_INCREMENT , `a` VARCHAR(45) NUL ...
- 和阿文一起学H5——H5工具、素材
字体: 一般的H5工具都会提供一部分字体,如果提供的字体中没有自己想用的字体,可以在PPT或PS中进行加工,然后另存为图片,再导入到H5工具中去. 字全生成器: 1.http://www.diyizi ...
- JSP之request对象
在请求转发时,我们需要把一些数据传递到转发后的页面进行处理.这时就需要使用request对象的setAttribute()方法将数据保存到request范围内的变量中. 示例:创建index.jsp文 ...
- 北大ACM(POJ1002-487-3279)
Question:http://poj.org/problem?id=1002问题点:字符映射.选重复项及排序. Memory: 1136K Time: 813MS Language: C++ Res ...
- lstm-思想
RNN(Recurrent Neural Network) 今天我这里讲到的RNN主要是上图这种结构的,即是Hidden Layer会有连向下一时间Hidden Layer的边,还有一种结构是Bidi ...
- SQLite的简单应用
安装部署 1)进入 SQL 下载页面:http://www.sqlite.org/download.html 2)下载预编译二进制文件包. Windows 环境的如下: 下载完之后,就算部署完成.(P ...