solution Of Pat 1114. Family Property (25)

来源:互联网 发布:foxmail国外端口设置 编辑:程序博客网 时间:2024/04/29 01:41

1114. Family Property (25)

This time, you are supposed to help us collect the data for family-owned property. Given each person’s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:

ID Father Mother k Child1 … Childk M_estate Area

where ID is a unique 4-digit identification number for each person; Father and Mother are the ID’s of this person’s parents (if a parent has passed away, -1 will be given instead); k (0<=k<=5) is the number of children of this person; Childi’s are the ID’s of his/her children; M_estate is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.

Output Specification:

For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:

ID M AVG_sets AVG_area

where ID is the smallest ID in the family; M is the total number of family members; AVG_sets is the average number of sets of their real estate; and AVG_area is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID’s if there is a tie.

Sample Input:
10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100
Sample Output:
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000


结题思路:

题意要求我们找到每个family的root,以及每个家庭的平均拥有房产数和平均的住宅面积。
要求1:用-1表示单亲不存在;
要求2:先用平均住宅面积进行,再用家庭成员跟节点的编号大小排序;
要求3:考虑到要进行平均住宅面积(浮点型)的大小比较,为避免讨论,直接转换成乘积的比较。

程序步骤:
第一步、先利用并查集进行建树,每次合并将较小的那个节点编号作为根节点;
第二步、进行遍历,将每个节点的数据都合并到他的家庭的Root成员的数据上;
第三步、比较Root成员间的数据汇总,进行排序,输出即可。

具体程序(AC)如下:

#include <iostream>#include <vector>#include <algorithm>using namespace std;vector<int> exist;//节点存在的标记,用于判断节点是否合并到某个family当中,对输入节点编号进行重新的映射vector<int> relation;//存储父节点的indexvector<int> rootSet;//存储最后的每个family的Rootint start=-1;struct node{    int no;    int totP;    long long estate;    long long area;};vector<node> input;int pushInPut(int no)//完成对于输入编号的重新映射,并保存重映射结果到exist{    if(exist[no]==-1)    {        exist[no]=++start;        node tmp;        tmp.no=no;        tmp.estate=0;        tmp.totP=1;        tmp.area=0;        input.push_back(tmp);        return start;    }    return exist[no];}int cmp(const int& a,const int& b){//先按平均的居住面积递减排序,再按Root的编号递增排序    if(input[a].area*input[b].totP!=input[b].area*input[a].totP)        return input[a].area*input[b].totP>input[b].area*input[a].totP;    else        return input[a].no<input[b].no;}int getparent(int cur){    if(relation[cur]!=cur)        return relation[cur]=getparent(relation[cur]);    else         return cur;}void merge(int cur,int parent){    int b=getparent(parent);    int a=getparent(cur);    if(a>b)        relation[a]=b;    else        relation[b]=a;  }void getAVG_areaPerFamily(){//遍历,将每个人的房产信息汇总到family的Root节点上    int n=input.size();    int parent;    int cur;    rootSet.clear();    for(int i=0;i<n;++i)    {        cur=input[i].no;        parent=getparent(cur);        cur=i;        parent=exist[parent];        if(cur!=parent)        {        //  cout<<parent<<" "<<cur<<endl;            input[parent].totP+=1;            input[parent].estate+=input[cur].estate;            input[parent].area+=input[cur].area;        }        else            rootSet.push_back(i);    }}int main(){    int n;    cin>>n;    exist.resize(10000,-1);    relation.resize(10000);    for(int i=0;i<relation.size();++i)        relation[i]=i;    int cur,parent,child,tofC;    int location;    int noState,areaState;    for(int i=0;i<n;++i)    {        cin>>cur;        location=pushInPut(cur);//当前节点存入的位置        for(int j=0;j<2;++j)        {            cin>>parent;            if(parent!=-1)            {                pushInPut(parent);                merge(cur,parent);            }        }//对双亲进行合并        cin>>tofC;        for(int j=0;j<tofC;++j)        {            cin>>child;            if(child!=-1)            {                pushInPut(child);                merge(cur,child);            }        }//对孩子进行合并        cin>>noState>>areaState;        input[location].estate=noState;        input[location].area=areaState;    }    getAVG_areaPerFamily();    if(rootSet.size()>0)        sort(rootSet.begin(),rootSet.end(),cmp);    cout<<rootSet.size()<<endl;    int index;    for(int i=0;i<rootSet.size();++i)    {        index=rootSet[i];        printf("%04d %d %.3lf %.3lf\n",input[index].no,input[index].totP,(input[index].estate+0.0)/input[index].totP,(input[index].area+0.0)/input[index].totP);    }    return 0;}
0 0
原创粉丝点击