PTA-L2-007 家庭房产 (25分)

19 篇文章 0 订阅

PTA-L2-007 家庭房产 (25分)

传送门

这道题,简单并查集。就是输出处理麻烦了些许。

我们把个人的id作为数组的下标。然后进行存储,合并好之后,用set容器把每个家庭的代表的id存起来,然后相应的存入到结构体中,然后sort一下即可。

代码部分:

#include <bits/stdc++.h>
#define mst(a, n) memset(a, n, sizeof(a))
using namespace std;
const int N = 1e4+ 10;
const int M = 55;
const int INF = 1e6 + 10;
typedef long long ll;

struct node
{
	int num;
	int peo;
	double sumh;
	double sumare;
}a[N];
int n;
int id, fath, moth, k, chi;
int fa[N];
int vis[N];
int cnt[N];
int area[N];
int sum[N];

int find(int x)
{
	if (x != fa[x])
	{
		return fa[x] = find(fa[x]);
	}
	return fa[x];
}

void merge(int x, int y)
{
	int fax = find(x);
	int fay = find(y);
	if (fax > fay)
	{
		fa[fax] = fay;
	}
	else
	{
		fa[fay] = fax;
	}
}

bool cmp(node x, node y)
{
	if (x.sumare == y.sumare)
	{
		return x.num < y.num;
	}
	return x.sumare > y.sumare;
}

int main()
{
	for (int i = 0; i < N; i++)
	{
		fa[i] = i;
	}
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> id >> fath >> moth >> k;
		vis[id] = 1;
		if (fath != -1)
		{
			vis[fath] = 1;
			merge(id, fath);
		}
		if (moth != -1)
		{
			vis[moth] = 1;
			merge(id, moth);
		}
		for (int j = 0; j < k; j++)
		{
			int child;
			cin >> child;
			vis[child] = 1;
			merge(id, child);
		}
		cin >> cnt[id] >> area[id];
	}
	set<int> st;
	for (int i = 0; i < N; i++)
	{
		if (!vis[i])
		{
			continue;
		}
		int root = find(i);
		sum[root]++;
		if (root != i)
		{
			cnt[root] += cnt[i];
			area[root] += area[i];
		}
		st.insert(root);
	}
	int count = 0;
	set<int>::iterator it = st.begin();
	while (it != st.end())
	{
		int root = *it;
		a[count].num = root;
		a[count].peo = sum[root];
		a[count].sumare = 1.0 * area[root] / sum[root];
		a[count++].sumh = 1.0 * cnt[root] / sum[root];
		it++;
	}
	sort(a, a + count, cmp);
	cout << count << endl;
	for (int i = 0; i < count; i++)
	{
		printf ("%04d %d %.3f %.3f\n", a[i].num, a[i].peo, a[i].sumh, a[i].sumare);
	}
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

娃娃酱斯密酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值