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,也就是说能获取值也可以赋值.
随机推荐
- 机器学习A
订阅地址 : http://blog.csdn.net/lizhe_dashuju/rss/list
- LeetCode 234. Palindrome Linked List (回文链表)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- iOS----四方块 动画button实现
突然想起来上一次面试考官提问的一个问题:怎样创建一个菱形,并让它对应单击事件.能够开合的效果. 当时第一反应使用button来填充菱形的图片来实现,只是考官说,这样点击的区域不够灵敏,毕竟button ...
- 文章编辑器 文本替换 操作dom 发帖 富文本 今日头条发布富文本的实现 键盘化的html
js 修改 iframe it=document.getElementById('ueditor_0').contentWindow.document.getElementsByTagName(& ...
- Java代码规范_插件_阿里java开发手册
给大家分享一个阿里巴巴的java开发规范,在日常自动化工作中我们可以参考一下,特别是用java进行coding的同学. 而且还可以利用相应的插件进行代码扫描检测,感兴趣的们可以马上应用到自动化中来. ...
- Android之利用EventBus进行数据传递
在项目中,不可避免的要在两个页面之间进行数据的传递,就算不传递,也需要进行刷新之类的,我们根据Google提供的库类方法,也是可以做的,主要有广播broadcastreceiver,startacti ...
- Faas 典型场景——应用负载有显著的波峰波谷,典型用例-基于事件的数据处理
Serverless适用的两大场景 场景一:应用负载有显著的波峰波谷 Serverless化与否的评判标准并不是公司规模的大小,而是其业务背后的具体技术问题,比如业务波峰波谷明显,如何实现削峰填谷.一 ...
- 用回调函数创建一个XMLHttpRequest,并从一个TXT文件中检索数据。
<script> var xmlhttp; function loadXMLDoc(url,soyo) { if (window.XMLHttpRequest) {// IE7+, Fir ...
- Android.mk添加第三方jar包(转载)
转自:www.cnblogs.com/hopetribe/archive/2012/04/23/2467060.html LOCAL_PATH:= $(call my-dir)include $(CL ...
- C# Pen绘制虚线(System.Drawing.Pen与System.Windows.Media.Pen)
一.绘制虚线的方法 GDI绘制,使用的是System.Drawing.Pen Pen pen = new Pen(Color.Red, 1); pen.DashStyle = S ...