1114. Family Property (25)
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 100Sample Output:
3 8888 1 1.000 1000.000 0001 15 0.600 100.000 5551 4 0.750 100.000
【解析】 此题其实就是叫你判断一个家庭里面只要有亲戚关系的有几个人?然后房子平均分,人均有多少套,人均面积有多少,那么怎么判断是亲戚和同属于一个家庭这个就 有点麻烦了不过在这里用并查集感觉再合适不过了,其实并查集是这样的,可以理解成一棵树吧,树肯定有根结点,根结点其实就是老大,所以我们就不断的向上去寻找 那个老大,判断两人是否是亲戚。如果是的话就是人数加1.此处参考了大佬的写法。
#include<cstdio> #include<iostream> #include<string> #include<algorithm> using namespace std; struct data1 { int id; int fid; int mid; int num; int area; int c[15]; }Data[1001]; struct data2 { int id; int people; double num; double area; bool flag=false; }data[10001]; int root[10001]; bool f[10001]; int find(int a)//寻找自己的上级,只有自己的上级特的上级才会是自己 { while(a!=root[a]) a=root[a]; return a; } void Union(int a,int b) { int A=find(a); int B=find(b); if(A>B) root[A]=B; else if(A<B) root[B]=A; } int cmp(data2 a,data2 b) { if(a.area!=b.area) return a.area>b.area; else return a.id<b.id; } int main() { int n,k,count1=0,i,j; scanf("%d",&n); for(i=0;i<10000;i++)//每个人的上级都是自己 { root[i]=i; } for(i=0;i<n;i++) { scanf("%d%d%d%d",&Data[i].id,&Data[i].fid,&Data[i].mid,&k); f[Data[i].id]=true; if(Data[i].fid!=-1) { f[Data[i].fid]=true; Union(Data[i].fid,Data[i].id); } if(Data[i].mid!=-1) { f[Data[i].mid]=true; Union(Data[i].mid,Data[i].id); } for(int j=0;j<k;j++) { scanf("%d",&Data[i].c[j]); f[Data[i].c[j]]=true; Union(Data[i].c[j],Data[i].id); } scanf("%d%d",&Data[i].num,&Data[i].area); } for(i=0;i<n;i++) { int id=find(Data[i].id); data[id].id=id; data[id].num+=Data[i].num; data[id].area+=Data[i].area; data[id].flag=true; } for(i=0;i<10000;i++) { if(f[i]) data[find(i)].people++; if(data[i].flag) count1++; } for(i=0;i<10000;i++) { if(data[i].flag) { data[i].num=(double)(data[i].num*1.0/data[i].people); data[i].area=(double)(data[i].area*1.0/data[i].people); } } sort(data,data+10000,cmp); printf("%d\n",count1); for(i=0;i<count1;i++) { printf("%04d %d %.3f %.3f\n",data[i].id,data[i].people,data[i].num,data[i].area); } return 0; }