A - Ice_cream’s world III
Description
Input
Output
Sample Input
Sample Output
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int ans;
int father[],rank[];//父节点,深度记录(这道题没必要用深度记录)
//int max1;
struct Line{
int first;
int last;
int cost;
}line[];//还没学会用数组来记录边,还是在用结构体做的,编程风格有待提高
int cmp(Line a,Line b){return a.cost<b.cost;}//排序的依据函数
int find(int x)//找点的父节点,把一条边的点搞到一个集合里面
{
if(father[x]!=x)
{
father[x]=find(father[x]);//这里有个优化,不会出现长链的情况,直接把father[x]=find[father[x]];
}
return father[x];
}
bool Union(int a,int b)//合并集合
{
int a1=find(a),b1=find(b);
if(a1==b1) return false;//如果是同一个父亲节点的,return false;
else{
father[b1]=a1;//否则合并成一个集合
ans++;
rank[a1]+=rank[b1];
if(max1<rank[a1]) max1=rank[a1];
return true;
}
} int main()
{
int a,b,c,n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
{
father[i]=i;
rank[i]=;
}
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&a,&b,&c);
line[i].first=a;
line[i].last=b;
line[i].cost=c; }
sort(line+,line+m+,cmp);
max1=;
int sum=;
ans=;
for(int i=;i<=m;i++)
{
if(Union(line[i].first,line[i].last))
sum+=line[i].cost;
}
if(ans==n-) printf("%d\n",sum);
else printf("impossible\n");
printf("\n");
}
return ;
}
A - Ice_cream’s world III的更多相关文章
- hdoj 2122 Ice_cream’s world III
并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Ice_cream’s world III(prime)
Ice_cream’s world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- Ice_cream’s world III
Ice_cream's world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- HDU 2122 Ice_cream’s world III【最小生成树】
解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS ...
- hdoj 2122 Ice_cream’s world III【最小生成树】
Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 【HDU2122】Ice_cream’s world III(MST基础题)
2坑,3次WA. 1.判断重边取小.2.自边舍去. (个人因为vis数组忘记初始化,WA了3次,晕死!!) #include <iostream> #include <cstring ...
- Ice_cream’s world III--2122
Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)
Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- mac 解决eclipse OutOfMemoryError
1.找到eclipse.ini文件 找到你的eclipse图标右击————>显示包内容-->contents -->macos -->eclipse.ini 2.修改内容 -s ...
- JavaEE SpringMVC 基础概念(如需详细资料请留言)
SpringMVC Web开发核心思想: 模型与视图相分离: 控制逻辑与业务逻辑相分离: 视图选择与具体视图技术相分离: 非侵入式开发(尽量少使用框架特定API),易于测试. SpringMVC是什么 ...
- sql 多行转换为一行
select 字段1, [val]=( select 字段2 +',' from 表名 as b where b.字段1 = a.字段1 for xml path('')) from 表名 as a ...
- window下关闭nginx
nginx -s stop taskkill /F /IM nginx.exe > nul
- PHP是什么
php 是一种服务器端的,嵌入html的脚本语言.php区别其他像客户端java的地方是它的代码在服务器端执行.php能做什么? 最低水平,php可以做任何其他cgi程序所能做的事,例如收集表格数据, ...
- 【转】responseText,responseBody,responseXML差别
为了做ajax的代理,研究了下服务器端的xmlhttp和客户端ajax中的xmlhttp,做了个比较 由于我一直使用JavaScript作为Asp的教本语言,所以比较起来更清楚.服务器端的xmlhtt ...
- centos下在线安装mysql
1 首先查看是否有安装过,如果已经安装过,就不必再安装了 yum list installed mysql* rpm -qa | grep mysql* 2 查看有没有安装包: yum list my ...
- c++学习笔记2
T: 1.默认实参只能声明一次: 设计含有默认实参的函数时,要合理设置形参的顺序 :局部变量不能作为默认实参 2.内联机制用于优化规模较小.流程直接.频繁调用的函数 3.constexpr函数 返 ...
- C++小项目:directx11图形程序(七):modelclass
模型类是世界空间中的表示物体的类,那么他的所做的事就是加载模型,移动模型,渲染模型 modelclass.h #pragma once #include <d3d11.h> #includ ...
- HTML元素隐藏和显示
在web前端开发过程中,经常会用到隐藏和显示元素的方法 总结:1.通过JS或Jquery控制 2.通过CSS样式控制 一.Js或jquery (jquery为例) 1.隐藏元素 使用 ...