加载中...

家产

https://www.acwing.com/problem/content/1606/

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

using namespace std;

const int N = 10010;

int n;
int p[N], c[N], hc[N], ha[N];
bool st[N];

struct Edge
{
    int a, b;
}e[N];

struct Family
{
    int id, c, hc, ha;

    bool operator< (const Family &t) const
    {
        // ha / c ? t.ha / t.c
        if (ha * t.c != t.ha * c) return ha * t.c > t.ha * c;
        return id < t.id;
    }
};

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

int main()
{
    cin >> n;

    int m = 0;
    for (int i = 0; i < n; i ++ )
    {
        int id, father, mother, k;
        cin >> id >> father >> mother >> k;

        st[id] = true;
        if (father != -1) e[m ++ ] = {id, father};
        if (mother != -1) e[m ++ ] = {id, mother};
        for (int j = 0; j < k; j ++ )
        {
            int son;
            cin >> son;
            e[m ++ ] = {id, son};
        }

        cin >> hc[id] >> ha[id];
    }

    for (int i = 0; i < N; i ++ ) p[i] = i, c[i] = 1;
    for (int i = 0; i < m; i ++ )
    {
        int a = e[i].a, b = e[i].b;

        st[a] = st[b] = true;
        int pa = find(a), pb = find(b);
        if (pa != pb)
        {
            if (pb > pa) swap(pa, pb);
            c[pb] += c[pa];
            hc[pb] += hc[pa];
            ha[pb] += ha[pa];
            p[pa] = pb;
        }
    }

    vector<Family> family;
    for (int i = 0; i < N; i ++ )
        if (st[i] && p[i] == i)
            family.push_back({i, c[i], hc[i], ha[i]});

    sort(family.begin(), family.end());

    cout << family.size() << endl;
    for (auto f : family)
        printf("%04d %d %.3lf %.3lf\n", f.id, f.c, (double)f.hc / f.c, (double)f.ha / f.c);

    return 0;
}

posted @ 2022-08-22 23:05  英雄不问出处c  阅读(26)  评论(0编辑  收藏  举报