PAT 1114 Family Property 并查集

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 Child1 … Childk 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; Childi’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

题意就是按照题目要求把一个人的信息给我们 让我们根据给出的信息网络
找出一个家庭中符合条件的信息输出 这个家庭的最小编号 家庭人数 每个人的人均数量和人均房产面积

并查集和搜索 都可以做 这里我用了并查集
每个结构体里多记录点信息 然后合并的时候好合并 因为这些信息都是可以加减替代更新到根节点上的 这里我们可以把输入的所有有关联的id全部连接起来
然后连接的时候如果不是在一个联通块中就要把不同联通块更新到一个联通块中
最后输出的时候 就是把每个家庭的根节点排序输出出来即可

#include<bits/stdc++.h>
using namespace std;
struct node{int f,s_area,s_set,min_id,pep;  
}n[10003];
void unin(int a,int b){n[b].f = a;n[a].min_id = min(n[a].min_id,n[b].min_id);n[a].pep +=n[b].pep;n[a].s_area+=n[b].s_area;n[a].s_set+=n[b].s_set;
}
bool cmp(node a,node b){return ((double)a.s_area/a.pep)>((double)b.s_area/b.pep)||(((double)a.s_area/a.pep)==((double)b.s_area/b.pep)&&a.min_id<b.min_id);
}// 
int find(int x){int k,p,j=x;while(n[x].f!=x)x = n[x].f;k = x;while(n[j].f!=j){p = n[j].f;n[j].f = k;j = p;      }return k;
}
int main()
{int N;scanf("%d",&N);for(int i=0;i<=9999;i++){n[i].f=i;n[i].min_id = i;n[i].pep = 1;}set<int>pep;while(N--){int id,fa,ma,k;scanf("%d%d%d%d",&id,&fa,&ma,&k);pep.insert(id);int fid = find(id);if(fa!=-1){int ffa = find(fa);if(ffa!=fid)unin(fid,ffa);  //      pep.insert(fa);}if(ma!=-1){int fma = find(ma);if(fma!=fid)unin(fid,fma);      //pep.insert(ma);}while(k--){int child;scanf("%d",&child);int fchild = find(child);//if(fchild!=fid)unin(fid,fchild);//pep.insert(child);}int m_es,area;scanf("%d%d",&m_es,&area);n[fid].s_area+=area;n[fid].s_set+=m_es;} set<int>s;// 去重复根节点 vector<node>ans; for(set<int>::iterator i = pep.begin();i!=pep.end();i++){int c = find(*i);if(s.find(c)==s.end()){s.insert(c);ans.push_back(n[c]);}}sort(ans.begin(),ans.end(),cmp);printf("%d\n",ans.size());for(int i = 0;i<ans.size();i++){printf("%04d %d %.3f %.3f\n",ans[i].min_id,ans[i].pep,(double)ans[i].s_set/ans[i].pep,(double)ans[i].s_area/ans[i].pep);}//return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/425261.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

web架构师编辑器内容-使用html2canvas获取截图,并处理一些问题

html2canvas-api 为了使用html2canvas完成截图的功能&#xff0c;我们首先先使用一个按钮来测试一下html2canvas的截图功能。 首先在页面上创建一个img标签 <img id"test-image" :style"{ width: 300px}"/>创建一个button按钮&#xff0c;添加点击…

win10安装mudbox失败,怎么强力卸载删除注册表并重新安装

一些搞设计的朋友在win10系统下安装mudbox失败或提示已安装&#xff0c;也有时候想重新安装mudbox的时候会出现本电脑windows系统已安装mudbox&#xff0c;你要是不留意直接安装mudbox&#xff0c;只会安装mudbox的附件或者直接提示失败&#xff0c;mudbox是不会安装上的。这种…

72 include动作和指令的区别

<%include...> 与<jsp:include....>指令的区别 include指令: 在翻译阶段&#xff08;将JSP页面转换成servlet的阶段&#xff09;&#xff0c;include会读入指定的页面中的内容&#xff0c;并将这些内容和原来的页面融合在一起 <% include file”header.htm…

[小技巧] ArrayList与LinkedList对比与常见方法

ArrayList与LinkedList对比 ArrayList是顺序结构&#xff0c;所以定位很快&#xff0c;但插入&#xff0c;删除数据慢。 LinkedList 是链表结构&#xff0c;定位慢&#xff0c;但插入&#xff0c;删除数据快。 ArrayList与LinkedList常见方法 ArrayList实现了List接口&#…

perl语言之列表与数组

第1课时 列表与数组 1&#xff09;直接列表 &#xff08;12&#xff0c;’abc‘&#xff0c;3.14&#xff0c;True&#xff09; # 这是一个直接量列表 2&#xff09;都是字符串可以用qw # 两者等价&#xff0c;最好不要有内嵌变量 a qw(abc def ghi) b (abc,def,ghi) c …

[剑指offer]面试题第[60]题[JAVA][n个骰子的点数][动态规划][空间优化]

【问题描述】[中等] 把n个骰子扔在地上&#xff0c;所有骰子朝上一面的点数之和为s。输入n&#xff0c;打印出s的所有可能的值出现的概率。你需要用一个浮点数数组返回答案&#xff0c;其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。示例 1:输入: …

面试资料

相关文章 面试官&#xff1a;谈谈你对 CSS 盒模型的认识?&#xff08;你确定会&#xff1f;&#xff09; 做为前端开发者&#xff0c;你应该要懂的 http协议 前端面试&#xff1a;谈谈 JS 垃圾回收机制 图片优化让网站大小减少 62% Javascript 面试中经常被问到的三个问题&…

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第50篇]什么是BLS基于Weil对的签名方案?

转载链接&#xff1a;https://www.cnblogs.com/zhuowangy2k/p/12248721.html 原文链接&#xff1a;http://bristolcrypto.blogspot.com/2015/10/52-things-number-50-what-is-bls-pairing.html

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第51篇]什么是基于ID的加密的安全模型,描述一个IBE方案

在公钥密码学中&#xff0c;如果Alice想要给Bob发送一条消息&#xff0c;她需要Bob的公钥&#xff0c;一般来说公钥都很长&#xff0c;就像一个随机的字符串。 假设Alice可以不用公钥而是使用Bob的名字或者邮件地址作为他的公钥。实际的来说&#xff0c;这会很方便&#xff0c…

更改配置:远程访问gitlab的postgresql数据库

作为这篇文章的补充&#xff1a; 将gitlab中的postgresql数据库开通远程访问 https://www.cnblogs.com/andy9468/p/10609682.html 替代&#xff08;二&#xff09;中的2、3、4步骤。 继续修改gitlab.rb vim /etc/gitlab/gitlab.rb postgresql[custom_pg_hba_entries] {APPLICA…

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第52篇]选择一个先进的应用概念,如电子投票、拍卖或多方计算。这种系统的大致安全要求是什么?

