PAT甲级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:
ID Father Mother k Child1​​ ⋯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; 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

题目大意

其实就是给出家族关系(不止一个家族),然后计算该家族的平均资产等等信息

思路

第一反应使用并查集来做,因为并查集有合并操作可以将两个集合合并。但是粗略想想每个人有父亲和母亲两个“father”,这该怎么表示呢?然后就犯了难,就选择用无向图来解决。用结点表示人,不管关系有多复杂,只要两个结点是连通的,他们就是一家人,否则是两家人。再用dfs或bfs根据连接关系遍历图就可以在杂乱的家族关系中把自家人筛出来,存储到数组中进一步进行数据处理即可。刚开始使用10000*10000的邻接矩阵存储图,结果最后每个测试点都内存超限,最后只好改成邻接表的方法,一次就成功AC

//邻接表版(AC)
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define MAXV 10000
struct info
{
	int sets;
	int area;
};
struct fin
{
	int min;
	int mem;
	double sets;
	double area;
};

vector<int> Adj[MAXV];
vector<fin> ans;
map<int, info> mp;
map<int, bool> vis;
void bfs(int);
bool cmp(fin a, fin b)
{
	if (a.area != b.area)
		return a.area > b.area;
	else
		return a.min < b.min;
}
int main()
{
	int N;
	cin >> N;

	vector<int> rec;
	for (int i = 0; i < N; i++)
	{
		int id, dad, mom, k;
		cin >> id >> dad >> mom >> k;
		rec.push_back(id);
		//无向图要双向存储相连结点
		if (dad != -1)
		{
			Adj[id].push_back(dad);
			Adj[dad].push_back(id);
		}
		if (mom != -1)
		{
			Adj[id].push_back(mom);
			Adj[mom].push_back(id);
		}
		for (int j = 0; j < k; j++)
		{
			int child;
			cin >> child;
			Adj[id].push_back(child);
			Adj[child].push_back(id);
		}
		//存储这个人的家产信息
		info s;
		cin >> s.sets >> s.area;
		mp[id] = s;
	}
	//BFS_Trav
	for (int i = 0; i < rec.size(); i++)
		if (!vis[rec[i]])
			bfs(rec[i]);
	sort(ans.begin(), ans.end(), cmp);
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
		printf("%04d %d %.3lf %.3lf\n", ans[i].min, ans[i].mem, ans[i].sets, ans[i].area);
	system("pause");
	return 0;
}
void bfs(int u)
{
	queue<int> Q;
	vector<int> family;

	Q.push(u);
	vis[u] = true;
	while (!Q.empty())
	{
		int top = Q.front();
		family.push_back(top);
		Q.pop();
		for (int i = 0; i < Adj[top].size(); i++)
		{
			int v = Adj[top][i];
			if (vis[v] == false)
			{
				Q.push(v);
				vis[v] = true;
			}
		}
	}
	//bfs之后family存储着自家人
	fin s;
	s.mem = family.size();
	int min = MAXV, cnt = 0;
	double total_sets = 0.0, total_area = 0.0;
	for (int i = 0; i < family.size(); i++)
	{
		//如果这个人有资产信息
		if (mp.find(family[i]) != mp.end())
		{
			total_sets += mp[family[i]].sets;
			total_area += mp[family[i]].area;
		}
		if (min > family[i])
			min = family[i];
	}
	s.sets = total_sets / s.mem;
	s.area = total_area / s.mem;
	s.min = min;
	ans.push_back(s);
}

这里再贴出超时的邻接矩阵版

