PAT_A1114#Family Property
Source:
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 Child1⋯Childk MestateArea
where
ID
is a unique 4-digit identification number for each person;Father
andMother
are theID
'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; Childi's are theID
's of his/her children; Mestate is the total number of sets of the real estate under his/her name; andArea
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 AVGareawhere
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
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的更多相关文章
- 探究@property申明对象属性时copy与strong的区别
一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...
- JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...
- -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法
最近在使用maven,项目测试的时候出现了这么一个错.-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2 ...
- python property理解
一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...
- Android动画效果之Property Animation进阶(属性动画)
前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...
- Android动画效果之初识Property Animation(属性动画)
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
- # ios开发 @property 和 Ivar 的区别
ios开发 @property 和 Ivar 的区别 @property 属性其实是对成员变量的一种封装.我们先大概这样理解: @property = Ivar + setter + getter I ...
- Cesium原理篇:Property
之前主要是Entity的一个大概流程,本文主要介绍Cesium的属性,比如defineProperties,Property(ConstantProperty,CallbackProperty,Con ...
- 为Guid数据类型的属性(property)赋值
先来看看数据库表中的字段设计: 在数据库的数据类型为uniqueidentifier. 而在程序中对应的数据类型为GUID. property有get和set,也就是说能获取值也可以赋值.
随机推荐
- [PsTools]psexec.exe使用范例-运行远程电脑程序(exe、bat等)
前置条件 先下载psexec.exe.放置到C盘根文件夹 下载地址:http://download.csdn.net/detail/whylaughing/8885893 命令范例:(注意空格) C: ...
- 移动匿名支付购物方案 A Lightweight Anonymous Mobile Shopping Scheme Based on DAA for Trusted Mobile Platform
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
Redis总结(五)缓存雪崩和缓存穿透等问题 前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhon ...
- Bitmap通过getWidth和getHeight获取尺寸不符
在使用BitmapFactory载入图片时,常会出现这样的情况,返回的图片尺寸与实际尺寸不符.这是因为我们把图片资源放到res/drawable文件路径下时,选择的文件不同所致.不同的目录会有不同的缩 ...
- TLabel和TEdit的初次显示过程
procedure TForm1.Button2Click(Sender: TObject); var l: TLabel;begin l:=TLabel.Create(self); l.Name:= ...
- seq2seq里的数学
seq2seq模型详解 原创 2017年12月25日 09:41:04 标签: seq2seq / 自然语言 / 机器人 在李纪为博士的毕业论文中提到,基于生成的闲聊机器人中,seq2seq是一种 ...
- bzoj 3993 星际战争
题目大意: X军团和Y军团正在激烈地作战 在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai 当一个巨型机器人的装甲值减少到0或者以下时,这个巨型机 ...
- B1072 [SCOI2007]排列perm 状压dp
很简单的状压dp,但是有一个事,就是...我数组开大了一点,然后每次memset就会T,然后开小就好了!!!震惊!以后小心点这个问题. 题干: Description 给一个数字串s和正整数d, 统计 ...
- P2453 [SDOI2006]最短距离 dp
自己想出来了!这个dp比较简单,而且转移也很简单,很自然,直接上代码就行了. 题干: 一种EDIT字母编辑器,它的功能是可以通过不同的变换操作可以把一个源串X [l..m]变换为新的目标串y[1..n ...
- Linux 文件和目录操作 - cd - 切换目录
命令详解 重要星级: ★★★★★ 功能说明: cd 命令是 "change directory" 中每个单词的首字母缩写,其功能是从当前工作目录切换到指定工作目录. 语法格式: c ...