这是我们认为每个密码学博士一年级都应该知道的52件事中的最后一件。你可能已经收集了过去的52个博客&#xff0c;我们希望学生知道从理论到实践的各个方面。但关键是你需要在密码学中考虑的不仅是对遵守规则的玩家的安全&#xff0c;还有对不遵守规则的玩家的安全。让我们从投…

【PMP】组织结构类型

1.简单型 描述&#xff1a;人员并肩工作&#xff0c;所有者/经营者直接做出主要决定并监督执行。 PM角色&#xff1a;兼职(协调员) PM权限&#xff1a;极少(无) 项目管理人员&#xff1a;极少(无) 资源可用性&#xff1a;极少(无) 项目预算管理人&#xff1a;负责人 2.职能型组…

[Leetcode][第96题][JAVA][不同的二叉搜索树][动态规划][数学]

【问题描述】[中等] 【解答思路】 1. 动态规划 第 1 步&#xff1a;设计状态 第 2 步&#xff1a;状态转移方程 第 3 步&#xff1a;考虑初始化 第 4 步&#xff1a;考虑输出 时间复杂度&#xff1a;O(N^2) 空间复杂度&#xff1a;O(N) public int numTrees(int n) {…

培训班出身的前端,出路在哪儿

前段时间&#xff0c;有个郑州的朋友在微信上给我留言&#xff0c;咨询培训班毕业找工作的事情。我当时答应他会写一篇文章的&#xff0c;没想到耽搁到现在。 所以&#xff0c;趁着今天有空&#xff0c;在公司码点字&#xff0c;聊聊这方面的事情。 想知道出路&#xff0c;必先…

[剑指offer]面试题第[61]题[JAVA][扑克牌中的点数][HashSet][数组]

【问题描述】[简单] 【解答思路】 1. 集合 Set 遍历 时间复杂度&#xff1a;O(N) 空间复杂度&#xff1a;O(N) class Solution {public boolean isStraight(int[] nums) {Set<Integer> repeat new HashSet<>();int max 0, min 14;for(int num : nums) {if(nu…

[剑指offer]面试题第[65]题[JAVA][不用加减乘除做加法][位运算]

【问题描述】[简单] 【解答思路】 1. 位运算 时间复杂度&#xff1a;O(1) 空间复杂度&#xff1a;O(1) public int add(int a, int b) {while(b ! 0) { // 当进位为 0 时跳出int c (a & b) << 1; // c 进位a ^ b; // a 非进位和b c; // b 进位}return a;}【…

2018 蓝桥杯省赛 B 组模拟赛(一)H.封印之门 最短路

蒜头君被暗黑军团包围在一座岛上&#xff0c;所有通往近卫军团的路都有暗黑军团把手。幸运的是&#xff0c;小岛上有一扇上古之神打造的封印之门&#xff0c;可以通往近卫军团&#xff0c;传闻至今没有人能解除封印。封印之门上有一串文字&#xff0c;只包含小写字母&#xff0…

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第4篇] P类复杂问题

这个是密码学52件事第四篇&#xff0c;和理论密码学的第一篇。在这篇中我被要求定义P类复杂问题。我对计算理论科学知道的非常少当我开始我的PhD,而且我确信很多人和我一样。因此这篇文章从最简单的开始&#xff0c;如果你已经会了你可以跳过前面的部分。首先我会描述复杂性的定…

2018蓝桥模拟赛·天上的星星 暴力|二维树状数组

在一个星光摧残的夜晚&#xff0c;蒜头君一颗一颗的数这天上的星星。蒜头君给在天上巧妙的画了一个直角坐标系&#xff0c;让所有的星星都分布在第一象。天上有 nn 颗星星&#xff0c;他能知道每一颗星星的坐标和亮度。现在&#xff0c;蒜头君问自己 qq 次&#xff0c;每次他问…

VS2010主题设置及插件推荐

本文主要写了个人使用 VS2010 的一些配置及实用插件&#xff0c;从而打造一个符合个人风格的开发环境。 基础设置 安装 Visual Assist X 在 VS2010 中若不安装 Visual Assist X 这个插件&#xff0c;直接开发 C 相关的项目将是非常痛苦的事情。默认环境没有对代码的不同部分进行…