PAT A1114 Family Property (25分) GPLT L2-007 家庭房产 (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.
题目详情见PAT
原题链接
题目大意
输入每一个家庭父母,孩子,房产等信息,最后计算求出每一个家族总人数,平均房产等信息
大概思路
本题主要采用并查集操作,首先设置两个结构体,一个结构体data用于保存每个家庭的详细信息,另一个结构体head用于保存每个家族信息,在输入家庭信息时同时完成Union操作,因为题目要求输出较小编号,所以Union操作要注意父节点大小。同时设置一个bool型vis数组用来表示此人是否出现,方便后面计算家族人数。find操作时可以采用路径压缩(不采用好像也可以AC)。
代码
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=10010;
//该结构体用来保存家庭信息
struct data{
int myId,faId,moId,num,area;
int chId[10];
}list[1010];
//该结构体用来保存家族信息
struct head{
int id,p_num;//id为此家族最小编号,p_num为家族人数
double r_num,area;//r_num为房产数,area为房产面积
bool isHead=false;//isHead为true时表示该id为家族最小id
}ans[maxn];
int root[maxn];//设置父节点
bool vis[maxn]={
false};
//比较函数,根据题目要求,先按平均面积大小排列,再按最小id排列
bool cmp(head a,head b){
if(a.area!=b.area){
return a.area>b.area;
}else{
return a.id<b