7-11 家庭房产(25 分)

7-11 家庭房产(25 分)

只能过前两个点

 

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<iomanip>
#include<stack>
using namespace std;
#define STDIN freopen("in.in", "r", stdin);freopen("out.out", "w", stdout);

const int N = 10000+10;
int fa[N];
int n;
int area[N], house[N], cnt[N];
void init()
{
    for (int i = 0; i <= 10000; i++) 
    {
        fa[i] = -1;
        cnt[i] = 1;
    }
}
int find(int x)
{
    if (fa[x] == -1) return fa[x] = x;
    if (x == fa[x]) return x;
    return fa[x] = find(fa[x]);
}

void unite(int a, int b)
{
    int f1 = find(a);
    int f2 = find(b);
    if (f1 <= f2) {
        fa[f2] = f1;
        house[f1] += house[f2];
        area[f1] += area[f2];
        cnt[f1] += cnt[f2];
    }
    else {
        fa[f1] = f2;
        house[f2] += house[f1];
        area[f2] += area[f1];
        cnt[f2] += cnt[f1];
    }
}
struct node{
    int v, cnt;
    double h, a;
};
bool cmp(node a, node b)
{
    if (a.a == b.a) return a.v < b.v;
    return a.a > b.a;
}
int main()
{
//     STDIN
    cin >> n;
    init();
    for (int i = 1; i <= n; i++)
    {
        int k, f, m, kk;
        cin >> k >> f >> m >> kk;
        for (int j = 1; j <= kk; j++)
        {
            int x;
            cin >> x;
            if (find(x) != find(k))
                unite(x, k);
        }
        int h, ar;
        cin >> h >> ar;
        area[find(k)] += ar;
        house[find(k)] += h;
        if (f!=-1 && find(k) != find(f))
            unite(k, f);
        if (m!=-1 && find(k) != find(m))
            unite(find(k), m);
    }
    int res = 0;
    vector<node> vec;
    for (int i = 1; i <= 10000; i++) {
        if (fa[i] == i) {
            res ++;
            vec.push_back({i, cnt[i], house[i]*1.0/cnt[i],area[i]*1.0/cnt[i]});
        }
    }
    cout << res << endl;
    sort(vec.begin(), vec.end(), cmp);
    for (auto &i : vec)
    {
        printf("%04d %d %.3f %.3f\n", i.v, i.cnt, i.h, i.a);
    }
}    

 

posted @ 2020-11-25 21:56  hulian425  阅读(133)  评论(0编辑  收藏  举报