This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:

ID Father Mother k Child​1​​⋯Child​k​​ M​estate​​ Area

where ID is a unique 4-digit identification number for each person; Father and Mother are the ID's of this person's parents (if a parent has passed away, -1 will be given instead); k (0≤k≤5) is the number of children of this person; Child​i​​'s are the ID's of his/her children; M​estate​​ is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.

Output Specification:

For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:

ID M AVG​sets​​ AVG​area​​

where ID is the smallest ID in the family; M is the total number of family members; AVG​sets​​ is the average number of sets of their real estate; and AVG​area​​ is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID's if there is a tie.

Sample Input:

10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100

Sample Output:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
#include <iostream>
#include <vector>
#include <algorithm>using namespace std;struct Data
{int id, fid, mid, num, area;int cid[10];
};struct person
{int id, people;double num, area;bool flag = false;
}ans[10000];bool cmp(person a, person b)
{if (a.area!=b.area){return a.area > b.area;}else{return a.id < b.id;}
}vector<int> father(10000);
vector<bool> visit(10000);int find(int x)
{if (x==father[x])//含路径压缩{return x;}return father[x] = find(father[x]);/*while (x!=father[x]){x = father[x];}return  x;*/
}void Union(int a,int b)
{int fA = find(a), fB = find(b);if (fA>fB){father[fA] = fB;}else if (fA<fB){father[fB] = fA;}
}int main()
{//freopen("in.txt", "r", stdin);int n, k, cnt = 0;cin >> n;for (int i = 0; i < 10000; i++)//初始化{father[i] = i;}vector<Data> d(1000);for (int i = 0; i < n; i++){cin >> d[i].id >> d[i].fid >> d[i].mid >> k;visit[d[i].id] = true;if (d[i].fid!=-1){visit[d[i].fid] = true;Union(d[i].id, d[i].fid);}if (d[i].mid!=-1){visit[d[i].mid] = true;Union(d[i].id, d[i].mid);}for (int j = 0; j < k; j++){cin >> d[i].cid[j];visit[d[i].cid[j]] = true;Union(d[i].id, d[i].cid[j]);          }cin >> d[i].num >> d[i].area;}for (int i = 0; i < n; i++){int id = find(d[i].id);ans[id].id = id;ans[id].num += d[i].num;ans[id].area += d[i].area;ans[id].flag = true;}for (int i = 0; i < 10000; i++){if (visit[i]){ans[find(i)].people++;}if (ans[i].flag){cnt++;}}for (int i = 0; i < 10000; i++){if (ans[i].flag){ans[i].num = (double)(ans[i].num*1.0 / ans[i].people);ans[i].area = (double)(ans[i].area*1.0 / ans[i].people);}}sort(ans, ans + 10000, cmp);cout << cnt << endl;for (int i = 0; i < cnt; i++){printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num, ans[i].area);}return 0;
}

二刷:

#include <iostream>
#include <vector>
#include <algorithm>using namespace std;const int maxn = 10006;
vector<int> father(maxn);int findfather(int x)
{return x == father[x] ? x : (father[x] = findfather(father[x]));
}void Union(int a, int b)
{int fa = findfather(a), fb = findfather(b);if (fa>fb){father[fa] = fb;}else{father[fb] = fa;}
}struct data
{int id, fid, mid, estate, area;int cid[10];
}d[maxn];struct node
{int id,  num;double estate, area;bool flag;
}ans[maxn];bool cmp(node a, node b)
{if (a.area!=b.area){return a.area > b.area;}else{return a.id < b.id;}
}int main()
{//freopen("in.txt", "r", stdin);int n;cin >> n;for (int i = 0; i < maxn; i++){father[i] = i;}bool visit[maxn] = { false };for (int i = 1; i <= n; i++){cin >> d[i].id >> d[i].fid >> d[i].mid;visit[d[i].id] = true;if (d[i].fid != -1){visit[d[i].fid] = true;Union(d[i].id, d[i].fid);}if (d[i].mid != -1){visit[d[i].mid] = true;Union(d[i].id, d[i].mid);}int k;cin >> k;for (int j = 0; j < k; j++){cin >> d[i].cid[j];visit[d[i].cid[j]] = true;Union(d[i].id, d[i].cid[j]);}cin >> d[i].estate >> d[i].area;}for (int i = 1; i <= n; i++){int root = findfather(d[i].id);ans[root].id = root;ans[root].estate += d[i].estate;ans[root].area += d[i].area;ans[root].flag = true;}int cnt = 0;//family 个数for (int i = 0; i < maxn; i++){if (visit[i]){int root = findfather(i);ans[root].num++;}if (ans[i].flag){cnt++;}}for (int i = 0; i < maxn; i++){if (ans[i].flag){ans[i].estate = (double)(ans[i].estate / ans[i].num);ans[i].area = (double)(ans[i].area / ans[i].num);}}sort(ans, ans+maxn, cmp);cout << cnt << endl;for (int i = 0; i < cnt; i++){printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].num, ans[i].estate, ans[i].area);}return 0;
}

