Source:

PAT A1114 Family Property (25 分)

Description:

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 (≤). 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) 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

Keys:

Attention:

  • Union函数

Code:

 /*
Data: 2019-06-23 15:15:59
Problem: PAT_A1114#Family Property
AC: 42:40 题目大意:
统计家庭拥有的财产
输入:
第一行给出,户主人数N<=1000
接下来N行,my_id,dad_id,mom_id,m,kids,房产数,总面积
输出:
第一行给出,家庭数N
接来下N行,min_id,成员数,平均房产数,平均面积(三位小数)
平均面积递减,min_id递增 基本思路:
输入时按id做并查集,min_id作为父结点,存储户主信息
遍历户主,统计各家庭信息
排序
*/
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e5+;
int fa[M];
struct node
{
int id;
set<int> member;
double sets;
double area;
}info[M]; int Father(int v)
{
int x=v,s;
while(fa[v] != v)
v = fa[v];
while(fa[x] != x)
{
s = fa[x];
fa[x] = v;
x = s;
}
return v;
} void Union(int v1, int v2)
{
int f1 = Father(v1);
int f2 = Father(v2);
if(f1 < f2)
fa[f2] = f1;
else
fa[f1] = f2;
} bool cmp(node a, node b)
{
int s1=a.member.size();
int s2=b.member.size();
if(a.area/s1 != b.area/s2)
return a.area/s1 > b.area/s2;
else
return a.id < b.id;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE for(int i=; i<M; i++)
{
fa[i]=i;
info[i].id=i;
info[i].sets=;
info[i].area=;
} int n,my,dad,mom,m,kid;
int input[M];
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d%d%d%d", &my,&dad,&mom,&m);
input[i]=my;
info[my].member.insert(my);
if(dad != -)
{
Union(my,dad);
info[my].member.insert(dad);
}
if(mom != -)
{
Union(my,mom);
info[my].member.insert(mom);
}
for(int j=; j<m; j++)
{
scanf("%d", &kid);
Union(my,kid);
info[my].member.insert(kid);
}
scanf("%lf%lf", &info[my].sets, &info[my].area);
}
set<int> family;
for(int i=; i<n; i++)
{
my = input[i];
int f = Father(my);
family.insert(f);
if(f == my)
continue;
info[f].sets += info[my].sets;
info[f].area += info[my].area;
for(auto it=info[my].member.begin(); it!=info[my].member.end(); it++)
info[f].member.insert(*it);
}
vector<node> ans;
for(auto it=family.begin(); it!=family.end(); it++)
ans.push_back(info[*it]);
sort(ans.begin(),ans.end(),cmp);
printf("%d\n", ans.size());
for(int i=; i<ans.size(); i++){
int sum = ans[i].member.size();
printf("%04d %d %.3f %.3f\n", ans[i].id,sum,ans[i].sets/sum,ans[i].area/sum);
} return ;
}

PAT_A1114#Family Property的更多相关文章

  1. 探究@property申明对象属性时copy与strong的区别

    一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...

  2. JavaScript特性(attribute)、属性(property)和样式(style)

    最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...

  3. -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法

    最近在使用maven,项目测试的时候出现了这么一个错.-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2 ...

  4. python property理解

    一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...

  5. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...

  6. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

  7. # ios开发 @property 和 Ivar 的区别

    ios开发 @property 和 Ivar 的区别 @property 属性其实是对成员变量的一种封装.我们先大概这样理解: @property = Ivar + setter + getter I ...

  8. Cesium原理篇:Property

    之前主要是Entity的一个大概流程,本文主要介绍Cesium的属性,比如defineProperties,Property(ConstantProperty,CallbackProperty,Con ...

  9. 为Guid数据类型的属性(property)赋值

    先来看看数据库表中的字段设计: 在数据库的数据类型为uniqueidentifier. 而在程序中对应的数据类型为GUID. property有get和set,也就是说能获取值也可以赋值.

随机推荐

  1. hadoop-2.6.0 Unhealthy Nodes 问题

    近期安装hadoop-2.6.0集群时,打开8088页面,查看集群信息,看到集群出现Unhealthy Nodes 的问题,点击该处.会展开Unhealthy Nodes 的情况,这时会看到Healt ...

  2. window.setTimeout() 和window.setInterval() 的差别

    setTimeout 和setInterval的功能都是经过某一个时间段后发生某件指定的事件或者方法. 如window.setTimeout("sleep()",5000);指的是 ...

  3. MyEclipse 9.0 正式版公布新闻 下载

    MyEclipse 9.0 正式版公布 新闻 ============================================================================ ...

  4. C#实现马尔科夫模型例子

    已知条件:三个缸N状态,每个缸中不同颜色球的个数M状态值,时间轴T,观察值序列O 参数:状态值序列,转移概率序列 求:概率 后台代码如下 , M = ;//N状态,M状态值 (0橙色,1绿色,2蓝色, ...

  5. Unity3d中相应各平台Path

    IOS: Application.dataPath :                      Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xx ...

  6. InfluxDB 分布式时间序列数据库环境搭建——据qcon大会2016qiniu说集群很坑且闭源了

    InfluxDB 分布式时间序列数据库环境搭建   1. 环境说明 Ubuntu14.04  + influxDB V0.10.1 搭建3个节点的分布式数据库,副本数量2,各节点之间自动进行数据备份并 ...

  7. hibernate缓存机制详细分析(一级、二级、查询缓存,非常清晰明白)

    本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相信 ...

  8. 37. ext 中sm什么意思

    转自:https://zhidao.baidu.com/question/112450217.htmlsm是SelectionModel的缩写默认为RowSelectionModel其他模式还有Che ...

  9. [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. sql case when 用法

    sql语言中有没有类似C语言中的switch case的语句?? 没有,用case   when   来代替就行了.            例如,下面的语句显示中文年月         select  ...