[poj1679]The Unique MST(最小生成树)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28207 | Accepted: 10073 |
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
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<cmath>
- #include<climits>
- #include<algorithm>
- #include<queue>
- #define LL long long
- using namespace std;
- typedef struct{
- int to,frm,dis;
- }edge;
- edge gra[];
- int num=,fa[];
- int n,m;
- int cmp(const edge &a,const edge &b){
- return a.dis<b.dis;
- }
- int fnd(int x){
- return x==fa[x]?x:fnd(fa[x]);
- }
- int uni(int x,int y){
- int fx=fnd(x);
- int fy=fnd(y);
- fa[fy]=fx;
- return ;
- }
- inline int read(){
- int sum=;char ch=getchar();
- while(ch>''||ch<'')ch=getchar();
- while(ch<=''&&ch>=''){
- sum=sum*+ch-'';
- ch=getchar();
- }
- return sum;
- }
- int kru(){
- int ans=;
- sort(gra+,gra+m+,cmp);
- for(int i=;i<=n;i++)fa[i]=i;
- for(int i=;i<=m;i++){
- int x=gra[i].frm;
- int y=gra[i].to;
- int fx=fnd(x);
- int fy=fnd(y);
- if(fx!=fy){
- int j=i+;
- while(j<=m&&gra[j].dis==gra[i].dis){
- int y1=gra[j].frm;
- int x1=gra[j].to;
- int fy1=fnd(y1);
- int fx1=fnd(x1);
- if((fx1==fx&&fy1==fy)||(fx1==fy&&fy1==fx))return -;
- j++;
- }
- ans+=gra[i].dis;
- uni(fx,fy);
- }
- }
- return ans;
- }
- int main(){
- int t;
- t=read();
- while(t--){
- memset(gra,,sizeof(gra));
- n=read(),m=read();
- num=;
- for(int i=;i<=m;i++){
- gra[i].frm=read();
- gra[i].to=read();
- gra[i].dis=read();
- }
- int ans=kru();
- if(ans==-)printf("Not Unique!\n");
- else printf("%d\n",ans);
- }
- return ;
- }
[poj1679]The Unique MST(最小生成树)的更多相关文章
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- POJ1679 The Unique MST 【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20421 Accepted: 7183 D ...
- POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29902 Accepted: 10697 ...
- POJ1679 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 Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- poj1679 The Unique MST(判定次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23180 Accepted: 8235 D ...
- POJ-1679.The Unique MST.(Prim求次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39561 Accepted: 14444 ...
随机推荐
- rhel 7.0 配置centos yum源(2016/12/8),成功!
1.首先查看redhat 7.0系统本身所安装的那些yum 软件包: rpm -qa | grep yum #列出所有已安装的yum包 2.删除这些包: rpm -e *.rpm --nodeps # ...
- PHP自带防SQL攻击函数区别
为了防止SQL注入攻击,PHP自带一个功能可以对输入的字符串进行处理,可以在较底层对输入进行安全上的初步处理,也即Magic Quotes.(php.ini magic_quotes_gpc).如果m ...
- 全国城市三级联动 html+js
全国城市三级联动,没有css,所以屏幕的自适应必须自己想办法,手机端慎用(最好不要用,因为有些我也说不出的展示问题). html页面 <!DOCTYPE html> <html> ...
- python 在最后一行追加
2.文本文件的写入 import fileinput file = open("D:\\test.txt", encoding="utf-8",mode=&qu ...
- spring cloud 学习研究- spring-cloud-microservice-example
spring cloud + docker 微服务架构 http://www.open-open.com/lib/view/open1437363835818.html 实例项目 https://gi ...
- 赶时髦过了一遍Swift 语言....
Swift 语言 2014年6月3日发布,替代OBJECT-C Swift is a new programming language for creating iOS and OS X apps. ...
- Lucene热词显示并选择
利用Jquery easyui里的autocomplete(1.10.0版本) 的异步请求(remot.html) 添加引用 <script src="~/Scripts/jquery ...
- javascript 使用方法名作为参数
Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="u ...
- 怎么用AJAX来判断dedecms用户是否登录呢
JS代码:Copy code<script language="javascript" src="{dede:global name='cfg_cmspath'/} ...
- animate动画jquery
<script> $(".change").animate({height:"hide",width:"300px"},&quo ...