Test instructions: Give everyone's family members information and the number of their own property and the total area of the property, so that you count the number of households per household, the number of per capita property and the per capita housing area. The first row outputs the number of families, and then each row outputs the minimum number of family members, the number of households, the number of homes per capita, and the per capita housing area.
And check set, when the combination of small number as a Father node, the last Father node is the same as belonging to a family, the other is the details of the treatment has nothing to say.
#include <iostream>#include<cstdio>#include<algorithm>#include<string.h>using namespacestd;/*and check Set*/Const intmaxn=10000+5;intVIS[MAXN];structuf{intFATHER[MAXN]; voidinit () { for(intI=0; i<maxn;i++) {Father[i]=i; } } intFind_root (intx) { if(father[x]!=x) {Father[x]=Find_root (father[x]); } returnFather[x]; } voidUnion (intAintb) { intHasFind_root (a); intfb=Find_root (b); if(fa!=FB) {Father[max (FA,FB)]=min (FA,FB);//the smallest number as an ancestor}}}uf;structperson{intID; intDad,mom; intK; intchild[5]; intm_estate; intArea ;} PERSON[MAXN];structfamily{intMiniD;//minimum number of family members intm=0;//number of family population intM_estate=0;//total number of properties intTotarea=0;//Total Property Area floatAvg_sets;//number of property per capita floatAvg_area;//per capita housing area BOOL operator< (ConstFamily tmp)Const{ if(avg_area==tmp. Avg_area) {returnminid<Tmp.minid; } Else{ returnAvg_area>tmp. Avg_area; }}}FAMILY[MAXN];intMain () {intN,id; scanf ("%d",&N); Uf.init (); memset (Vis,0,sizeof(VIS)); for(intI=0; i<maxn;i++) Person[i].id=-1; for(intI=0; i<n;i++) {scanf ("%d",&ID); Person[id].id=ID; scanf (" %d%d%d",&person[id].dad,&person[id].mom,&PERSON[ID].K); Vis[person[id].id]=1;//mark the number that has occurred if(person[id].dad!=-1) {uf. Union (Id,person[id].dad); Vis[person[id].dad]=1; } if(person[id].mom!=-1) {uf. Union (ID,PERSON[ID].MOM); VIS[PERSON[ID].MOM]=1; } for(intj=0; j<person[id].k;j++) {scanf ("%d",&Person[id].child[j]); uf. Union (Id,person[id].child[j]); VIS[PERSON[ID].CHILD[J]]=1; } scanf ("%d%d",&person[id].m_estate,&Person[id].area); } intIDARRAY[MAXN]; intCnt=0; for(intI=0; i<maxn;i++){ if(vis[i]==1) {idarray[cnt++]=i;//the numbers that appear are saved.}} memset (Vis,0,sizeof(VIS)); intU,fa; for(intI=0; i<cnt;i++) {u=Idarray[i]; FA=Uf.find_root (Idarray[i]); VIS[FA]=1;//Tag Father nodeFamily[fa].minid=FA; FAMILY[FA].M++; if(person[u].id!=-1) {Family[fa].totarea+=Person[u].area; Family[fa].m_estate+=person[u].m_estate; } } intfamilynum=0; for(intI=0; i<maxn;i++){ if(Vis[i]) {Familynum++; Family[i]. Avg_sets=family[i].m_estate*1.0f/family[i].m; Family[i]. Avg_area=family[i].totarea*1.0f/family[i].m; }} sort (family,family+MAXN); printf ("%d\n", Familynum); for(intI=0; i<familynum;i++) {printf ("%04d%d%.3f%.3f\n", Family[i].minid,family[i].m,family[i]. Avg_sets,family[i]. Avg_area); } return 0;}View Code
Pat a problem-1114. Family Property (25)-(and set template title)