L2-007 Family Property (25 points)

Posted by atrum on Tue, 23 Jul 2019 09:49:36 +0200

L2-007 Family Property (25 points)

Given everyone's family members and their own real estate, please count the number of people in each family, the per capita real estate area and the number of housing units.

Input format:

Input the first line gives a positive integer N (< 1000), followed by N lines, each line gives a person's property in the following format:

Number Parents, Kids, 1..... Kids, k Real Estate Set Number, Total Area

The number is a unique four-digit number for each person; the father and mother are the numbers of the parents of the person corresponding to the number (if they have passed away, it shows - 1); k (0 < k < 5) is the number of the children of the person; and the child i is the number of the children of the person.

Output format:

First, output the number of families in the first line (all relatives belong to the same family). The information of each family is then output in the following format:

Minimum Number of Family Members

Among them, the average value per capita requires that the last three decimal places be retained. Family information is first output in descending order by per capita area, and in ascending order by membership number if it is juxtaposed.

Input sample:

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

Output sample:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

 

 

Analysis: and collection. Two arrays of structured bodies are used to receive data. When receiving data, it is realized and collected incidentally.

Operate union, and output the final answer with another array of ans_because to calculate the number of family mushrooms, the visit tag appears all the time.

For each node, people++ counts the number of muscles. Mark flflag == true, and compute the number of true cnt.

Know how many families there are. The cnt number before the output after sorting is the answer ~~

#include <cstdio>
#include <algorithm>
using namespace std;
struct DATA {
 int id, fid, mid, num, area;
 int cid[10];
}data[1005];
struct node {
 int id, people;
 double  num, area;
 bool    flag = false;
}ans[10000];
int father[10000];
bool visit[10000];
int find(int x) {
    return x == father[x] ? x : father[x] = find( father[x]);
}
void Union(int a, int b) {
 int fA = find(a);
 int fB = find(b);
 if(fA > fB)
 father[fA] = fB;
 else if(fA < fB)
      father[fB] = fA;
}
int cmp1(node a, node b) {
 if(  a.area != b.area)
      return a.area > b.area;
 else return a.id < b.id;
}
int main() {
 int n, k, cnt = 0;
 scanf("%d", &n);
 for( int i = 0; i < 10000; i++)
      father[i] = i;
 for( int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &data[i].id, &data[i].fid, &data[i].mid,&k);
      visit[data[i].id] = true;
      if( data[i].fid != -1) {
          visit[data[i].fid] = true;
          Union(data[i].fid, data[i].id);
       }
      if( data[i].mid != -1) {
          visit[data[i].mid] = true;
          Union(data[i].mid, data[i].id);
       }
      for( int j = 0; j < k; j++) {
           scanf("%d", &data[i].cid[j]);
           visit[data[i].cid[j]] = true;
           Union(data[i].cid[j], data[i].id);
       }
      scanf("%d %d", &data[i].num, &data[i].area);
   }
 for( int i = 0; i < n; i++) {
      int id = find(data[i].id);
      ans[id].id = id;
      ans[id].num += data[i].num;
      ans[id].area += data[i].area;
      ans[id].flag = true;
 }
 for( int i = 0; i < 10000; i++) {
      if( visit[i])
          ans[find(i)].people++;
      if( ans[i].flag)
          cnt++;
  }
 for( int i = 0; i < 10000; i++) {
      if( ans[i].flag) {
          ans[i].num = (double)(ans[i].num * 1.0 / ans[i].people);
          ans[i].area = (double)(ans[i].area * 1.0 / ans[i].people);
      }
 }
 sort(ans, ans + 10000, cmp1);
 printf("%d\n", cnt);
 for( int i = 0; i < cnt; i++)
      printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people,ans[i].num, ans[i].area);
 return 0;
}