https://pintia.cn/problem-sets/994805342720868352/problems/994805356599820288
挺好的一个并查集,先读入然后再处理。注意:在并查集合并的时候选编号小的作为根。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int p[N],people[N],home_cnt[N],st[N],n;
double area[N];
struct node{int a,b;}temp;
struct family
{int id,cnt;double avg,area;
};
vector<node>ve;
vector<family>ans;
bool cmp(family a,family b)
{if(a.area==b.area)return a.id<b.id;return a.area>b.area;
}
int find(int x)
{if(x!=p[x]) p[x]=find(p[x]);return p[x];
}
int main(void)
{cin>>n;for(int i=0;i<N;i++) p[i]=i,people[i]=1;for(int i=0;i<n;i++){int id,month,father,k; cin>>id>>month>>father>>k;st[id]=1;if(month!=-1) ve.push_back({id,month}),st[month]=1;if(father!=-1) ve.push_back({id,father}),st[father]=1;for(int j=0;j<k;j++){int x; cin>>x; st[x]=1;ve.push_back({id,x});}cin>>home_cnt[id]>>area[id];}for(int i=0;i<ve.size();i++){int a=ve[i].a,b=ve[i].b;int fa=find(a),fb=find(b);if(fa!=fb){if(fa>fb) swap(fa,fb);//选小结点作为根people[fa]+=people[fb];home_cnt[fa]+=home_cnt[fb];area[fa]+=area[fb];p[fb]=fa;}}for(int i=0;i<N;i++)if(st[i]&&p[i]==i)//如果该点存在,且是该集合的根{ans.push_back({i,people[i],home_cnt[i]*1.0/people[i],area[i]/people[i]});}sort(ans.begin(),ans.end(),cmp);cout<<ans.size()<<endl;for(int i=0;i<ans.size();i++)printf("%04d %d %.3lf %.3lf\n",ans[i].id,ans[i].cnt,ans[i].avg,ans[i].area);return 0;
}

1114 Family Property (25 分)【难度: 中/ 知识点: 并查集】相关推荐

  1. 1034 Head of a Gang (30 分) 【难度: 中 / 知识点: 并查集】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 首先不难想到的是并查集,不过这里有一个关键的 ...

  2. 1107 Social Clusters (30 分)【难度: 中 / 知识点: 并查集】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805361586847744 很不错的一个并查集的操作.你会发现我们需要处 ...

  3. C++学习之路 | PTA(甲级)—— 1114 Family Property (25分)(带注释)(并查集)(精简)

    1114 Family Property (25分) This time, you are supposed to help us collect the data for family-owned ...

  4. 1114 Family Property (25分) (并查集) 复杂题 经典并查集

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  5. 7-25 朋友圈 (25 分)(详解+并查集的了解和应用)

    一:题目 某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友"这个 ...

  6. 1114. Family Property (25)-PAT甲级真题(并查集)

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  7. 关于算法中的并查集,写的很有意思,转过来看看~

    文章来自于:http://blog.csdn.net/niushuai666/article/details/6662911 例子就是杭电上的畅通工程: http://acm.hdu.edu.cn/s ...

  8. 分珠(dfs+并查集)

    1140 分珠 时间限制:500MS  内存限制:65536K 提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量 ...

  9. 7-3 最小生成树-kruskal (10 分)(思路+详解+并查集详解+段错误超时解决)宝 Come

    一:前言 本题需要用到并查集的知识,建议先学完并查集后再看看本题 二:题目 题目给出一个无向连通图,要求求出其最小生成树的权值. 温馨提示:本题请使用kruskal最小生成树算法. 输入格式: 第一行 ...

最新文章

  1. 详解Apache下.htaccess文件常用配置
  2. oneshot单样本学习笔记
  3. TensorFlow学习笔记(十六)tf.random_normal
  4. mysql索引过多为什么会慢_mysql – 为什么索引使这个查询更慢?
  5. (lucas) Saving Beans
  6. 去认识一下WCDMA中的SCH信道
  7. Attit 现代编程语言重要特性 目录 第一章 可读性与开发效率 简单性 1 第一节 垃圾回收 1 第二节 自动垃圾回收 1 第三节 命名参数 函数多返回值 1 第四节 多语言 语言交互性 2 第五
  8. Arcgis学习笔记(二)投影和定义投影
  9. 大牛讲堂 | 深度学习Sequence Learning技术分享
  10. DFRobot for Arduino 中级套件
  11. Prewitt和Sobel算子
  12. 用于空间搜索的四叉树
  13. linux使用tmux后台运行程序
  14. word2016在方块里打勾
  15. 数据结构之单源最短路径(迪杰斯特拉算法)-(九)
  16. 抖音前端团队国际化是怎么做的?
  17. SQL注入学习之特殊符号(三)
  18. 关于wifi portal认证--为浏览器添加wifi认证功能
  19. java网络编程实现一个聊天程序
  20. ProcessDB实时/时序数据库——C/C++操作数据点对象

热门文章

  1. .net core 监听性能,异常
  2. centos7下tomcat7 或tomcat8启动超慢原因
  3. 网络流 24 题汇总(LOJ 上只有 22 题???)
  4. 自然语言处理在医学领域的应用
  5. 团队计划(5.25)
  6. GPL/dnsmasq源代码分析, DNS 部分
  7. ubuntu软件位置(安装位置, 运行文件位置, 缓存位置)
  8. Java自带的多线程监控分析工具(VisualVM)
  9. 近世代数--群同构--第三同构定理
  10. [[HOW TO]-ubuntu20.10安装openjrok指南