1114 Family Property (25分) (并查集) 复杂题 经典并查集相关推荐

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

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

  2. 1114 Family Property (25 分)【难度: 中/ 知识点: 并查集】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805356599820288 挺好的一个并查集,先读入然后再处理.注意:在 ...

  3. 【CCCC】L2-021 点赞狂魔 (25分),,模拟水题,map数组,间接排序

    problem L2-021 点赞狂魔 (25分) 微博上有个"点赞"功能,你可以为你喜欢的博文点个赞表示支持.每篇博文都有一些刻画其特性的标签,而你点赞的博文的类型,也间接刻画了 ...

  4. 【CCCC】L2-020 功夫传人 (25分),,模拟水题,多叉树的存储与遍历

    problem L2-020 功夫传人 (25分) 一门武功能否传承久远并被发扬光大,是要看缘分的.一般来说,师傅传授给徒弟的武功总要打个折扣,于是越往后传,弟子们的功夫就越弱-- 直到某一支的某一代 ...

  5. 【CCCC】L2-019 悄悄关注 (25分),,模拟水题,STL大法好

    problem L2-019 悄悄关注 (25分) 新浪微博上有个"悄悄关注",一个用户悄悄关注的人,不出现在这个用户的关注列表上,但系统会推送其悄悄关注的人发表的微博给该用户.现 ...

  6. 【CCCC】L2-017 人以群分 (25分),,模拟水题

    problem L2-017 人以群分 (25分) 社交网络中我们给每个人定义了一个"活跃度",现希望根据这个指标把人群分为两大类,即外向型(outgoing,即活跃度高的)和内向 ...

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

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

  8. 1114. Family Property (25)

    //给定 id id的父亲 id的母亲(-1表示过世) k id的k个孩子 房产的数量 房产的面积 //求每个家庭(用最小的id存储)房产的总数 平均房产数量和面积(平均保留三位) //排序:平均面积 ...

  9. PAT甲级1114 Family Property:[C++题解]结构体、并查集、测试点3、4、5有问题的进来!!

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 先建边.读入每家的信息,在本人和父母(如果有的话),本人与子女(如果有的话)之间分别建边.边用结构体来存,边记录两个端点. 遍历每条 ...

最新文章

  1. Windows 7 + Fedora 17 双系统安装详解
  2. 多线程——线程的生命周期
  3. 【图像处理】一种低光照图像的亮度提升方法(Adaptive Local Tone Mapping Based on Retinex for High Dynamic Range Images)
  4. 开源 三层模型_开源模型将如何超越其他模型
  5. 变速更顺滑_CVT变速箱中的小弱鸡?10万公里必坏?变速箱该如何养护?
  6. 如何自动搜出更好、更小、更快的NLP模型?
  7. 可变换大小的星星c语言源程序,五邑大学试卷_C语言程序设计_信息学院各专业_B卷1教学内容.doc...
  8. 高分辨率扫描出来的图片有摩尔纹_【艺术与设计】 摩尔纹的设计
  9. node.js 谷歌翻译api
  10. xp系统升级到win7系统打印驱动的安装
  11. 【项目实战——emos在线办公系统】:会议申请、请假申请等部分代码理解
  12. .San(三). Xia(峡).对长江二号洪水削峰作用总结
  13. 自由-进化/开源中国众包平台
  14. HHUOJ 1012 欧洲杯(水题)
  15. P1931 套利-SPFA最长路与环的判断
  16. 网页认证上网服务器无响应,portal认证失败,网络故障或者portal服务器没有响应排查方法...
  17. AngularJS的优缺点总结
  18. [转]阿里云的这群疯子
  19. 如何安装虚拟机linux
  20. numpy.ndarray中对于字符串的处理

热门文章

  1. 每日一题 --- P1093 [NOIP2007 普及组] 奖学金[洛谷][JAVA]
  2. 哪些公司有计算机财务管理,计算机财务管理汇总.doc
  3. 11.Blinn-Phong高光
  4. android的适配器作用,适配器模式安卓中的应用
  5. 服务注册与发现 — 选择 CP 还是 AP?
  6. icloud安装错误怎么办_怎么办?iCloud云备份失败该如何解决?
  7. 独立显卡 台式计算机,解决方案:如何在台式计算机上安装独立显卡
  8. 独立显卡怎么安装-安装独立显卡简易视频安装教程
  9. x86架构手机_Intel也做过手机CPU?X86架构!曾今正面硬刚高通骁龙!结果唏嘘
  10. 《HTML5 2D游戏编程核心技术》——第1章,第1.1节Snail Bait游戏