L2-007. Household Property
Time limit
400 ms
Memory limit
65536 kB
Code Length Limitation
8000 B
Judgment procedure
Standard
author
Chen Yue
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:
Enter the first line to give a positive integer N (<=1000), and then N lines, each line giving a person's property in the following format:
Number Parents K Children 1.. Total area of children's k-house
The number is a unique four-digit number for each person; the father and mother are the parents of the person corresponding to the number (if they have passed away, show-1); K (0<=k<=5) is the number of children of the person; and child i is the number of their children.
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 according to per capita area, and if it is juxtaposed, it is output in ascending order according to member number.
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
Problem Solution: And the application of collection,, but I wrote too much trouble.==
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define MST(s,q) memset(s,q,sizeof(s))
#define INF 0x3f3f3f3f
#define MAXN 1005
using namespace std;
struct person // Everybody
{
int NO; // number
int father, mother; // Number of parents
int kidnum; // Number of children
int kid[6]; // Child number
int home; // House number
int S_home; // Property area
} per[1005];
struct Family // Every family
{
int Min_No; // Minimum number of family members
int num; // Family population
double sum_home, p_home; // Total and Per Capita Housing Sets
double sum_Shome, p_Shome; // Total area of real estate and per capita area of real estate
} family[10005];
set<int> Set[10005]; // Calculating the number of households
map<int, int> mp;
bool cmp(Family a, Family b)
{
if (a.p_Shome == b.p_Shome)
return a.Min_No < b.Min_No;
return a.p_Shome > b.p_Shome;
}
/*Union checking set*/
struct edge
{
int u, v;
} Edge[10000];
int F[10005];
int find(int x)
{
return F[x] == x ? x : F[x] = find(F[x]);
}
int main()
{
int N, k, edgeCnt = 0;
cin >> N;
for (int i = 0; i < 10005; i++) F[i] = i;
for (int i = 0; i < N; i++)
{
cin >> per[i].NO >> per[i].father >> per[i].mother;
if (per[i].father != -1)
Edge[edgeCnt].u = per[i].NO, Edge[edgeCnt++].v = per[i].father; // Add edge
if (per[i].mother != -1 )
Edge[edgeCnt].u = per[i].NO, Edge[edgeCnt++].v = per[i].mother; // Add edge
cin >> per[i].kidnum; // Enter the number of children
for (int j = 0; j < per[i].kidnum; j++)
{
cin >> per[i].kid[j];
Edge[edgeCnt].u = per[i].NO, Edge[edgeCnt++].v = per[i].kid[j]; // Add edge
}
cin >> per[i].home >> per[i].S_home;
}
// Union checking set
for (int i = 0; i < edgeCnt; i++)
{
int a = find(Edge[i].u);
int b = find(Edge[i].v);
F[a] = b;
}
MST(family, 0);
int cnt = 1; // Number of families
for (int i = 0; i < N; i++)
{
int pos = find(per[i].NO);
if (!mp[pos])
{
mp[pos] = cnt ;
cnt++;
}
pos = mp[pos]; // The number of families
// Calculate the number and minimum number of households
Set[pos].insert(per[i].NO);
if (per[i].father != -1)
Set[pos].insert(per[i].father);
if (per[i].mother != -1)
Set[pos].insert(per[i].mother);
for (int j = 0; j < per[i].kidnum; j++)
Set[pos].insert(per[i].kid[j]);
family[pos].num = Set[pos].size();
family[pos].Min_No = *Set[pos].begin();
// Calculating Per Capita Housing Set Number and Per Capita Housing Area
family[pos].sum_home += per[i].home;
family[pos].p_home = family[pos].sum_home / Set[pos].size();
family[pos].sum_Shome += per[i].S_home;
family[pos].p_Shome = family[pos].sum_Shome / Set[pos].size();
}
// Sort by condition
sort(family + 1, family + cnt , cmp);
cout << cnt - 1 << endl;
for (int i = 1; i < cnt; i++)
{
printf("%04d %d", family[i].Min_No, family[i].num);
printf(" %.3lf %.3lf\n", family[i].p_home, family[i].p_Shome );
}
}