Given everyone's family members and their own real estate, please count the population, per capita real estate area and number of real estate sets of each family.
Input format:
Enter the first line to give a positive integer N (≤ 1000), and then N lines to give a person's real estate in the following format:
No. parent k child 1... Child k total area of real estate units
The number is a 4-digit number unique to each person; The parent and parent are the number of the person's parents corresponding to the number (if they have passed away, - 1 is displayed); k (0 ≤ k ≤ 5) is the number of children of the person; Child i is the number of their child.
Output format:
First, output the number of families in the first line (all relatives belong to the same family). Then output the information of each family in the following format:
Minimum number of family members family population per capita real estate units per capita real estate area
The per capita value is required to keep 3 decimal places. Family information is first output in descending order of per capita area. If there is juxtaposition, it is output in ascending order of member number.
Input example:
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
No blank lines at the end
Output example:
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
No blank lines at the end
In this problem, use and query the set. Each node should have family related information. Initially, use the data and query the set to build a tree. Store the number and area of real estate in a member from 0 to 9999. Cycle. If you find the root (record the root with an array), and then cycle 0 to 9999. If you encounter a member, give the member's real estate to the root. In this way, the root node is the property of the whole family. You only need to sort the root node to output
The following figure shows the process of query set tree creation.
#include<bits/stdc++.h> using namespace std; const int Max=10000; int Min;//Minimum coding struct node { int num;//Family representative number int peoples;//Number of families double HouseCount;//Number of real estate units double HouseArea;//Real estate area }Node[Max]; void init()//Node initialization { for(int i=0;i<=9999;i++) { Node[i].num=i;//Everyone is his own representative Node[i].HouseArea=0; Node[i].HouseCount=0; Node[i].peoples=1; } } int Find(int x)//Find the source of x { if(x==Node[x].num) return x;//Returns the source code. return Node[x].num=Find(Node[x].num); } void join(int x,int y)//Put y into the family and compare it with the family representative, and take the one with the smaller code as the representative. { x=Find(x);//Always the smallest code in the family y=Find(y); if(x>y) swap(x,y); Node[y].num=x; } bool cmp(int x,int y)//Sort the node nodes. Only the family representatives in the node are sorted according to the per capita area. { if(Node[x].HouseArea/Node[x].peoples==Node[y].HouseArea/Node[y].peoples) return Node[x].num<Node[y].num;//If the average area is the same, the front number is smaller than the rear number return Node[x].HouseArea/Node[x].peoples>Node[y].HouseArea/Node[y].peoples; //Otherwise, the one with large average area is in front. } int main() { int n,vist[Max]={0},family[Max];//The vist tag number exists. Vist [number] = 1, and the family stores the family representative. cin>>n; init();//Node initialization. int my,father,mather,k,son; for(int i=0;i<n;i++) { cin>>my>>father>>mather>>k; vist[my]=1,vist[father]=1,vist[mather]=1;//Mark the number that exists. if(father!=-1) join(my,father);//Put my and father into a family. if(mather!=-1) join(my,mather); while(k--) { cin>>son; vist[son]=1; join(my,son); } cin>>Node[my].HouseCount>>Node[my].HouseArea;//Put the number and area of real estate into my. My is not necessarily a family representative. } k=0; for(int i=0;i<=9999;i++)//Find the representative and assign the correct value to the representative. { if(vist[i]&&Find(i)==i)//Find the representative. { family[k++]=i; for(int j=0;j<=9999;j++) { if(Find(j)==i&&i!=j)//Find member J (J is in i's family and j is not representative). { Node[i].peoples++;//All members' properties are put into the family representative first Node[i].HouseArea+=Node[j].HouseArea; Node[i].HouseCount+=Node[j].HouseCount; } } } } //The family is actually stored as the code of the family representative and the subscript of the node. sort(family,family+k,cmp);//Sort node s, that is, sort delegates. cout<<k<<endl; for(int i=0;i<k;i++) printf("%04d %d %0.3f %0.3f\n",Node[family[i]].num,Node[family[i]].peoples,Node[family[i]].HouseCount/Node[family[i]].peoples,Node[family[i]].HouseArea/Node[family[i]].peoples); return 0; }