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.
知识点:并查集
注意并查集的写法
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 using namespace std; 5 const int maxn = 10003; 6 7 int ManSet[maxn]; 8 int Property[maxn][2]; 9 int used[maxn]; 10 //set<int> 11 12 bool cmp(int a,int b){ 13 if(Property[a][1]/(-ManSet[a]+0.0)==Property[b][1]/(-ManSet[b]+0.0)){ 14 return a<b; 15 } 16 return Property[a][1]/(-ManSet[a]+0.0)>Property[b][1]/(-ManSet[b]+0.0); 17 } 18 19 int Find(int v){ 20 if(ManSet[v]<=-1){ 21 return v; 22 } 23 return ManSet[v] = Find(ManSet[v]); 24 } 25 26 int Unite(int n1, int n2){ 27 28 if(n1<n2){ 29 ManSet[n1] += ManSet[n2]; 30 ManSet[n2] = n1; 31 return n1; 32 }else{ 33 ManSet[n2] += ManSet[n1]; 34 ManSet[n1] = n2; 35 return n2; 36 } 37 } 38 39 int main(int argc, char *argv[]) { 40 int n; 41 fill(ManSet,ManSet+maxn,-1); 42 fill(Property[0],Property[0]+maxn*2,0); 43 fill(used,used+maxn,0); 44 45 scanf("%d",&n); 46 int man,fa,mo,k,tmpc,m,area; 47 for(int i=0;i<n;i++){ 48 //printf("\n"); 49 scanf("%d %d %d",&man,&fa,&mo); 50 used[man] = used[fa] = used[mo] = 1; 51 //printf("\n. %d %d,%d\n",Find(man),i,n); 52 man = Find(man); //printf("%d ",man); 53 if(fa!=-1){ 54 fa = Find(fa); 55 if(fa!=man){ 56 man = Unite(man,fa); //printf("%d ",man); 57 } 58 } 59 man = Find(man); 60 if(mo!=-1){ 61 mo = Find(mo); 62 if(mo!=man){ 63 man = Unite(man,mo);//printf("%d ",man); 64 } 65 } 66 scanf("%d",&k); 67 for(int j=0;j<k;j++){ 68 scanf("%d",&tmpc); 69 used[tmpc] = 1; 70 tmpc = Find(tmpc); 71 man = Find(man); 72 if(man!=tmpc){ 73 man = Unite(man,tmpc);//printf("%d ",man); 74 } 75 } 76 scanf("%d %d",&m,&area); 77 Property[man][0] += m; 78 Property[man][1] += area; 79 } 80 for(int i=0;i<maxn;i++){ 81 if(used[i]==1&&ManSet[i]>0&&Property[i][0]>0){ 82 int an = Find(i); 83 Property[an][0]+=Property[i][0]; 84 Property[an][1]+=Property[i][1]; 85 } 86 } 87 vector<int> list; 88 for(int i=0;i<maxn;i++){ 89 if(used[i]==1&&ManSet[i]<0){ 90 list.push_back(i); 91 //printf("%04d %d %.3f %.3f\n",i,(-ManSet[i]),Property[i][0]/(-ManSet[i]+0.0),Property[i][1]/(-ManSet[i]+0.0)); 92 } 93 } 94 sort(list.begin(), list.end(), cmp); 95 printf("%d\n",list.size()); 96 for(int i=0;i<list.size();i++){ 97 printf("%04d %d %.3f %.3f\n",list[i],(-ManSet[list[i]]),Property[list[i]][0]/(-ManSet[list[i]]+0.0),Property[list[i]][1]/(-ManSet[list[i]]+0.0)); 98 } 99 }
posted on 2018-11-07 20:11 iojafekniewg 阅读(333) 评论(0) 编辑 收藏 举报