//
7-11 家庭房产 (25 分)
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。
输入格式:
输入第一行给出一个正整数N(≤1000),随后N行,每行按下列格式给出一个人的房产:
编号 父 母 k 孩子1 ... 孩子k 房产套数 总面积
其中编号是每个人独有的一个4位数的编号;父和母分别是该编号对应的这个人的父母的编号(如果已经过世,则显示-1);k(0≤k≤5)是该人的子女的个数;孩子i是其子女的编号。
输出格式:
首先在第一行输出家庭个数(所有有亲属关系的人都属于同一个家庭)。随后按下列格式输出每个家庭的信息:
家庭成员的最小编号 家庭人口数 人均房产套数 人均房产面积
其中人均值要求保留小数点后3位。家庭信息首先按人均面积降序输出,若有并列,则按成员编号的升序输出。
输入样例:
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
输出样例:
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
// 7-11
#include<bits/stdc++.h>
using namespace std;
// max == 1e3 * 8 < 1e4+5
// id < 9999 < 1e4+5
const int N=2e4+5;
typedef struct home{ int cnt; double s; }H; H in[N];
typedef struct family{ int id,cnt1,cnt2; double p,s; }F; F ans[N];
set<int> s1;
set<int> s2;
int father[N],sum[N];
int my_find( int x )
{
return father[x]==x ? x : father[x]=my_find( father[x] ) ;
}
void my_link( int a,int b )
{
int ra=my_find(a),rb=my_find(b);
if( ra<rb ) father[rb]=ra;
else if( rb<ra ) father[ra]=rb;
}
bool cmp( F a,F b )
{
return ( a.s==b.s ) ? ( a.id<b.id ) : ( a.s>b.s ) ;
}
void init()
{
memset( in,0,sizeof( in ) );
memset( ans,0,sizeof( ans ) );
memset( sum,0,sizeof( sum ) );
s1.clear();
s2.clear();
for( int i=0;i<=10005;i++ ) father[i]=i; // 01 注意范围 小心越界 02 father 编号初始化
}
int main()
{
int n,a,b,c,k,x,root;
while( ~scanf("%d",&n) )
{
init();
while( n-- )
{
scanf("%d%d%d%d",&a,&b,&c,&k); s1.insert(a); // insert not push
if( b!=-1 ) { s1.insert(b); my_link( a,b ); }
if( c!=-1 ) { s1.insert(c); my_link( a,c ); }
while( k-- ) { scanf("%d",&x); s1.insert(x); my_link( a,x ); }
scanf("%d%lf",&in[a].cnt,&in[a].s );
}
for( auto i:s1 )
{
root=my_find(i); s2.insert( root );
sum[ root ]+=in[i].s; // i not root
ans[ root ].id=root;
ans[ root ].cnt1++;
ans[ root ].cnt2+=in[i].cnt; //
}
for( auto i:s2 )
{
ans[i].p=(double) ans[i].cnt2 / ans[i].cnt1;
ans[i].s=(double) sum[i] / ans[i].cnt1;
}
sort( ans,ans+10005,cmp );
printf("%d\n",s2.size() );
for( int i=0;i<s2.size();i++ )
{
printf("%04d %d %.3lf %.3lf\n",ans[i].id ,ans[i].cnt1 ,ans[i].p ,ans[i].s );
}
}
return 0;
}