PTA Family Property (25分)

It is the human mind that releases infinite light, and it is also the human mind that creates boundless darkness. Light and darkness are intertwined and fight together. This is the world for which we are nostalgic and helpless.

This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:

ID Father Mother k Child​1​​⋯Child​k​​ M​estate​​ Area

where ID is a unique 4-digit identification number for each person; Father and Mother are the ID's of this person's parents (if a parent has passed away, -1 will be given instead); k (0≤k≤5) is the number of children of this person; Child​i​​'s are the ID's of his/her children; M​estate​​ is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.

Output Specification:

For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:

ID M AVG​sets​​ AVG​area​​

where ID is the smallest ID in the family; M is the total number of family members; AVG​sets​​ is the average number of sets of their real estate; and AVG​area​​ is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID's if there is a tie.

Sample Input:

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

Sample Output:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=10010;
struct DATA{
    int id, fid, mid, num, area;
    int cid[10];
}data[1010];
struct node {
    int id, pp;
    double num, area;
    bool fg=false;
}P[maxn];
int fa[maxn];
bool vis[maxn];
int fd(int x) {
    while(x!=fa[x])
        x=fa[x];
    return x;
}
void Union(int a, int b) {
    int faA=fd(a);
    int faB=fd(b);
    if(faA>faB)
        fa[faA]=faB;
    else if(faA<faB)
        fa[faB]=faA;
}
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, ct=0;
    scanf("%d", &n);
    for(int i=0; i<maxn; i++)
        fa[i]=i;
    for(int i=0; i<n; i++) {
        scanf("%d %d %d %d", &data[i].id, &data[i].fid, &data[i].mid, &k);
        vis[data[i].id]=true;
        if(data[i].fid!=-1) {
            vis[data[i].fid]=true;
            Union(data[i].fid, data[i].id);
        }
        if(data[i].mid!=-1){
            vis[data[i].mid]=true;
            Union(data[i].mid, data[i].id);
        }
        for(int j=0; j<k; j++) {
            scanf("%d", &data[i].cid[j]);
            vis[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=fd(data[i].id);
        P[id].id=id;
        P[id].num+=data[i].num;
        P[id].area+=data[i].area;
        P[id].fg=true;
    }
    for(int i=0; i<maxn; i++) {
        if(vis[i])
            P[fd(i)].pp++;
        if(P[i].fg)
            ct++;
    }
    for(int i=0; i<maxn; i++) {
        if(P[i].fg){
            P[i].num=(double)(P[i].num*1.0/P[i].pp);
            P[i].area=(double)(P[i].area*1.0/P[i].pp);
        }
    }
    sort(P, P+maxn, cmp1);
    printf("%d\n", ct);
    for(int i=0; i<ct; i++)
        printf("%04d %d %.3f %.3f\n", P[i].id, P[i].pp, P[i].num, P[i].area);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44170305/article/details/108511630
Recommended