PAT 1114 Family Property(dfs)

题目链接:

1114 Family Property

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:
I D ID , F a t h e r Father , M o t h e r Mother , k k , C h i l d 1 C h i l d k    M e s t a t e ,    A r e a 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; C h i l d i Child'_i s are the ID’s of his/her children; M e s t a t e 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:
I D ID , M M , A V G s e t s AVG_{sets} , A V G a r e a AVG_{area}
where ID is the smallest ID in the family; M is the total number of family members; A V G s e t s AVG_{sets} is the average number of sets of their real estate; and A V G a r e a 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

思路:

主要考察连通分量的求法。这里采用深度优先搜索来解决,当然用并查集也可以。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map> 
#define MAX 99999999
using namespace std;
struct Family{
	int id;
	int father,mother;
	vector<int> child;
	int estate,area;
	Family(){
		father=-1;mother=-1;//初始化
		estate=0;area=0;
	}
}p[10005];
map<int,int> m;
int vis[10005];
struct Person{
	int id,estate,area;
};
struct Total{
	int id;
	int count;
	double averEstate;
	double averArea;//最后结果
};
vector<Person> f[1005];//统计每个联通块的信息
vector<Total> ans;//结果
int cnt=0;
bool cmp(const Total &a,const Total &b){//排序方式
	if(a.averArea!=b.averArea) return a.averArea>b.averArea;
	return a.id<b.id;
}
void dfs(int id){
	Person tmp;
	tmp.id=id;tmp.estate=p[id].estate;tmp.area=p[id].area;
	f[cnt].push_back(tmp);
	if(p[id].father!=-1&&!vis[p[id].father]){//向上搜索父辈的信息
		vis[p[id].father]=1;
		dfs(p[id].father);
	}
	if(p[id].mother!=-1&&!vis[p[id].mother]){//向上搜索母亲节点的信息
		vis[p[id].mother]=1;
		dfs(p[id].mother);
	}
	if(p[id].child.size()){
		for(int j=0;j<p[id].child.size();j++){//向下搜索孩子节点的信息
			if(!vis[p[id].child[j]]){
				vis[p[id].child[j]]=1;
				dfs(p[id].child[j]);
			}
		}
	}
}
int main(int argc, char** argv) {
	int n;scanf("%d",&n);
	for(int i=0;i<n;i++){
		int id;scanf("%d",&id);
		m[i]=id;
		scanf("%d %d ",&p[id].father,&p[id].mother);
		if(p[id].father!=-1)
			p[p[id].father].child.push_back(id);//把当前节点作为父节点的孩子节点
		if(p[id].mother!=-1)
			p[p[id].mother].child.push_back(id);//把当前节点作为母亲节点的孩子节点
		int k;scanf("%d ",&k);
		for(int j=0;j<k;j++){
			int chd;scanf("%d ",&chd);
			p[id].child.push_back(chd);
			if(chd!=-1){
				p[chd].father=p[chd].mother=id;//特殊化处理,意思是把当前节点作为孩子节点的父节点或者母亲节点,这里将两个化为相同,不影响后续的搜索
			}
		}
		scanf("%d %d",&p[id].estate,&p[id].area);
	} 
	for(int i=0;i<n;i++){
		if(!vis[m[i]]){
			vis[m[i]]=1;
			dfs(m[i]);
			cnt++;//统计联通块的数量
		}
	}
	for(int i=0;i<cnt;i++){
		int minId=MAX;
		double sumEstate=0,sumArea=0;
		int count=f[i].size();
		for(int j=0;j<count;j++){
			if(minId>f[i][j].id){
				minId=f[i][j].id;
			}
			sumEstate+=f[i][j].estate;
			sumArea+=f[i][j].area;
		}
		sumEstate=sumEstate*1.0/count;
		sumArea=sumArea*1.0/count;
		Total tmp;
		tmp.id=minId;tmp.count=count;
		tmp.averEstate=sumEstate;tmp.averArea=sumArea;
		ans.push_back(tmp);
	}
	sort(ans.begin(),ans.end(),cmp);
	printf("%d\n",ans.size());
	for(int i=0;i<ans.size();i++){
		printf("%04d %d %.3lf %.3lf\n",ans[i].id,ans[i].count,ans[i].averEstate,ans[i].averArea);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhuixun_/article/details/84137368