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 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

注意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的更多相关文章

  1. PAT甲级1114. Family Property

    PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...

  2. PAT甲级——1114 Family Property (并查集)

    此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  6. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  7. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  8. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  9. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

随机推荐

  1. 【idea-部署web项目】

    IntelliJ IDEA 14.x 与 Tomcat 集成,并运行Web项目 时间 2015-01-17 09:40:06  PHP博客 原文  http://blog.snsgou.com/pos ...

  2. 一步步教你学会browserify

    本文来自网易云社区 作者:孙圣翔 注意 文章需要边看边练习,不然你可能忘得速度比看的还快. 原文地址: http://my.oschina.net/goskyblue/blog/552284 Brow ...

  3. 打通Java与MySQL的桥梁——jdbc

    实现的基本步骤: 1.加载驱动程序: Class.forName("com.mysql.jdbc.Driver"); 2.获得数据可连接: private static final ...

  4. Android代码笔记

    1. 如何监听Android的短信收发,自动填充验证码? getContentResolver().registerContentObserver(Uri.parse(SMS_URI_ALL), tr ...

  5. jmeter场景设计实战(一)

    需求:2000用户在线,100用户并发访问首页. 和开发沟通了解了具体的需求:2000用户是在线登录状态,这2000用户中要达到100用户并发去访问首页,在这个过程中可能会有停留时间,并不是用户登录之 ...

  6. Java Web之文件的上传及下载

    一.文件的上传 1. 简介 > 将一个客户端的本地的文件发送到服务器中保存. > 上传文件是通过流的形式将文件发送给服务器. 2.表单的设置 1.向服务器上传一个文件时,表单要使用post ...

  7. PAT天梯赛L2-007 家庭房产

    题目链接:点击打开链接 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列 ...

  8. IIS上部署网站问题总结

    主要是对使用IIS过程遇到的问题的一些简单总结: 1. 当部署完web系统后,通过浏览器访问,如果遇到问题,一定要仔细阅读抛出来的error信息,很重要!很重要!很重要!说三遍. 2. 当每次尝试修改 ...

  9. 生产阶段Webpack打包【基础打包】

    webpack打包 1.在根目录创建一个 webpack.config.prod.js[它其实就是在开发阶段的基础上增加点东西] 增加了 output 去除了 devServer 2.在package ...

  10. Luogu P4161 [SCOI2009]游戏 数论+DP

    ywy神犇太巨辣!!一下就明白了!! 题意:求$lcm(a_1,a_2,...,a_k)$的种类,其中$\Sigma\space a_i <=n$,$a_i$相当于环长 此处的$DP$,相当于是 ...