pat甲级1114
1114 Family Property(25 分)
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 Mestate 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; Mestate 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 AVGsets AVGarea
where ID is the smallest ID in the family; M is the total number of family members; AVGsets is the average number of sets of their real estate; and AVGarea 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没有说不等于0.因此判断时为大于等于0,而不是大于。否则测试点2、4会错误。
我用的深度优先搜索求联通分量来完成的这道题,另外用并查集好像也比较方便。
由于ID不连续,所以求连通分量之前先将所有ID放到vector里面,而且用邻接表存储图更节省空间和时间。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; bool marked[];
vector<vector<int>> G();
int estate[], area[]; struct family
{
int id = , size = ;
double estate = 0.0, area = 0.0;
}; void connect(int id)
{
int t;
cin >> t;
if (t >= )
{
G[id].push_back(t);
G[t].push_back(id);
}
} void dfs(int s, family& f)
{
marked[s] = true;
f.size++;
if (s < f.id) f.id = s;
f.area += area[s];
f.estate += estate[s];
for (int w : G[s])
if (!marked[w]) dfs(w, f);
} bool cmp(family a, family b)
{
if (a.area != b.area) return a.area > b.area;
else return a.id < b.id;
} int main()
{
int N;
cin >> N;
int i, j, id, k;
vector<int> allId;
for (i = ; i < N; i++)
{
cin >> id;
allId.push_back(id);
for (j = ; j < ; j++)
connect(id);
cin >> k;
for (j = ; j < k; j++)
connect(id);
cin >> estate[id] >> area[id];
}
vector<family> v;
for (int id : allId)
{
if (!marked[id])
{
family f;
dfs(id, f);
f.area /= f.size;
f.estate /= f.size;
v.push_back(f);
}
}
sort(v.begin(), v.end(), cmp);
cout << v.size() << endl;
for (i = ; i < v.size(); i++)
printf("%04d %d %.3f %.3f\n", v[i].id, v[i].size, v[i].estate, v[i].area);
return ;
}
pat甲级1114的更多相关文章
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- PAT甲级——1114 Family Property (并查集)
此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
随机推荐
- Eclipse 整合SpringMybatis,SpringMVC,用Maven管理项目搭建详情
环境:JDK下载地址 https://pan.baidu.com/s/1UyvEAI-4Ci6TDdVJiYUUiQ 密码:ma51 IDE:eclipse下载地址 https://pan.baidu ...
- python语言基础语法笔记<note1库安装和工具安装>
Python是一门入门简单的编程语言,它的安装和搭建也非常简单.在大部分的发行Linux版本上都预装了python2,部分也预装了python3,需要查看Linux上是否安装Python,只需要在 命 ...
- burpsuite 抓HTTPS数据包
抓HTTPS数据包 导出保存为cer证书文件,导入到受信任的根证书颁发机构 设置代理服务器与burp中proxy listeners保持一致 设置目标url 抓包 可用repeater发请求
- [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标
目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...
- Java 异常处理基本规则,Java异常处理的基本规范
看了团队中原来代码中的异常处理,心碎了一地,稍微对照阿里巴巴的异常处理规范整理了一遍,准备分享一下,Java的异常处理规范&约束. 一.运行异常的扑捉 不要捕获 Java 类库中定义的继承自 ...
- 再看thinkphp5分页类使用
之前使用tp5的分页paginate类时只用到了第一个参数,也就是每页显示多少行 今天又仔细看了下手册和paginate类,发现paginate可传入的参数有很多,可以满足更多需求 比如可以指定分页的 ...
- Django 03 模板路径、模板变量、常用的过滤器
Django 03 模板路径.模板变量.常用的过滤器 一.模板路径 #1.在每个app下面添加一个templates文件 #2.在项目views.py里面第33行INSTALLED_APPS里面添加上 ...
- 洛谷P5280 [ZJOI2019]线段树
https://www.luogu.org/problemnew/show/P5280 省选的时候后一半时间开这题,想了接近两个小时的各种假做法,之后想的做法已经接近正解了,但是有一些细节问题理不 ...
- java中存在垃圾回收机制,但是还会有内存泄漏的问题,原因是
答案是肯定的,但不能拿这一句回答面试官的问题.分析:JAVA是支持垃圾回收机制的,在这样的一个背景下,内存泄露又被称为“无意识的对象保持”.如果一个对象引用被无意识地保留下来,那么垃圾回收器不仅不会处 ...
- iOS 本地缓存实现 方案借鉴
在手机应用程序开发中,为了减少与服务端的交互次数,加快用户的响应速度,一般都会在iOS设备中加一个缓存的机制,前面一篇文章介绍了iOS设备的内存缓存,这篇文章将设计一个本地缓存的机制. 功能需求 这个 ...