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 NNN (≤1000\le 1000≤1000). Then NNN lines follow, each gives the infomation of a person who owns estate in the format:
ID
Father
Mother
kkk Child1⋯ChildkChild_1 \cdots Child_kChild1⋯Childk MestateM_{estate}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); kkk (0≤k≤50\le k\le 50≤k≤5) is the number of children of this person; ChildiChild_iChildi's are the ID
's of his/her children; MestateM_{estate}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
AVGsetsAVG_{sets}AVGsets AVGareaAVG_{area}AVGarea
where ID
is the smallest ID in the family; M
is the total number of family members; AVGsetsAVG_{sets}AVGsets is the average number of sets of their real estate; and AVGareaAVG_{area}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
题意:给出n个人的编号,父母亲,孩子编号,拥有的财产数量和总面积,让你统计一共有多少个家族 ,并按照家族平均财产面积降序排列,如果相同则按照家族中最小id升序排列,最后按格式输出这些家族。
思路:使用并查集进行家族聚合,每次合并操作都让较小的编号作为父节点。首先建立结构体存储每个节点的数据
struct node{
int id,people;
double M,area;
}ans[10010],family[10010]; //ans存放各节点,family存放家族
每一条数据都进行并查集合并,然后再次便利统计家族个数,最后排序输出。为了节省时间,遍历时只遍历存在的编号,可以使用哈希表,如果编号i出现过,则令vis[i]=1。
参考代码:
#include<cstdio>
#include<unordered_map>
#include<algorithm>
using namespace std;
int n,father[10010],vis[10010]; //father完成并查集,vis标记结点是否存在
unordered_map<int,int> mp; //mp统计一共多少个家族
struct node{
int id,people;
double M,area;
}ans[10010],family[10010]; //ans存放各节点,family存放家族
int findFather(int x){ //查找父节点并进行路径压缩
int a=x,z=x;
while(a!=father[a])
a=father[a];
while(x!=father[x]){
z=x;
x=father[x];
father[z]=a;
}
return a;
}
void Union(int u,int v){ //完成父节点合并操作
int fu=findFather(u);
int fv=findFather(v);
if(fu!=fv)
father[max(fu,fv)]=min(fu,fv);
}
bool cmp(node a,node b){ //按家族平均面积和id排序
if(a.people!=0&&b.people!=0){
if(a.area/a.people!=b.area/b.people) return a.area/a.people>b.area/b.people;
else return a.id<b.id;
}else return a.people>b.people;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<10000;i++) father[i]=i; //初始化
for(int i=0;i<n;i++){
int id,fa,mo,t,num;
scanf("%d%d%d%d",&id,&fa,&mo,&num);
if(fa!=-1) {
Union(id,fa);
vis[fa]=1;
}
if(mo!=-1) {
Union(id,mo);
vis[mo]=1;
}
for(int j=0;j<num;j++){
scanf("%d",&t);
Union(id,t);
vis[t]=1;
}
ans[id].id=id;
vis[id]=1;
scanf("%lf%lf",&ans[id].M,&ans[id].area);
}
for(int i=0;i<10000;i++){ //这个循环统计家族总人数,总面积,总财产数量
if(!vis[i]) continue;
int fa=findFather(i);
if(mp.find(fa)==mp.end()) //mp统计家族总数
mp[fa]=1;
family[fa].area+=ans[i].area;
family[fa].M+=ans[i].M;
family[fa].people++;
family[fa].id=fa;
}
sort(family,family+10000,cmp);
printf("%d\n",mp.size());
for(int i=0;i<mp.size();i++){
printf("%04d %d %.3f %.3f\n",family[i].id,family[i].people,family[i].M/family[i].people,family[i].area/family[i].people);
}
return 0;
}