PAT_A1114#Family Property
作者:互联网
Source:
Description:
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 (≤). Then N lines follow, each gives the infomation of a person who owns estate in the format:
ID
Father
Mother
k Child1⋯Childk MestateArea
where
ID
is a unique 4-digit identification number for each person;Father
andMother
are theID
's of this person's parents (if a parent has passed away,-1
will be given instead); k (0) is the number of children of this person; Childi's are theID
's of his/her children; Mestate is the total number of sets of the real estate under his/her name; andArea
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 AVGareawhere
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
Keys:
Attention:
- Union函数
Code:
1 /* 2 Data: 2019-06-23 15:15:59 3 Problem: PAT_A1114#Family Property 4 AC: 42:40 5 6 题目大意: 7 统计家庭拥有的财产 8 输入: 9 第一行给出,户主人数N<=1000 10 接下来N行,my_id,dad_id,mom_id,m,kids,房产数,总面积 11 输出: 12 第一行给出,家庭数N 13 接来下N行,min_id,成员数,平均房产数,平均面积(三位小数) 14 平均面积递减,min_id递增 15 16 基本思路: 17 输入时按id做并查集,min_id作为父结点,存储户主信息 18 遍历户主,统计各家庭信息 19 排序 20 */ 21 #include<cstdio> 22 #include<set> 23 #include<vector> 24 #include<algorithm> 25 using namespace std; 26 const int M=1e5+10; 27 int fa[M]; 28 struct node 29 { 30 int id; 31 set<int> member; 32 double sets; 33 double area; 34 }info[M]; 35 36 int Father(int v) 37 { 38 int x=v,s; 39 while(fa[v] != v) 40 v = fa[v]; 41 while(fa[x] != x) 42 { 43 s = fa[x]; 44 fa[x] = v; 45 x = s; 46 } 47 return v; 48 } 49 50 void Union(int v1, int v2) 51 { 52 int f1 = Father(v1); 53 int f2 = Father(v2); 54 if(f1 < f2) 55 fa[f2] = f1; 56 else 57 fa[f1] = f2; 58 } 59 60 bool cmp(node a, node b) 61 { 62 int s1=a.member.size(); 63 int s2=b.member.size(); 64 if(a.area/s1 != b.area/s2) 65 return a.area/s1 > b.area/s2; 66 else 67 return a.id < b.id; 68 } 69 70 int main() 71 { 72 #ifdef ONLINE_JUDGE 73 #else 74 freopen("Test.txt", "r", stdin); 75 #endif // ONLINE_JUDGE 76 77 for(int i=0; i<M; i++) 78 { 79 fa[i]=i; 80 info[i].id=i; 81 info[i].sets=0; 82 info[i].area=0; 83 } 84 85 int n,my,dad,mom,m,kid; 86 int input[M]; 87 scanf("%d", &n); 88 for(int i=0; i<n; i++) 89 { 90 scanf("%d%d%d%d", &my,&dad,&mom,&m); 91 input[i]=my; 92 info[my].member.insert(my); 93 if(dad != -1) 94 { 95 Union(my,dad); 96 info[my].member.insert(dad); 97 } 98 if(mom != -1) 99 { 100 Union(my,mom); 101 info[my].member.insert(mom); 102 } 103 for(int j=0; j<m; j++) 104 { 105 scanf("%d", &kid); 106 Union(my,kid); 107 info[my].member.insert(kid); 108 } 109 scanf("%lf%lf", &info[my].sets, &info[my].area); 110 } 111 set<int> family; 112 for(int i=0; i<n; i++) 113 { 114 my = input[i]; 115 int f = Father(my); 116 family.insert(f); 117 if(f == my) 118 continue; 119 info[f].sets += info[my].sets; 120 info[f].area += info[my].area; 121 for(auto it=info[my].member.begin(); it!=info[my].member.end(); it++) 122 info[f].member.insert(*it); 123 } 124 vector<node> ans; 125 for(auto it=family.begin(); it!=family.end(); it++) 126 ans.push_back(info[*it]); 127 sort(ans.begin(),ans.end(),cmp); 128 printf("%d\n", ans.size()); 129 for(int i=0; i<ans.size(); i++){ 130 int sum = ans[i].member.size(); 131 printf("%04d %d %.3f %.3f\n", ans[i].id,sum,ans[i].sets/sum,ans[i].area/sum); 132 } 133 134 return 0; 135 }
标签:PAT,estate,family,area,int,ID,fa,Property,A1114 来源: https://www.cnblogs.com/blue-lin/p/11073307.html