1. 程式人生 > 其它 >L2-007 家庭房產

L2-007 家庭房產

//並查集

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 10010;
constexpr double eps = 1e-8;
int f[N];
//int Size[N]; //集合的人數
vector<int> g[N];

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

int main() {
    int n;
    cin >> n;
    for (int i = 0; i <= 9999; i++) {
        f[i] = i;
    }
    map<int, pair<int, int>> people;
    set<int> all;
    for (int i = 0; i < n; i++) {
        int cur, fa, mo;
        cin >> cur >> fa >> mo;

        vector<int> v;
        v.push_back(cur);
        all.insert(cur);
        if (fa != -1) v.push_back(fa), all.insert(fa);
        if (mo != -1) v.push_back(mo), all.insert(mo);
        sort(v.begin(), v.end(), [&](int a, int b) {
            return find(a) < find(b);
            });
        int minFa = find(v[0]);
        for (int j = 1; j < v.size(); j++) {
            int father = find(v[j]);
            f[father] = minFa;
        }

        int k;
        cin >> k;
        vector<int> child;
        for (int _ = 0; _ < k; _++) {
            int son;
            cin >> son;
            all.insert(son);
            child.push_back(son);
        }
        if (k != 0) {
            sort(child.begin(), child.end(), [&](int a, int b) {
                return find(a) < find(b);
                });
            int curMinfa = find(child[0]);
            if (curMinfa < minFa) {
                f[minFa] = curMinfa;
                minFa = curMinfa;
            }
            //minFa = min(minFa, curMinfa);
            for (int j = 0; j < child.size(); j++) {
                int father = find(child[j]);
                f[father] = minFa;
            }
        }

        int num;
        cin >> num;
        int area;
        cin >> area;
        people[cur] = { num, area };
    }

    for (int i = 0; i <= 9999; i++) {
        if (!all.count(i)) continue;
        if (find(i) == i) {
            g[i].push_back(i);
        }
        else {
            int father = find(i);
            g[father].push_back(i);
        }
    }

    struct ans {
        int minNumber, cnt;
        double houseNumber, areaNumber;
    };

    vector<ans> res;
    for (int i = 0; i <= 9999; i++) {
        if (!all.count(i)) continue;
        if (g[i].size() == 0) continue;
        ans cur;
        cur.cnt = (int)g[i].size();
        int houseNumber = 0, areaNumber = 0;
        for (int j = 0; j < g[i].size(); j++) {
            if (g[i][j] == i) cur.minNumber = i;
            houseNumber += people[g[i][j]].first;
            areaNumber += people[g[i][j]].second;
        }
        cur.houseNumber = (double) houseNumber / cur.cnt;
        cur.areaNumber = (double) areaNumber / cur.cnt;
        res.push_back(cur);
    }

    sort(res.begin(), res.end(), [&](ans x, ans y) { 
        if (abs(x.areaNumber - y.areaNumber) < eps) return x.minNumber < y.minNumber;
        return x.areaNumber > y.areaNumber;
    });

    cout << res.size() << "\n";
    for (int i = 0; i < res.size(); i++) {
        printf("%04d %d %.3f %.3f", res[i].minNumber, res[i].cnt, (double)res[i].houseNumber, (double)res[i].areaNumber);
        if (i != res.size() - 1) cout << "\n";
        //cout << res[i].minNumber << " " << res[i].cnt << " " << res[i].houseNumber << " " << res[i].areaNumber << "\n"[i == res.size() - 1];
    }

    return 0;
}