PAT 1114 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 Mestate 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; Mestate 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 AVGsets AVGarea
where 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
分析
参考并查集简析
#include<iostream> //并查集的变形
#include<vector>
#include<set>
#include<algorithm>
#include<iomanip>
using namespace std;
struct family{
int id, mid, fid, area, num;
int cid[10];
}data[1005];
struct node{
int id, people;
double num, area;
}ans[10000];
vector<int> peo(10000,0);
set<int> member, vec;
int findfather(int c){
while(c!=peo[c])
c=peo[c];
return c;
}
void Union(int a, int b){
int m=findfather(a);
int n=findfather(b);
if(m<n)
peo[n]=m;
else
peo[m]=n;
}
bool cmp(const node& n1, const node& n2){
return (n1.area!=n2.area?n1.area>n2.area:n1.id<n2.id);
}
int main(){
int n, cn, cnt=0;
cin>>n;
for(int i=0; i<10000; i++)
peo[i]=i;
for(int i=0; i<n; i++){
cin>>data[i].id>>data[i].fid>>data[i].mid>>cn;
member.insert(data[i].id);
if(data[i].fid!=-1){
member.insert(data[i].fid);
Union(data[i].id, data[i].fid);
}
if(data[i].mid!=-1){
member.insert(data[i].mid);
Union(data[i].id, data[i].mid);
}
for(int j=0; j<cn; j++){
cin>>data[i].cid[j];
member.insert(data[i].cid[j]);
Union(data[i].id, data[i].cid[j]);
}
cin>>data[i].num>>data[i].area;
}
for(int i=0; i<n; i++){
int t=findfather(data[i].id);
ans[t].id=t;
ans[t].num+=data[i].num;
ans[t].area+=data[i].area;
vec.insert(t);
}
for(auto it=member.begin(); it!=member.end(); it++)
ans[findfather(*it)].people++;
for(auto it=vec.begin(); it!=vec.end(); it++){
ans[*it].area=double(ans[*it].area/ans[*it].people);
ans[*it].num=double(ans[*it].num/ans[*it].people);
cnt++;
}
sort(ans, ans+10000, cmp);
cout<<cnt<<endl;
for(int i=0; i<cnt; i++)
cout<<setw(4)<<setfill('0')<<ans[i].id<<" "<<ans[i].people<<" "<<setiosflags(ios::fixed)<<setprecision(3)<<ans[i].num<<" "<<ans[i].area<<endl;
return 0;
}
PAT 1114 Family Property的更多相关文章
- PAT 1114 Family Property[并查集][难]
1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- PAT甲级——1114 Family Property (并查集)
此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...
- 1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- PAT (Advanced Level) 1114. Family Property (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT甲题题解-1114. Family Property (25)-(并查集模板题)
题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...
- 【PAT甲级】1114 Family Property (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行每行包括一个人的ID和他双亲的ID以及他的孩子数量和孩子们的ID(四位整数包含前导零),还有他所拥有的房产数量和房产面积.输出一共有多少个 ...
- 1114 Family Property
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- PAT A1114 Family Property
用并查集处理每个家庭的信息,注意标记~ #include<bits/stdc++.h> using namespace std; ; bool visit[maxn]={false}; i ...
随机推荐
- oc66--代理模式应用2
// // BabyProtocol.h #import <Foundation/Foundation.h> @class Baby; @protocol BabyProtocol < ...
- thinkphp的model的where条件的两种形式
thinkphp的model的where查询时有两种形式. $model->field('id')->where('customer_num is null or customer_num ...
- CF 436D 最小生成树
设一个开头的虚节点,然后建稠密图,O(n^2).使用prim.O(n^2),保存方案,没什么好说的. #include <string.h> #include <stdio.h> ...
- 记一次Oracle冷备恢复的过程
一.故障来临 某日中午,市电意外中断,机房UPS电源由于负载过重而未接管供电,所有服务器全部重启...... 待所有服务器重启后,正在逐一检查设备和业务运行情况时,意外发生了.一台年代久远的HP PC ...
- linux的touch命令
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a ...
- C#学习-处理Excel
首先先了解下一个Excel文件的组成 1.一个Excel包含多个工作表(Sheet) 2.一个工作表(Sheet)包含多行(Row) 3.一行(Row)包含多个单元格(Cell) 如何判断一个单元 ...
- [ SCOI 2008 ] 着色方案
\(\\\) \(Description\) 给出\(K\)种颜料各自的个数\(C_i\),每一个颜料只够涂一个格子,求将颜料用完,涂一排格子,每个格子只能涂一次的条件下,相邻两个格子的颜色互不相同的 ...
- Java中常用的操作PDF的类库
iText iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好的给合.使用 ...
- ScrollView在调试状态一点击就挂的原因(OnMouseActivate)
这几天做的一个任务是做一个Dialog,需要在这个Dialog中添加一个自定义的CSrollvew类,但是遇到一个比较扯淡的问题,程序直接运行时可以的,调试状态下一点击CSrollview就挂了.而且 ...
- C# windform自定义控件的属性小知识
word中的加粗变斜之类的一直让我以为是button,直到我接触了自定义控件,才发现实现这种机能最好的是CheckBox,然后我们在做一个系统的时候,这种控件有可能要用好多次,总不能在用一次的时候,就 ...