PTA-1114 Family Property
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⋯ChildkMestate 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 AVGsets AVGarea 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 AVGarea 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
思路
这道题相对来说是PTA三道并查集里面最繁琐的了…
和之前的题思路相同,因为set没有重复,所以我们利用set将所有输入的人的编号都保存下来,输入的同时进行合并操作并用pair将sets和area存储起来。
然后遍历整个set容器,将有相同根的人的sets和area都加起来,他们是一个家族的,分别利用数组存储家族的最小编号、SUMsets还有SUMarea,最后就是输出操作,别忘了1要用0001输出,所以高位要补零。
解决代码
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxx=1e5+5;
int a[maxx];
int h[maxx];
int value[maxx];//area
int sum[maxx];//sets
int mini[maxx];//家族中最小编号
struct node
{
int no,num;
double size,value;
node(int _no,int _num,double _size,double _value)
{
no=_no,num=_num,size=_size,value=_value;
}
};
bool cmp(node x,node y)
{
return x.value==y.value ? x.no<y.no : x.value>y.value;
}
void init(int n)
{
for(int i=1;i<=n;i++)
{
a[i]=i;
h[i]=0;
}
}
int find(int x)
{
return x==a[x]? x:a[x]=find(a[x]);
}
void unite(int x,int y)
{
if(x==-1||y==-1) return ;
x=find(x);
y=find(y);
if(x==y) return ;
if(h[x]<h[y]) a[x]=y;
else
{
a[y]=x;
if(h[x]==h[y]) h[x]++;
}
}
int main()
{
int n;
cin>>n;
init(10000);
set<int> no;
pair<int,int> e[maxx];
unordered_map<int,int> m;
while(n--)
{
int x,y,z;
int num;
cin>>x>>y>>z>>num;
unite(x,y);
unite(x,z);
no.insert(x);
int mi=x;
if(z!=-1) no.insert(z);
if(y!=-1) no.insert(y);
for(int i=0;i<num;i++)
{
int q;
cin>>q;
unite(x,q);
no.insert(q);
}
int p,q;
cin>>p>>q;
e[x].first=p;
e[x].second=q;
}
memset(mini,INF,sizeof(mini));
for(auto it=no.begin();it!=no.end();it++)
{
m[find(*it)]++;
sum[find(*it)]+=e[*it].first;
value[find(*it)]+=e[*it].second;
mini[find(*it)]=min(*it,mini[find(*it)]);
}
int size=m.size();
cout<<size<<endl;
vector<node> v;
for(auto it=m.begin();it!=m.end();it++)
{
v.push_back(node(mini[it->first],it->second,(double)sum[find(it->first)]/it->second,(double)value[find(it->first)]/it->second));
}
sort(v.begin(),v.end(),cmp);
for(int i=0;i<size;i++)
{
if(v[i].no<1000)
{
printf("0");
if(v[i].no<100) printf("0");
if(v[i].no<10) printf("0");
}
printf("%d %d %.3lf %.3lf\n",v[i].no,v[i].num,v[i].size,v[i].value);
}
return 0;
}