PAT 天梯赛 L2-007. 家庭房产 【并查集】

题目链接

https://www.patest.cn/contests/gplt/L2-007

思路
将一个家庭里的所有人都并进去
然后最后查找的时候 找到所有同一个家庭的人,计算出人数,人均房产套数,人均房产面积 而且 在ID 小于当前 ID 的时候 要更新

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;
const double eps = 1e-6;

const int INF    = 0x3f3f3f3f;
const int maxn   = 1e6 + 5;
const int MOD    = 1e9 + 7;

int pre[10000];

int find(int x)
{
    while (pre[x] != x)
        x = pre[x];                   
    return x; 
}

void join(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (x != fy)
        pre[fx] = fy;
}   

struct Node
{
    int id;
    int total;
    int tot;
    double ave, area;
}temp, q[maxn];

bool comp(Node x, Node y)
{
    if (fabs(x.area - y.area) < eps)
        return x.id < y.id;
    return x.area > y.area;
}

int main()
{
    for (int i = 0; i < 10000; i++)
        pre[i] = i;
    int n;
    cin >> n;
    map <int, Node> m;
    map <int, int> vis; 
    for (int i = 0; i < n; i++)
    {
        int id, a, b, k;
        scanf("%d%d%d%d", &id, &a, &b, &k);
        vis[id] = 1;
        if (a != -1)
        {
            join(id, a);
            vis[a] = 1;
        }   
        if (b != -1)
        {
            join(id, b);
            vis[b] = 1;
        }   
        for (int j = 0; j < k; j++)
        {
            scanf("%d", &a);
            if (a != -1)
            {
                join(id, a);
                vis[a] = 1;
            }
        }
        scanf("%d%d", &a, &b);
        m[id].id   = id;
        m[id].tot  = a;
        m[id].area = b;
    }
    map <int, int>::iterator it, iter;
    int count = 0;
    for (it = vis.begin(); it != vis.end(); it++)
    {
        temp.id   = 0;
        temp.tot  = 0;
        temp.tot  = 0;
        temp.ave  = 0.0;
        temp.area = 0.0;
        if (it -> second == 1)
        {
            it -> second = 0;
            int flag   = find(it -> first);
            temp.id    = it -> first;
            temp.tot  += m[it -> first].tot;
            temp.area += m[it -> first].area;
            temp.total = 1;
            for (iter  = it, iter++; iter != vis.end(); iter++)
            {
                if (iter -> second == 1 && flag == find(iter -> first))
                {
                    iter -> second = 0;
                    if (m[iter -> first].id == iter -> first)
                    {
                        if (iter -> first < temp.id)
                            temp.id = iter -> first;
                        temp.tot  += m[iter -> first].tot;
                        temp.area += m[iter -> first].area;
                    }
                    temp.total ++;
                }
            }
            temp.ave  = temp.tot * 1.0 / temp.total;
            temp.area /= temp.total;
            q[count++] = temp;
        }
    }
    sort (q, q + count, comp);
    printf("%d\n", count);
    for (int i = 0; i < count; i++)
    {
        printf("%04d %d %.3lf %.3lf\n", q[i].id, q[i].total, q[i].ave, q[i].area);
    }
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值