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 小希的迷宫 基 ...
随机推荐
- 性能adb命令
启动时间-冷启动启动App命令adb shell am start -W -n com.bit_health.android/.ui.common.activities.BitHealthMainAc ...
- SQL Server中的日期格式化
SQL Server中文版的默认的日期字段datetime格式是yyyy-mm-dd Thh:mm:ss.mmm 例如: select getdate() 2004-09-12 11:06:08 ...
- Bootstrap <基础二十五>警告(Alerts)
警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加一个 ...
- Bootstrap<基础十> 响应式实用工具
Bootstrap 提供了一些辅助类,以便更快地实现对移动设备友好的开发.这些可以通过媒体查询结合大型.小型和中型设备,实现内容对设备的显示和隐藏. 需要谨慎使用这些工具,避免在同一个站点创建完全不同 ...
- JavaScript中的作用域
很多(JavaScript)开发者都在讨论"作用域",但它是什么?它们在JavaScript中的任何地方!我发现很多年轻的开发者不知道作用域是什么.他们中大多数人可以用jQuery ...
- B树
/************************************************ *作者:陈新 *时间:2014 6.3 *邮箱:cx2pirate@gmail.com * **** ...
- ICML历年Best Papers
作者:我爱机器学习原文链接:ICML历年Best Papers ICML (Machine Learning)(1999-2016) 2016 Dueling Network Architecture ...
- c语言操作一维数组-3
C语言选择题#includemain(){double a[15],k;k=fun(a);} 则以下选项中错误的fun函数首部是 ( D)A.double fun(double a[15]) B.do ...
- sql inner join , left join, right join , union,union all 的用法和区别
Persons 表: Id_P LastName FirstName Address City 1 Adams John Oxford Street London 2 Bush George Fift ...
- what is SVD and how to calculate it
http://web.mit.edu/be.400/www/SVD/Singular_Value_Decomposition.htm SVD是研究地震波运动极性化的一个方法.