1114 Family Property(25 分)

2018-09-02  本文已影响0人  zjh3029
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<numeric>
#include<string>
#include<iomanip>
using namespace std;

vector<set<string>>v;
set<string>s;

int findfather(string str,int num)
{
    for (int i = num; i < v.size(); i++)
    {
        if (find(v[i].begin(), v[i].end(), str) != v[i].end())//此时找到在一个集合里面找到了该元素
            return i;
    }
    return -1;
}

int main()
{
    int M,N,L,area;
    cin >> M;
    vector<int> allarea(M);
    vector<int> allset(M);

    string a, b, c,d;
    for (int i = 0; i < M; i++)
    {
        int nID_before = -1;
        cin >> a >> b >> c;//个人编号以及父母的编号,判断父母是否还在,完成集合的插入
        s.insert(a);
        if (b != "-1") s.insert(b);
        if (c != "-1") s.insert(c);

        cin >> N;//检查子女的个数
        for (int i = 0; i < N; i++)//儿女的编号,并完成集合的插入
        {
            cin >> d;
            s.insert(d);
        }

        cin >> L>>area;//房子的个数还有总的面积
        allset[i] = L;
        allarea[i] = area;

        /*完成输入操作,开始进行处理*/

        bool flag = false;
        int nID = -1;
        int nall = 0;
        for (auto d : s)
        {
            nID=findfather(d,nID+1);
            if (nID>-1)//找到,合并两个集合
            {
                flag = true;
                v[nID].insert(s.begin(), s.end());

                if (nall==0)
                {
                    allarea[nID] += area;
                    allset[nID] += L;
                    nall = 1;
                    cout << allarea[nID] << endl;
                    cout << allset[nID] << endl;

                }

                //cout << nID_before << endl;
                if (nID_before == -1)
                {
                    nID_before = nID;//保留一个ID
                }
                if (nID_before != nID)//如果现有的项又找到了新的匹配集合
                {
                    v[nID_before].insert(v[nID].begin(), v[nID].end());
                    allarea[nID_before] += allarea[nID];
                    allset[nID_before] += allset[nID];
                    //cout << allarea[nID] <<"   "<< allarea[nID_before]<< endl;
                    //cout << allset[nID] <<"   " << allset[nID_before]<<endl;
                    v[nID].clear();
                }
            }
            
        }
        if (flag==false)//证明没有找到,则新建一个集合
        {
            v.push_back(s);
        }
        s.clear();//清除所有存储的内容
    }
    //cout << "-------------"<<endl;
    int cnt = 0;
    vector<double> vd;
    for (int i = 0; i < v.size(); i++)
    {
        if (!v[i].empty())
        {
            cnt++;
            /*cout << allarea[i] << endl;
            cout << allset[i] << endl;

            cout<<v[i].size() << endl;
            cout <<setiosflags(ios::fixed)<<setprecision(3) <<double(allset[i]) / v[i].size() << endl;
            cout << setiosflags(ios::fixed) << setprecision(3) << double(allarea[i]) / v[i].size() << endl;*/
        }
        /*for (auto d : v[i])
        {
            cout << d << " ";
        }*/
        vd.push_back(double(allarea[i]) / v[i].size());
    }

    cout<<cnt<<endl;

    for (auto m : vd)
    {
        cout << m;
    }
    system("pause");
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读