//邻接矩阵版(超时)
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
#define INF 2147483647
#define MAXV 9999
struct info
{
	int sets;
	int area;
};
struct fin
{
	int min;
	int mem;
	double sets;
	double area;
};
int matrix[MAXV][MAXV];
map<int, info> mp;
map<int, bool> visit;
vector<int> vec;
vector<fin> ans;
bool cmp(fin a, fin b)
{
	if (a.area != b.area)
		return a.area > b.area;
	else
		return a.min < b.min;
}
void dfs(int);
int main()
{
	fill(matrix[0], matrix[0] + 10000 * 10000, INF);
	int N;
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		int id, dad, mom, k;
		cin >> id >> dad >> mom >> k;
		vec.push_back(id);
		if (dad != -1)
			matrix[id][dad] = matrix[dad][id] = 1;
		if (mom != -1)
			matrix[id][mom] = matrix[mom][id] = 1;
		for (int j = 0; j < k; j++)
		{
			int child;
			cin >> child;
			matrix[id][child] = matrix[child][id] = 1;
		}
		info s;
		cin >> s.sets >> s.area;
		mp[id] = s;
	}
	for (int i = 0; i < vec.size(); i++)
		if (visit[vec[i]] == false)
			dfs(vec[i]);
	sort(ans.begin(), ans.end(), cmp);
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
		printf("%04d %d %.3lf %.3lf\n", ans[i].min, ans[i].mem, ans[i].sets, ans[i].area);

	system("pause");
	return 0;
}
void dfs(int i)
{
	stack<int> stk;
	stack<int> save;
	
	stk.push(i);
	visit[i] = true;
	while (!stk.empty())
	{
		int u = stk.top();
		save.push(u);
		stk.pop();
		for (int v = 0; v < MAXV; v++)
		{
			if (matrix[u][v] != INF && visit[v] == false)
			{
				stk.push(v);
				visit[v] = true;
			}
		}
	}
	fin s;
	s.mem = save.size();
	int min = MAXV, cnt = 0;
	double total_sets = 0.0, total_area = 0.0;
	while (!save.empty())
	{
		int top = save.top();
		if (mp.find(top) != mp.end())
		{
			total_sets += mp[top].sets;
			total_area += mp[top].area;
		}
		if (min > top)
			min = top;
		cnt++;
		save.pop();
	}
	s.min = min;
	s.sets = total_sets / s.mem;
	s.area = total_area / s.mem;
	ans.push_back(s);
}

并查集法

实际并查集法也是可以做的,我之前的担心完全是对并查集不熟悉导致。每次输入后只要将自己、父亲、母亲和孩子全部合并即可。注意这里的合并和平时的并查集,因为题目要求输出id最小的家庭成员,所以我们每次合并时都将最小的那个id当作父亲。

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define MAXV 10000
struct record
{
	double sets;
	double area;
};
struct family
{
	int minID;
	int count = 0;
	double sets = 0.0;
	double area = 0.0;
};
int father[MAXV];
void init();
int find_father(int);
void Union(int, int);
bool cmp(family a, family b)
{
	if (a.area != b.area)
		return a.area > b.area;
	else
		return a.minID < b.minID;
}
int main()
{
	init();
	int N;
	cin >> N;

	map<int, record> mp;
	for (int i = 0; i < N; i++)
	{
		int id, dad, mom, k;
		cin >> id >> dad >> mom >> k;
		if (dad != -1)
			Union(id, dad);
		if (mom != -1)
			Union(id, mom);
		for (int j = 0; j < k; j++)
		{
			int child;
			cin >> child;
			Union(id, child);
		}
		record r;
		cin >> r.sets >> r.area;
		mp[id] = r;
	}
	vector<family> roots(MAXV);
	for (int i = 0; i < MAXV; i++)
	{
		int r = find_father(i);
		roots[r].minID = r;
		roots[r].count++;
		if (mp.find(i) != mp.end())
		{
			roots[r].sets += mp[i].sets;
			roots[r].area += mp[i].area;
		}
	}
	vector<family> ans;
	for (int i = 0; i < roots.size(); i++)
	{
		if (roots[i].sets != 0.0)
		{
			roots[i].sets /= roots[i].count;
			roots[i].area /= roots[i].count;
			ans.push_back(roots[i]);
		}
	}
	sort(ans.begin(), ans.end(), cmp);
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
		printf("%04d %d %.3lf %.3lf\n", ans[i].minID, ans[i].count, ans[i].sets, ans[i].area);
	system("pause");
	return 0;
}
void init()
{
	for (int i = 0; i < MAXV; i++)
		father[i] = i;
}
int find_father(int x)
{
	int rec = x;
	while (x != father[x])
		x = father[x];
	while (rec != father[rec])
	{
		int z = rec;
		rec = father[rec];
		father[z] = x;
	}
	return x;
}
void Union(int x, int y)
{
	int xFather = find_father(x);
	int yFather = find_father(y);
	if (xFather > yFather)
		father[xFather] = yFather;
	else if (xFather < yFather)
		father[yFather] = xFather;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值