日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++学习之路 | PTA(甲级)—— 1114 Family Property (25分)(带注释)(并查集)(精简)

發布時間:2024/7/23 c/c++ 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++学习之路 | PTA(甲级)—— 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 k Child
?1
?? ?Child
?k
?? M
?estate
?? 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; Child
?i
?? 's are the ID’s of his/her children; M
?estate
?? 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 AVG
?sets
?? AVG
?area
??
where ID is the smallest ID in the family; M is the total number of family members; AVG
?sets
?? is the average number of sets of their real estate; and AVG
?area
?? 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

#include<iostream> #include<set> #include<vector> #include<map> #include<algorithm> using namespace std; const int maxn = 10000; int pre[maxn]; struct node {//存儲開始輸入的人的基本信息int id, house;double area; }; struct family {//輸出的家庭結構體int id, number;double house, area; }; bool cmp(family a, family b)//自定義比較函數 {if (a.area != b.area)return a.area > b.area;else return a.id < b.id; } int find(int x)//并查集模板,查找 {while (x != pre[x]) x = pre[x];return x; } void merge(int x, int y)//并查集模板,合并 {int a = find(x);int b = find(y);if (a != b) pre[a] = b; } int main() {int n, x;cin >> n;set<int>s;//存儲每人的編號,默認升序map<int, node>nodes;//記錄人的房產信息for (int i = 0; i < maxn; i++)pre[i] = i;//初始化pre數組for (int i = 0; i < n; i++){int id, p1, p2, k, house, id1;//分別記錄編號 父 母 k 孩子1 ... 孩子k 房產套數double area;// 總面積cin >> id >> p1 >> p2 >> k;s.insert(id);//插入idif (p1 != -1)//有爸爸{s.insert(p1);merge(id, p1);}if (p2 != -1)//有媽媽{s.insert(p2);merge(id, p2);}for (int j = 0; j < k; j++)//隨后的家庭成員{cin >> id1;s.insert(id1);merge(id1, id);}cin >> house >> area;nodes[id] = node{ id,house,area };//記錄該人的家庭信息}set<int>s1;//記錄家庭編號(也可以理解記錄著每個家庭的老大)map<int, vector<int>>m;//記錄家庭編號()和成員編號for (auto it = s.begin(); it != s.end(); it++){int fa = find(*it);m[fa].push_back(*it);s1.insert(fa);}cout << s1.size() << endl;//輸出家庭的個數vector<family>v1;for (auto it = s1.begin(); it != s1.end(); it++){int fa = *it;double house = 0, area = 0;for (auto tmp = 0; tmp < m[fa].size(); tmp++)//將每個家庭的成員及其房產計算匯總,放入family結構體中{house += nodes[m[fa][tmp]].house;area += nodes[m[fa][tmp]].area;}v1.push_back(family{ m[fa][0],int(m[fa].size()),house * 1.0 / (int)m[fa].size(),area * 1.0 / (int)m[fa].size() });}sort(v1.begin(), v1.end(), cmp);//進行排序for (int i = 0; i < v1.size(); i++) {printf("%04d %d %.3lf %.3lf", v1[i].id, v1[i].number, v1[i].house, v1[i].area);if (i < v1.size() - 1) puts("");} }

總結

以上是生活随笔為你收集整理的C++学习之路 | PTA(甲级)—— 1114 Family Property (25分)(带注释)(并查集)(精简)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。