1114 Family Property(PAT甲级)(并查集)(c++版)(python版)(附代码注释)

1114 Family Property (25 分)


原文链接: http://kakazai.cn/index.php/Kaka/Pat/query/id/204

题目

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805356599820288

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 information of a person who owns estate in the format:

ID` `Father` `Mother` k Child1⋯Childk Mestate `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; Mestate 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 AVGsets AVGarea

where 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

题意分析

每个家族有个属性:1)家族人数,2)家族中最小编号,3)家族总房产数,人均房产数 ,4)家族总房产面积,人均房产面积, 5)家族编号

家族A中任意一个人若与家族B中任意一个人存在亲人关系,则合并A、B为更大的家族。

若将家族看成集合,问题变为,若集合A中任意一个元素与集合B中任意一个元素存在关系,则合并集合A、B

因此,可以使用并查集。

数值范围分析:1)家族数[1,1000];2)人数[1,9999]

知识点与坑点

  • 知识点

1)并查集

  • 坑点

1)

一、并查集

算法思路

1 逐次合并每个家的第一个成员跟其他所有成员所在的集合,同一个集合代表同一个家族。

2 先用set存放好所有人;将每个家的房产面积和房产数存放在第一个成员名下;

3 遍历set,找出每个人所在的家族,即每个人的根结点,计算根结点个数。累加该家族的人数、房产数,房产面积。

4 对家族的房产数、房产面积求平均值。

5 按要求排序,并输出。

代码-c++版
#include <iostream>
#include<algorithm>
#include<set>
using namespace std;

/*数值范围*/ 
const int maxn = 10000;	//最多有9999个人 

/*所有变量*/ 
struct node{	//家族的属性 
 int minid,n;	//家族中最小编号,人数 
 double sets, areas;	//总房产数,总房产面积
 double av_sets, av_areas; //人均房产,人均面积 
}family[maxn];
int father[maxn],members[maxn];	//并查集数组,某家人数
double sets[maxn],areas[maxn];	//某家房产数,房产面积 

/* 并查集-初始化*/
void initiate(){
  for(int i=0;i<maxn;i++){
   father[i]=i;
  }
}

/* 并查集-找根结点+压缩路径*/
int findroot(int a){
 int x = a;
 while(a != father[a]) {
  a = father[a];
 }
 int temp; 
 while(x != father[x]){
  temp = father[x];
  father[x] = a;
  x = temp;
 }
 return a;
}

/* 并查集-合并集合+保证根结点编号最小*/ 
 void union_ab(int a,int b){
  int fa = findroot(a);
  int fb = findroot(b);
  if(fa <= fb){ 
   father[fb] = fa;
  }else{
   father[fa] = fb;
  }
 }
 
/* 比较规则 */
int cmp(node a,node b){
 if(a.av_areas != b.av_areas) return a.av_areas > b.av_areas; //按平均房产面积降序 
 else return a.minid < b.minid;	//按家族最小编号升序 
} 

int main(){
 initiate();
	
 int n;
 scanf("%d",&n);
	
 /* 处理每个家庭 */
 set<int> all; //存放所有人 
set<int>::iterator it;
 int id1,id2,m; 
 for(int i = 0; i < n; i++){  
  
  /* 处理每家的第一人id1 */ 
  scanf("%d",&id1);
  all.insert(id1);
  
  /* 处理父母 */ 
  for(int p = 0; p < 2; p++){
   scanf("%d",&id2);
   if(id2==-1)continue; //过世了不处理 
   union_ab(id1,id2); //合并双方所在集合
   all.insert(id2); 
  }
  
  /* 处理孩子 */ 
  scanf("%d",&m);
  for(int ch = 0; ch < m; ch++){
   scanf("%d",&id2);
   union_ab(id1,id2);	//合并双方所在集合
   all.insert(id2);  
  }
  
  /* 处理财产 */ 
  double  tsets,tarea; 
  scanf("%lf %lf",&tsets, &tarea);
  sets[id1] = tsets;	//id1家的房产数 
  areas[id1] = tarea; //id1家的房产面积 
 } 
	
 /* 找出每人所在的家族编号,并加总各个属性 */
  set<int> familyid;	//存放所有家族编号 
  for(it = all.begin(); it != all.end(); it++){
   int root = findroot(*it);	//该人的家族编号 
   familyid.insert(root);
   family[root].minid = root;	//因为合并且压缩路径时,已经将根结点设为集合中最小编号 
  family[root].sets += sets[*it];	//家族总房产数 
  family[root].areas += areas[*it]; //家族总房产面积
  family[root].n += 1;	//该家族的人数 
  } 
  
 /* 求家族的人均房产数,人均房产面积 */
 for(it = familyid.begin(); it != familyid.end(); it++){
  family[*it].av_sets = family[*it].sets / family[*it].n;
  family[*it].av_areas = family[*it].areas / family[*it].n;
 } 
	
 /* 对各家族进行排序 */
 sort(family, family+maxn,cmp);
	
 /* 按要求输出 */
 printf("%d\n",familyid.size()) ;
 for (int i = 0; i < maxn; i++) {
  if(family[i].n == 0) break;	//该家族人数是0,则证明其后全是无效家族 
  printf("%04d %d %.3lf %.3lf\n", family[i].minid, family[i].n, family[i].av_sets, family[i].av_areas);
    }	
    
 return 0;
}
代码-python版
#!/usr/bin/python3
#code-python(3.6)

# 并查集-初始化
father = []
for i in range(10000):
    father.append(i)

#并查集-找根结点
def findroot(a):
    while(father[a]!=a):
        a = father[a]
    return a

#并查集-合并集合+保证根结点最小
def union_ab(a,b):
    fa = findroot(a)
    fb = findroot(b)
    if(fa <= fb):
        father[fb] = fa
    else:
        father[fa] = fb

n = int(input())
all = []    #存放所有人
property = {}   #存放每个人名下财产

#处理每个家
for i in range(n):
    line = input().split(" ")
#整理家庭成员名单
    ch = int(line[3])   #孩子数
    family = line[0:3]+line[4:4+ch] #家庭成员,包括'-1'
    while '-1' in family:   #去掉'-1'
        family.remove('-1')
    family = list(map(int, family)) #将字符串转为整数
#将家庭成员存放入所有人名单中
    all += family
#把家庭财产存储到家庭第一位成员名下
    property[family[0]] = [1,float(line[-2]),float(line[-1])]
#合并家庭成员所在的集合
    for p in family:
        union_ab(family[0],p)

all = list(set(all))  #所有人,去重
root = {}   #存放所有家族和其属性
#累计每个人所在的家族的各属性
for p in all:
    root_p = findroot(p)    #p所在的家族编号
    if root_p not in root:
        root[root_p] = [0,0,0]
    if p not in property:
        property[p] = [1,0,0]
    root[root_p] = [root[root_p][i]+property[p][i] for i in range(3)]   #累计家族人数和财产

#求出每个家族的人均房产数,人均房产面积
for x,y in root.items():
    root[x][1] = root[x][1] /root[x][0]
    root[x][2] = root[x][2] /root[x][0]

#按要求排序
x = sorted(root.items(),key = lambda x:(-x[1][2],x[0]))

#按要求输出
print(len(x))
for t in x:
    print("%04d %d %.3f %.3f" % (t[0],t[1][0],t[1][1],t[1][2]))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值