1114 Family Property(25 分)

并查集
注意在计算totsets时遍历a数组就可以,计算人数是从0到maxn遍历,因为不是每一个人有财产和土地

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int father[maxn];
bool isroot[maxn];
struct node {
    int id;
    double mestate, area;
}a[maxn];
int findfather(int x)
{
    int a = x;
    while (x != father[x])x = father[x];
    while (a != father[a])
    {
        int z = a;
        a = father[a];
        father[z] = x;
    }
    return x;
}
void Union(int x, int y)
{
    int fx = findfather(x);
    int fy = findfather(y);
    if (fx != fy)
    {
        if (fx < fy)father[fy] = fx;
        else father[fx] = fy;
    }
}

struct fam {
    int m, id;
    double totsets, totarea;
    bool valid;
    bool operator<(const fam&x)const
    {
        return valid == true && x.valid == true ? totarea / m == x.totarea / x.m ? id<x.id : totarea / m>x.totarea / x.m : valid > x.valid;
    }
}ans[maxn];
int main()
{
    int n;
    for (int i = 0; i < maxn; i++)father[i] = i;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        int id, f, m;
        scanf("%d %d %d", &id, &f, &m);
        if (f != -1)Union(id, f);
        if (m != -1)Union(id, m);
        int k, child;
        scanf("%d", &k);
        while (k--)
        {
            scanf("%d", &child);
            Union(id, child);
        }
        a[i].id = id;
        scanf("%lf%lf", &a[i].mestate, &a[i].area);
        
    }
    for (int i = 0; i < n; i++)
    {
        int f = findfather(a[i].id);
        ans[f].id = f;
        ans[f].totsets += a[i].mestate;
        ans[f].totarea += a[i].area;
        ans[f].valid = true;
        isroot[f] = true;
    }
    for (int i = 0; i < maxn; i++)
    {
        int f = findfather(i);
        ans[f].m++;
    }
    int cnt = 0;
    for (int i = 0; i < maxn; i++)cnt += isroot[i];
    sort(ans, ans + maxn);
    printf("%d\n", cnt);
    for (int i = 0; i < cnt; i++)
    {
        printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].m, ans[i].totsets / ans[i].m, ans[i].totarea / ans[i].m);
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 13,009评论 0 13
  • 遇到了很多人,匆匆过客,遍地都是朋友的人,能交心的朋友,彼此尊重认可的,有些人很奇怪,好像从未来过我的世界,却又在...
    JungleTiger阅读 1,531评论 0 0
  • 各位家长: 我们的教学理念是在孩子刚接触英语的时候能通过母语习得法获得良好的英语语感和英语思维,以此弥补学校英语教...
    甜妮儿nancy阅读 3,468评论 0 0
  • 挑选拉杆箱的时候,除了要注意款式尺寸之外,拉杆箱材质的选择也是很重要的。 材质方面,现在市面上的旅行箱按材质主要分...
    番茄不是西红柿222阅读 8,385评论 0 0
  • 恍惚,恍惚,恍惚! 来不及在记忆中搜寻你,找不到我期望的你,可能在梦里才能和你相遇。 你不是我要找的他,你不懂我,...
    Char1enenee阅读 1,117评论 0 0

友情链接更多精彩内容