admin 管理员组

文章数量: 1087131

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

并查集模板

father[]
type find(type x)
{while(x!=father[x])x=father[x];return x;
}
void Union(type a,type b)
{type fa=find(a);type fb=find(b);father[fa]=fb;无所谓大小/*小序号作为老父亲标记if(fa>fb){father[fa]=fb;}else{father[fb]=fa;}*/
}
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,id,fa,mo,k,t;
int father[10005];
bool vis[10005];
int no[10005];
struct Owner
{int ID;int sets;int area;
}O[10005];
struct Family
{int ID;int num;double sets;double area;
}ans[10005];
int find(int x)
{while(x!=father[x])x=father[x];return x;
}
void Union(int a,int b)
{int fa=find(a);int fb=find(b);if(fa<fb){father[fb]=fa;}else{father[fa]=fb;}
}
bool cmp(Family a,Family b)
{if(a.area==b.area)return a.ID<b.ID;elsereturn a.area>b.area;
}
int main()
{for(int i=0;i<10000;i++)father[i]=i;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d%d%d%d",&id,&fa,&mo,&k);O[id].ID=id;vis[id]=1;if(fa!=-1){vis[fa]=1;Union(id,fa);}if(mo!=-1){vis[mo]=1;Union(id,mo);}for(int j=0;j<k;j++){scanf("%d",&t);vis[t]=1;Union(id,t);}scanf("%d%d",&O[id].sets,&O[id].area);}int fami=0;for(int i=0;i<10000;i++){if(!vis[i])continue;t=find(i);if(!no[t]){no[t]=++fami;ans[no[t]].ID=t;}ans[no[t]].num++;ans[no[t]].sets+=O[i].sets;ans[no[t]].area+=O[i].area;}for(int i=1;i<=fami;i++)/不能等输出时再除,因为排序要用到/num的结果值;{ans[i].sets/=ans[i].num;ans[i].area/=ans[i].num;}sort(ans+1,ans+1+fami,cmp);printf("%d\n",fami);for(int i=1;i<=fami;i++){printf("%04d %d %.3f %.3f\n",ans[i].ID,ans[i].num,ans[i].sets,ans[i].area);}return 0;
}

本文标签: 1114 Family Property (25 分)并查集模板