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. 获取app下载链接

    https://itunes.apple.com/cn/app/id1398635899?mt=8 只需要更改其中的id就可以了

  2. validate验证注册表单

    点击预览; <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  3. python语言基础语法笔记<note1库安装和工具安装>

    Python是一门入门简单的编程语言,它的安装和搭建也非常简单.在大部分的发行Linux版本上都预装了python2,部分也预装了python3,需要查看Linux上是否安装Python,只需要在 命 ...

  4. Solr 6.7学习笔记(02)-- 配置文件 managed-schema (schema.xml)(1)

    刚学Solr(版本6.7.0),新建一个core时,提示要求schema.xml文件,我找了半天也没在源码包中找到名为schema.xml的文件.这个版本其实用的是managed-schema文件,没 ...

  5. 51nod1042(0-x出现次数&分治)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042 题意:中文题诶- 思路:这道题和前面的51nod100 ...

  6. 2014-11-3 NOIP模拟赛2

    NOIP 2014 水题模拟赛 (请选手务必仔细阅读本页内容) 一.题目概况 中文题目名称 数列 刷漆 排队 英文题目与子目录名 seq paint layout 可执行文件名 seq paint l ...

  7. mongoose的安装与使用(书签记录) 2017

    Windows7环境下安装:慕课网中的手记 http://www.imooc.com/article/12447 关于在CMD中对Mongo API的调用,从这些API调用中我们也可以更容易地理解在n ...

  8. 019 Remove Nth Node From End of List 删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点并返回头结点.例如,给定一个链表: 1->2->3->4->5, 并且 n = 2.当删除了倒数第二个节点后链表变成了 1->2 ...

  9. LCS(Longest Common Subsequence)

    http://blog.csdn.net/zztfj/article/details/6157429 LCS(Longest Common Subsequence) 就是求两个字符串最长公共子串的问题 ...

  10. UVALive - 6436

    题目链接:https://vjudge.net/contest/241341#problem/C Tree Land Kingdom is a prosperous and lively kingdo ...