PAT甲级——A1114 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 (≤). 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 (0) 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

两个代码,仅供参考:

 1 #include <iostream>
 2 #include <vector>
 3 #include <set>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 struct Node
 8 {
 9     int ID, nums = 0;
10     double ss = 0.0, aa = 0.0;
11 };
12 int n;
13 double num[10000] = { 0.0 }, area[10000] = { 0.0 };//房子数量//房子面积
14 int mark[10000];//标记属于哪个家族
15 vector<set<int>>sets(10000);//记录每个家庭有多少人
16 vector<Node*>res;
17 void combin(int my, int other)//合并家族
18 {
19     for (auto ptr = sets[other].begin(); ptr != sets[other].end(); ++ptr)//将其他成员进行同化
20     {
21         mark[*ptr] = my;
22         sets[my].insert(*ptr);
23     }
24     sets[other].clear();//删除其他成员的标记
25 }
26 int main()
27 {    
28     cin >> n;    
29     fill(mark, mark + 10000, -1);
30     for (int i = 0; i < n; ++i)
31     {
32         int my, parent, k, child, nn, aa;
33         cin >> my;
34         if (mark[my] == -1)//还未标记是哪个家族
35         {
36             mark[my] = my;//就以自己为标记
37             sets[mark[my]].insert(my);
38         }
39         for (int i = 0; i < 2; ++i)//添加父母
40         {
41             cin >> parent;
42             if (parent == -1)
43                 continue;
44             if (mark[parent] == -1)//家人未标记
45             {
46                 mark[parent] = mark[my];
47                 sets[mark[my]].insert(parent);
48             }
49             else if (mark[parent] != -1 && mark[parent] != mark[my])//家人与自己标记不同
50                 combin(mark[my], mark[parent]);//将家人同化为自己标记
51         }
52         cin >> k;
53         while (k--)
54         {
55             cin >> child;
56             if (mark[child] == -1)//孩子未标记
57             {
58                 mark[child] = mark[my];
59                 sets[mark[my]].insert(child);
60             }
61             else if (mark[child] != -1 && mark[child] != mark[my])//孩子与自己标记不同
62                 combin(mark[my], mark[child]);//将孩子同化为自己标记
63         }
64         cin >> nn >> aa;
65         num[my] = nn;
66         area[my] = aa;
67     }
68     for (int i = 0; i < sets.size(); ++i)
69     {
70         if (sets[i].empty())
71             continue;
72         Node* temp = new Node;
73         temp->ID = *sets[i].begin();
74         temp->nums = sets[i].size();
75         for (auto ptr = sets[i].begin(); ptr != sets[i].end(); ++ptr)
76         {
77             temp->aa += area[*ptr];
78             temp->ss += num[*ptr];
79         }
80         temp->ss /= temp->nums;
81         temp->aa /= temp->nums;
82         res.push_back(temp);
83     }
84     sort(res.begin(), res.end(), [](Node*a, Node*b) {
85         if (abs(a->aa - b->aa) < 0.0001)
86             return a->ID < b->ID;
87         else
88             return a->aa > b->aa; });
89     cout << res.size() << endl;
90     for (auto v : res)
91         printf("%04d %d %0.3f %0.3f\n", v->ID, v->nums, v->ss, v->aa);
92     return 0;
93 }
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Estate{//存储集合最小id、集合结点个数num、集合总sets、集合总area
 4     int id,num=0;
 5     double sets=0.0,area=0.0;
 6 };
 7 bool cmp(const Estate&e1,const Estate&e2){//比较函数
 8     return e1.area!=e2.area?e1.area>e2.area:e1.id<e2.id;
 9 }
10 int father[10005];//并查集底层数组
11 int findFather(int x){//查找根节点
12     if(x==father[x])
13         return x;
14     int temp=findFather(father[x]);
15     father[x]=temp;
16     return temp;
17 }
18 void unionSet(int a,int b){//合并两个集合
19     int ua=findFather(a),ub=findFather(b);
20     if(ua!=ub)
21         father[max(ua,ub)]=min(ua,ub);//以小的id作为根节点
22 }
23 unordered_map<int,pair<double,double>>idEstate;
24 unordered_map<int,Estate>familyEstate;
25 int main(){
26     int N;
27     scanf("%d",&N);
28     iota(father,father+10005,0);
29     while(N--){//读取数据并合并相关集合
30         int id,f,m,k,a;
31         scanf("%d%d%d%d",&id,&f,&m,&k);
32         if(f!=-1)
33             unionSet(id,f);
34         if(m!=-1)
35             unionSet(id,m);
36         while(k--){
37             scanf("%d",&a);
38             unionSet(id,a);
39         }
40         scanf("%lf%lf",&idEstate[id].first,&idEstate[id].second);//记录id和对应的sets、area
41     }
42     for(int i=0;i<10005;++i){//遍历并查集数组
43         int temp=findFather(i);
44         if(temp!=i)//根节点不等于本身
45             ++familyEstate[temp].num;//递增根节点下集合的结点个数
46         if(idEstate.find(i)!=idEstate.cend()){//累加集合的总sets、总area
47             familyEstate[temp].sets+=idEstate[i].first;
48             familyEstate[temp].area+=idEstate[i].second;
49         }
50     }
51     vector<Estate>v;
52     for(auto i=familyEstate.begin();i!=familyEstate.end();++i){//将familyEstate中的值搬迁到vector中,并更新相关信息
53         (i->second).id=i->first;
54         ++(i->second).num;//集合下结点个数没有统计根节点,所以要+1
55         (i->second).sets/=(i->second).num;
56         (i->second).area/=(i->second).num;
57         v.push_back(i->second);
58     }
59     sort(v.begin(),v.end(),cmp);//排序
60     printf("%d\n",v.size());
61     for(int i=0;i<v.size();++i)
62         printf("%04d %d %.3f %.3f\n",v[i].id,v[i].num,v[i].sets,v[i].area);
63     return 0;
64 }

猜你喜欢

转载自www.cnblogs.com/zzw1024/p/11456327.html