1114 Family Property (25 分)

 

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 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 (0k5) 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

 

分析:纯粹的模拟题,按题目要求做即可,一开始一直过不了,后来加了个数组vis表示该人还存活,另外3、4、5测试点里有00000这个人要注意。

  1 /**
  2 * Copyright(c)
  3 * All rights reserved.
  4 * Author : Mered1th
  5 * Date : 2019-02-27-13.50.01
  6 * Description : A1114
  7 */
  8 #include
  9 #include
 10 #include
 11 #include
 12 #include
 13 #include<string>
 14 #include
 15 #include
 16 #include
 17 #include<set>
 18 using namespace std;
 19 const int maxn=10010;
 20 struct Family{
 21     int id,m=0; //m是人口数
 22     double Avgsets=0,Avgarea=0;
 23     double sets=0,area=0;//总套数,总面积
 24     bool flag=false;
 25 }family[maxn];
 26 
 27 struct Node{
 28     int id,f,m,k;
 29     vector<int> child;
 30     double Msets,Area;
 31 }node[maxn];
 32 
 33 int father[maxn];
 34 int find_Father(int x){
 35     int a=x;
 36     while(x!=father[x]){
 37         x=father[x];
 38     }
 39     while(a!=father[a]){
 40         int z=a;
 41         a=father[a];
 42         father[z]=x;
 43     }
 44     return x;
 45 }
 46 
 47 void Union(int a,int b){
 48     int faA=find_Father(a);
 49     int faB=find_Father(b);
 50     if(faA!=faB){
 51         if(faA<faB){
 52             father[faB]=faA;
 53         }
 54         else if(faA>faB){
 55             father[faA]=faB;
 56         }
 57     }
 58 }
 59 
 60 bool cmp(Family a,Family b){
 61     if(a.Avgarea!=b.Avgarea) return a.Avgarea>b.Avgarea;
 62     else return a.id<b.id;
 63 }
 64 
 65 bool vis[maxn]={false};
 66 
 67 int main(){
 68 #ifdef ONLINE_JUDGE
 69 #else
 70     freopen("1.txt", "r", stdin);
 71 #endif
 72     int n,k,id,child;
 73     scanf("%d",&n);
 74     for(int i=0;ii;
 75     for(int i=0;i){
 76         scanf("%d",&id);
 77         scanf("%d%d%d",&node[id].f,&node[id].m,&k);
 78         vis[id]=true;
 79         node[id].id=id;
 80         if(node[id].f!=-1){
 81             Union(id,node[id].f);
 82             vis[node[id].f]=true;
 83         }
 84         if(node[id].m!=-1){
 85             Union(id,node[id].m);
 86             vis[node[id].m]=true;
 87         }
 88         for(int j=0;j){
 89             scanf("%d",&child);
 90             node[id].child.push_back(child);
 91             vis[child]=true;
 92             Union(id,child);
 93         }
 94         scanf("%lf%lf",&node[id].Msets,&node[id].Area);
 95     }
 96     for(int i=0;i){
 97         int faI=find_Father(node[i].id);
 98         if(vis[faI]){
 99             family[faI].id=faI;
100             family[faI].area+=node[i].Area;
101             family[faI].sets+=node[i].Msets;
102             family[faI].flag=true;
103         }
104     }
105     int num=0;
106     for(int i=0;i){
107         if(vis[i]){
108             family[find_Father(i)].m++;
109         }
110         if(family[i].flag==true){
111             num++;
112         }
113     }
114     for(int i=0;i){
115         if(family[i].flag!=0){
116             family[i].Avgarea=family[i].area/family[i].m;
117             family[i].Avgsets=family[i].sets/family[i].m;
118         }
119     }
120     sort(family,family+maxn,cmp);
121     cout<endl;
122     for(int i=0;i){
123         printf("%04d %d %.3f %.3f\n",family[i].id,family[i].m,family[i].Avgsets,family[i].Avgarea);
124     }
125     return 0;
126 }

 

转载于:https://www.cnblogs.com/Mered1th/p/10446004.html

你可能感兴趣的:(1114 Family Property (25 分))