PAT A1114 Family Property (25point(s))

Scroll Down

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 Child1⋯Child1 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

题意

给出一组家族关系和土地数量,求出家族中的最大编号、平均土地数、人数和平均土地面积。

解答

本题不能使用简单的树形结构,应该使用并查集,并用较大编号者标识集合。

#include<iostream>
#include<unordered_map>
#include<algorithm>
#include<vector>

using namespace std;

struct Person {
	int stateM = -1;
	int area = -1;
};

unordered_map<int, Person*> people;
unordered_map <int, int>parent;
bool cmp1(vector<double> v1, vector<double> v2)
{
	// 代表人 人数 平均块数 平均面积 
	if (v2[3] != v1[3]) return v1[3] > v2[3];
	else return v1[0] < v2[0];
}
int Find(int x) {
	if (parent.find(x) == parent.end()) parent[x] = x;
	if (parent[x] == x) return x;
	else return Find(parent[x]);
}

void Union(int x, int y){
	int p1 = Find(x), p2 = Find(y);
	if (p1 != p2) {
		if(p2<p1) parent[p1] = p2;
		else parent[p2] = p1;
	}
}

int main() 
{
	int n, id, cid, cnum, fa, mo;
	Person *person = NULL;
	cin >> n;
	
	for (int i = 0; i < n; i++) {
		cin >> id;
		Find(id);
		people[id] = new Person();
		person = people[id];
		cin >> fa >> mo;
		if(fa!=-1) Union(id, fa);
		if(mo!=-1)Union(id, mo);
		cin >> cnum;
		for (int j = 0; j < cnum; j++) {
			cin >> cid;
			Union(id, cid);
		}
		cin >> person->stateM >> person->area;
	}
	unordered_map<int, vector<int>> cnt;
	for (auto it = parent.begin(); it != parent.end(); it++) {
		int p = Find(it->second);
		if (cnt.find(p) == cnt.end()) cnt[p] = vector<int>(3, 0);
		cnt[p][0]++;
		if (people.find(it->first) != people.end()) {
			cnt[p][1] += people[it->first]->stateM;
			cnt[p][2] += people[it->first]->area;
		}
	}
	vector<vector<double>> ans;
	for (auto it = cnt.begin(); it != cnt.end(); it++){
		vector<double> tmp;
		tmp.push_back(it->first);
		tmp.push_back(it->second[0]);
		tmp.push_back(1.0*it->second[1]/ it->second[0]);
		tmp.push_back(1.0*it->second[2] / it->second[0]);
		ans.push_back(tmp);
	}
	sort(ans.begin(), ans.end(), cmp1);
	printf("%lu\n", ans.size());
	for (int i = 0; i < ans.size();i++) {
		printf("%04d %d %.3f %.3f\n", (int)ans[i][0], (int)ans[i][1], ans[i][2], ans[i][3]);
	}
	return 0;
}