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:
/*
Data: 2019-06-23 15:15:59
Problem: PAT_A1114#Family Property
AC: 42:40 题目大意:
统计家庭拥有的财产
输入:
第一行给出,户主人数N<=1000
接下来N行,my_id,dad_id,mom_id,m,kids,房产数,总面积
输出:
第一行给出,家庭数N
接来下N行,min_id,成员数,平均房产数,平均面积(三位小数)
平均面积递减,min_id递增 基本思路:
输入时按id做并查集,min_id作为父结点,存储户主信息
遍历户主,统计各家庭信息
排序
*/
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e5+;
int fa[M];
struct node
{
int id;
set<int> member;
double sets;
double area;
}info[M]; int Father(int v)
{
int x=v,s;
while(fa[v] != v)
v = fa[v];
while(fa[x] != x)
{
s = fa[x];
fa[x] = v;
x = s;
}
return v;
} void Union(int v1, int v2)
{
int f1 = Father(v1);
int f2 = Father(v2);
if(f1 < f2)
fa[f2] = f1;
else
fa[f1] = f2;
} bool cmp(node a, node b)
{
int s1=a.member.size();
int s2=b.member.size();
if(a.area/s1 != b.area/s2)
return a.area/s1 > b.area/s2;
else
return a.id < b.id;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE for(int i=; i<M; i++)
{
fa[i]=i;
info[i].id=i;
info[i].sets=;
info[i].area=;
} int n,my,dad,mom,m,kid;
int input[M];
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d%d%d%d", &my,&dad,&mom,&m);
input[i]=my;
info[my].member.insert(my);
if(dad != -)
{
Union(my,dad);
info[my].member.insert(dad);
}
if(mom != -)
{
Union(my,mom);
info[my].member.insert(mom);
}
for(int j=; j<m; j++)
{
scanf("%d", &kid);
Union(my,kid);
info[my].member.insert(kid);
}
scanf("%lf%lf", &info[my].sets, &info[my].area);
}
set<int> family;
for(int i=; i<n; i++)
{
my = input[i];
int f = Father(my);
family.insert(f);
if(f == my)
continue;
info[f].sets += info[my].sets;
info[f].area += info[my].area;
for(auto it=info[my].member.begin(); it!=info[my].member.end(); it++)
info[f].member.insert(*it);
}
vector<node> ans;
for(auto it=family.begin(); it!=family.end(); it++)
ans.push_back(info[*it]);
sort(ans.begin(),ans.end(),cmp);
printf("%d\n", ans.size());
for(int i=; i<ans.size(); i++){
int sum = ans[i].member.size();
printf("%04d %d %.3f %.3f\n", ans[i].id,sum,ans[i].sets/sum,ans[i].area/sum);
} return ;
}