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. ViewPager + Handler 实现的图片自己主动轮播

    首先上图看效果 我也是在网上看各种大牛们做的效果,非常多都是自己定义重写了一些控件来实现这个效果的. 我把当中的一位大牛写的ViewPager的效果加上了Handler实现了自己主动轮播效果.在此做个 ...

  2. Fedora下克隆Octopress博客

    我在自己的github上搭建了一个Octopress博客(http://songlee24.github.io/),用于不定期的写一些学习笔记和心得体会.但是有时候由于换了电脑或者重装了linux系统 ...

  3. ZOJ2599:Graduated Lexicographical Ordering(很经典的数位DP)

    Consider integer numbers from 1 to n. Let us call the sum of digits of an integer number its weight. ...

  4. sqlite 数据库 相关知识

    一基本简单介绍 SQLite 是一个自持的(self-contained).无server的.零配置的.事务型的关系型数据库引擎.由于他非常小,所以也能够作为嵌入式数据库内建在你的应用程序中. SQL ...

  5. 具体解释kernel中watchdog 驱动程序

    watchdog不管在小系统还是大的project系统中都是必须存在的.在解决线程挂死.系统死循环等都用非常重要的应用,算是系统出问题恢复初始状态的救命稻草. 在kernel中wdt的应用不是非经常见 ...

  6. 使用Vitamio插件显示花屏

    Vitamio是一款 Android 与 iOS 平台上的全能多媒体开发框架,全面支持硬件解码与 GPU 渲染. 使用vitamio进行播放器的开发非常便捷,使用vitamio的解码,自己编写播放器界 ...

  7. linux内核中的宏ffs(x)

    linux内核中ffs(x)宏是平台相关的宏,在arm平台,该宏定义在 arch/arm/include/asm/bitops.h #define ffs(x) ({ unsigned long __ ...

  8. 第二章、Tiny4412 U-BOOT移植二 启动分析【转】

    本文转载自:http://blog.csdn.net/eshing/article/details/37521481 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.启动过程说明 讲解启动过 ...

  9. [Codeforces 489E] Nastya and King-Shamans

    [题目链接] http://codeforces.com/contest/992/problem/E [算法] 线段树 + 二分 时间复杂度 : O(NlogN^2) [代码] #include< ...

  10. 简单动态规划——最长公共子序列&&最长回文子序列&&最长上升||下降子序列

    最长公共子序列,顾名思义当然是求两个字符串的最长公共子序列啦,当然,这只是一道非常菜的动规,所以直接附上代码: #include<iostream> #include<cstdio& ...