L2-007 家庭房產 分數 25

Frodnx發表於2024-08-30
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
// 分別為祖宗節點,人數,房子數,總面積
int p[N], cnt[N], fangzi[N], totalarea[N], minnum[N];
// 編號是否存在,為true存在
bool flag[N];
class ans
{
public:
    int zuixiao;
    int renkou;
    double renjunfang;
    double renjunmian;
    ans(int a1, int b1, double c1, double d1) : zuixiao(a1), renkou(b1), renjunfang(c1), renjunmian(d1){};
    ans(){};
    void print()
    {
    	cout << setw(4) << setfill('0') << zuixiao;
        cout << " " << renkou << " ";
    	cout << fixed << setprecision(3) << renjunfang << " " << renjunmian << endl;
    }
};
void init()
{
    for(int i = 0; i < N; ++ i)
    {
        p[i] = i;
        cnt[i] = 1;
        fangzi[i] = 0;
        totalarea[i] = 0;
        minnum[i] = i;
    }
}
bool sort_func(ans a, ans b)
{
    if(a.renjunmian != b.renjunmian) return a.renjunmian > b.renjunmian;
    else return a.zuixiao < b.zuixiao;
}
int find(int x)
{
    if(x != p[x]) p[x] = find(p[x]);
    return p[x];
}
void merge(int son, int par)
{
    int fs = find(son), fp = find(par);
    if(fs != fp)
    {
        p[fs] = fp;
        cnt[fp] += cnt[fs];
        fangzi[fp] += fangzi[fs];
        totalarea[fp] += totalarea[fs];
        minnum[fp] = min(minnum[fs], minnum[fp]);
    }
}
void fenzhi()
{
    int ncnt = 0;
    for(int i = 0; i < N; ++ i)
        if(flag[i] && p[i] == i) ++ ncnt;
    cout << ncnt << endl;
    vector<ans> totalans;
    for(int i = 0; i < N; ++ i)
    {
        if(flag[i] && p[i] == i)
        {
            ans tmp(minnum[i], cnt[i], (double)fangzi[i] / (double)cnt[i], (double)totalarea[i] / (double)cnt[i]);
            totalans.push_back(tmp);
        }
    }
    sort(totalans.begin(),totalans.end(),sort_func);
    for(auto ti : totalans)
    	ti.print();
}
int main()
{
    int n;
    cin >> n;
    init();
    vector<vector<int>> input;
    // 先將個人的房子和總面積初始化
    for(int i = 1; i <= n; ++ i)
    {
    	vector<int> tmp;
        int arr[4];
        for(int j = 0; j < 4; ++ j)
        {
            cin >> arr[j];
            tmp.push_back(arr[j]);
        }
        flag[arr[0]] = true;
        if(arr[1] != -1) flag[arr[1]] = true;
        if(arr[2] != -1) flag[arr[2]] = true;
        for(int j = 1; j <= arr[3]; ++ j)
        {
            int kid;
            cin >> kid;
            flag[kid] = true;
            tmp.push_back(kid);
        }
        int fang, area;
        cin >> fang >> area;
        fangzi[arr[0]] = fang;
        totalarea[arr[0]] = area;
        input.push_back(tmp);
    }
    
    // 再合併
    for(int i = 0; i < input.size(); ++ i)
    {
        int self = input[i][0], fu = input[i][1], mu = input[i][2];
        if(fu != -1) merge(self,fu);
        if(mu != -1) merge(self,mu);
        for(int j = 1; j <= input[i][3]; ++ j)
            merge(input[i][3 + j], self);
    }
    fenzhi();
    return 0;
}

相關文章