A1114. Family Property
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 Child1 ... Childk 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; Childi'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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
double estate[], area[];
int father[], familyNum[], tb[];
bool cmp(int a, int b){
if(area[a] != area[b])
return area[a] > area[b];
else return a < b;
}
int findFather(int x){
int temp = x;
while(x != father[x]){
x = father[x];
}
int temp2;
while(temp != x){
temp2 = father[temp];
father[temp] = x;
temp = temp2;
}
return x;
}
void merge(int a, int b){
if(a == - || b == -)
return;
int af = findFather(a);
int bf = findFather(b);
if(af == bf)
return;
if(af > bf)
swap(af, bf);
father[bf] = af;
}
int main(){
int N;
scanf("%d", &N);
int id, idf, idm, child;
int chiN;
for(int i = ; i < ; i++){
estate[i] = ;
area[i] = ;
father[i] = i;
familyNum[i] = ;
tb[i] = ;
}
for(int i = ; i < N; i++){
cin >> id >> idf >> idm >> chiN;
tb[id] = tb[idf] = tb[idm] = ;
merge(id, idf);
merge(id, idm);
int son;
for(int j = ; j < chiN; j++){
cin >> son;
tb[son] = ;
merge(id, son);
}
cin >> estate[id] >> area[id];
}
int cnt = ;
vector<int> ans;
for(int i = ; i < ; i++){
if(tb[i] == ){
int ff = findFather(i);
if(ff != i){
estate[ff] += estate[i];
area[ff] += area[i];
}
familyNum[ff]++;
if(i == ff){
cnt++;
ans.push_back(i);
} }
}
for(int i = ; i < ; i++){
if(tb[i] == && father[i] == i){
int Num = familyNum[i];
area[i] = area[i] / (double)Num;
estate[i] = estate[i] / (double)Num;
}
}
sort(ans.begin(), ans.end(), cmp);
printf("%d\n", cnt);
for(int i = ; i < ans.size(); i++){
printf("%04d %d %.3f %.3f\n", ans[i], familyNum[ans[i]], estate[ans[i]], area[ans[i]]);
}
cin >> N;
return ;
}
总结:
1、由于一个人既有双亲又有N多个孩子,所以用树形结构肯定不行。只能用并查集或者图搜索。
2、并查集:并查集仅仅对父母、子女的关系进行合并处理。房子数、面技数、家庭人口等先不计算,仅仅存在每个人各自的对应数组中。由于序号不是连续的1到N,因此father数组中会有很多无效节点,需要一个tb数组在输入的时候就记录哪些是有效的节点。 在并查集处理完家庭关系之后,对father数组再次遍历,当遇到非根节点时,查找到他对应的根节点,并把房子、面积、等累加至根节点对应的数组中。
3、由于要求输出的id为家族中最小id,所以在两个root合并时,要将更小的root作为新的root。
A1114. Family Property的更多相关文章
- PAT甲级——A1114 Family Property【25】
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- 【刷题-PAT】A1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- PAT A1114 Family Property
用并查集处理每个家庭的信息,注意标记~ #include<bits/stdc++.h> using namespace std; ; bool visit[maxn]={false}; i ...
- PAT_A1114#Family Property
Source: PAT A1114 Family Property (25 分) Description: This time, you are supposed to help us collect ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- 探究@property申明对象属性时copy与strong的区别
一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...
- JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...
随机推荐
- awk骚操作
一.awk自加 [root@168web3 ~]# head /data/logs/cloud_monitor_rds_cpu.log |awk '{sum+=$NF}END{print sum}' ...
- 利用Python制作简单的小程序:IP查看器
前言 说实话,查看电脑的IP,也挺无聊的,但是够简单,所以就从这里开始吧.IP地址在操作系统里就可以直接查看.但是除了IP地址,我们也想通过IP获取地理地址和网络运营商情况.IP地址和地理地址并没有固 ...
- 生成统计数据并导出Excel
需求:看如下表格的统计需求 生产调度中心部门需要从IT技术部门得到这些统计数据 步骤: (1)获取所有的子公司列表 (2)遍历所有的子公司,获取每个子公司的库存信息 (3)遍历所有的库存信息,并对库存 ...
- DELPHI中MDI子窗口的关闭 和打开
Delphi中MDI子窗口的关闭方式默认为缩小而不是关闭,所以当你单击子窗口右上角的关闭按钮时会发觉该子窗口只是最小化,而不是你预期的那样被关闭.解决办法是在子窗口的OnClose事件处理过程中加入如 ...
- Supervisord管理进程实践
今天凑空研究了下Supervisord,这是一款linux进程管理工具,使用python开发,主要用于在后台维护进程(类似master守护进程),可以实现监控进程的状态.自动重启进程等操作,便于一些服 ...
- spring和junit整合
- POI Excel 单元格内容类型判断并取值
个人用到的 String birthdayVal = null; ...
- 第二十一天,pickle json xml shelve configparser模块
今日内容 1.pcikle 专用于python语言的序列化 2.json 是一种跨平台的数据格式 也属于序列化的一种方式 3.xml 可拓展标记语言 一种编写文档的语法 也支持跨平台 比较json而言 ...
- ContOS7切换国内源
ContOS更换国内下载源 一,什么是yum源? yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由y ...
- 洛谷P1216数字三角形题解
题目 这道题是一个典型的DP,可以用倒推,顺推的方法,来解这道题.当然用不同的方法他的循环次序是不一样的,所以我们一定要深刻地理解题目的大意,再采用状态转移方程与边界每次求出最优解,并记录循环一遍后就 ...