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 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.
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
注意ID没有说不等于0.因此判断时为大于等于0,而不是大于。否则测试点2、4会错误。
我用的深度优先搜索求联通分量来完成的这道题,另外用并查集好像也比较方便。
由于ID不连续,所以求连通分量之前先将所有ID放到vector里面,而且用邻接表存储图更节省空间和时间。
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 6 bool marked[10000]; 7 vector<vector<int>> G(10000); 8 int estate[10000], area[10000]; 9 10 struct family 11 { 12 int id = 10000, size = 0; 13 double estate = 0.0, area = 0.0; 14 }; 15 16 void connect(int id) 17 { 18 int t; 19 cin >> t; 20 if (t >= 0) 21 { 22 G[id].push_back(t); 23 G[t].push_back(id); 24 } 25 } 26 27 void dfs(int s, family& f) 28 { 29 marked[s] = true; 30 f.size++; 31 if (s < f.id) f.id = s; 32 f.area += area[s]; 33 f.estate += estate[s]; 34 for (int w : G[s]) 35 if (!marked[w]) dfs(w, f); 36 } 37 38 bool cmp(family a, family b) 39 { 40 if (a.area != b.area) return a.area > b.area; 41 else return a.id < b.id; 42 } 43 44 int main() 45 { 46 int N; 47 cin >> N; 48 int i, j, id, k; 49 vector<int> allId; 50 for (i = 0; i < N; i++) 51 { 52 cin >> id; 53 allId.push_back(id); 54 for (j = 0; j < 2; j++) 55 connect(id); 56 cin >> k; 57 for (j = 0; j < k; j++) 58 connect(id); 59 cin >> estate[id] >> area[id]; 60 } 61 vector<family> v; 62 for (int id : allId) 63 { 64 if (!marked[id]) 65 { 66 family f; 67 dfs(id, f); 68 f.area /= f.size; 69 f.estate /= f.size; 70 v.push_back(f); 71 } 72 } 73 sort(v.begin(), v.end(), cmp); 74 cout << v.size() << endl; 75 for (i = 0; i < v.size(); i++) 76 printf("%04d %d %.3f %.3f ", v[i].id, v[i].size, v[i].estate, v[i].area); 77 return 0; 78 }