PAT甲级1114
来源:互联网 发布:源明雅捏脸数据 编辑:程序博客网 时间:2024/05/16 10:42
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 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; Childi'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:106666 5551 5552 1 7777 1 1001234 5678 9012 1 0002 2 3008888 -1 -1 0 1 10002468 0001 0004 1 2222 1 5007777 6666 -1 0 2 3003721 -1 -1 1 2333 2 1509012 -1 -1 3 1236 1235 1234 1 1001235 5678 9012 0 1 502222 1236 2468 2 6661 6662 1 3002333 -1 3721 3 6661 6662 6663 1 100Sample Output:
38888 1 1.000 1000.0000001 15 0.600 100.0005551 4 0.750 100.000
#include<stdio.h>#include<vector>#include<algorithm>using namespace std;const int maxn = 10000 + 10;struct person{int ID, M_estate, area;person():M_estate(0),area(0){}}p[maxn];int father[maxn];bool flag[maxn] = { false };int findfather(int x){if (x == father[x])return x;else{int F = findfather(father[x]);father[x] = F;return F;}}struct family{int id, m;double as, aa;bool operator<(family f){if (aa != f.aa)return aa >f.aa;elsereturn id < f.id;}};int main(){for (int i = 0; i < maxn; i++){father[i] = i;}int N,t,a,b,k,c;scanf("%d", &N);for (int i = 0; i < N; i++){scanf("%d", &t);flag[t] = true;p[t].ID = t;int fp = findfather(t);scanf("%d%d", &a, &b);if (a != -1){flag[a] = true;int fa = findfather(a);if (fa != fp) father[fa] = fp;}if (b != -1){flag[b] = true;int fb = findfather(b);if (fb != fp) father[fb] = fp;}scanf("%d", &k);for (int j = 0; j < k; j++){scanf("%d", &c);if (c != -1){flag[c] = true;int fc = findfather(c);if (fc != fp) father[fc] = fp;}}scanf("%d%d", &p[t].M_estate, &p[t].area);}vector<family> v; family ff;for (int i = 0; i < maxn; i++){if (flag[i] && father[i] == i){int count = 0;int sets = 0;int areas = 0;int minID = 10000;for (int j = 0; j < maxn; j++){if (flag[j]&&findfather(j)==i){count++;sets += p[j].M_estate;areas += p[j].area;minID = min(minID, j);}}ff.id = minID; ff.as = (double)sets/count; ff.aa = (double)areas/count;ff.m = count;v.push_back(ff);}}sort(v.begin(), v.end());printf("%d\n", v.size());for (int i = 0; i < v.size(); i++){printf("%04d %d %.3f %.3f\n", v[i].id, v[i].m, v[i].as, v[i].aa);}return 0;}
- PAT 甲级 1114
- PAT甲级1114
- PAT 甲级
- *浙大PAT甲级 1114 并查集
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- QT之深入理解QThread
- Python设计模式之单例模式实例
- JSP页面动态生成表格并为表格添加事件(续)
- ThreadLocal简单分析
- 环境搭建之Linux系统安装MySQL(centos7.2)
- PAT甲级1114
- 编写openMP的心得
- MySQL部分DDL(数据库定义语言(Data Definition Language)),MySQL数据库表中字段的数据类型
- MTK battery 原理
- Open vSwitch的ovs-vsctl命令详解
- [LeetCode] Longest Substring Without Repeating Characters
- [Leetcode] #42 Trapping Rain Water
- 每日小记 2017.2.14
- 树状数组求逆序对与luogu1908