zoukankan      html  css  js  c++  java
  • 1114 Family Property (25 分)

    考察并查集的熟练使用。

    const int N=10010;
    struct Node
    {
        int id;
        int father,mother;
        int k;
        int child[6];
    }a[1010];
    struct Answer
    {
        int id;
        int cnt;
        double sets;
        double area;
        bool operator<(const Answer &W) const
        {
            if(area != W.area) return area > W.area;
            return id < W.id;
        }
    };
    int p[N],cnt[N];
    double sets[N],area[N];
    bool vis[N];
    int n;
    
    int find(int x)
    {
        if(x != p[x]) p[x]=find(p[x]);
        return p[x];
    }
    
    void merge(int x,int y)
    {
        int px=find(x),py=find(y);
        if(px < py) swap(px,py);
        if(px != py)
        {
            p[px]=py;
            cnt[py]+=cnt[px];
            sets[py]+=sets[px];
            area[py]+=area[px];
        }
    }
    
    int main()
    {
        for(int i=0;i<N;i++) p[i]=i,cnt[i]=1;
    
        cin>>n;
    
        for(int i=0;i<n;i++)
        {
            cin>>a[i].id>>a[i].father>>a[i].mother;
    
            cin>>a[i].k;
            for(int j=0;j<a[i].k;j++) cin>>a[i].child[j];
            cin>>sets[a[i].id]>>area[a[i].id];
        }
    
        for(int i=0;i<n;i++)
        {
            vis[a[i].id]=true;
            if(~a[i].father)
            {
                vis[a[i].father]=true;
                merge(a[i].id,a[i].father);
            }
            if(~a[i].mother)
            {
                vis[a[i].mother]=true;
                merge(a[i].id,a[i].mother);
            }
    
            for(int j=0;j<a[i].k;j++)
            {
                vis[a[i].child[j]]=true;
                merge(a[i].id,a[i].child[j]);
            }
        }
    
        int tot=0;
        vector<Answer> res;
        for(int i=0;i<N;i++)
            if(vis[i] && p[i] == i)
            {
                tot++;
                res.pb({i,cnt[i],sets[i]/cnt[i],area[i]/cnt[i]});
            }
    
        sort(res.begin(),res.end());
    
        cout<<tot<<endl;
        for(int i=0;i<res.size();i++)
        {
            int id=res[i].id;
            printf("%04d %d %.3f %.3f
    ",id,cnt[id],sets[id]/cnt[id],area[id]/cnt[id]);
        }
    
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    监控网页是否有变化
    设置开机自动启动进程
    nagios-调用脚本
    连接数据库出现10061错误
    小程序修改默认的radio样式
    小程序端,做类似于支付宝充值话费或流量的样式
    jq 在字符串中,去掉指定的元素
    vue 使用 proxyTable 解决跨域问题
    vue-cli 动态绑定图片失败
    vue-cli 使用 font-awesome 字体插件
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14502474.html
Copyright © 2011-2022 走看看