pat 团体天梯赛 L2-007. 家庭房产

L2-007. 家庭房产

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。

输入格式:

输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式给出一个人的房产:

编号 父 母 k 孩子1 ... 孩子k 房产套数 总面积

其中 编号 是每个人独有的一个4位数的编号; 分别是该编号对应的这个人的父母的编号(如果已经过世,则显示-1);k(0<=k<=5)是该人的子女的个数;孩子i是其子女的编号。

输出格式:

首先在第一行输出家庭个数(所有有亲属关系的人都属于同一个家庭)。随后按下列格式输出每个家庭的信息:

家庭成员的最小编号 家庭人口数 人均房产套数 人均房产面积

其中人均值要求保留小数点后3位。家庭信息首先按人均面积降序输出,若有并列,则按成员编号的升序输出。

输入样例:
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
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

思路:并查集,之后统计相关信息。



 #define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring> 
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
#define EPS 1e-5
#define pi cos(-1)
const int N_MAX = 10000+10;
struct person{//记录每个人拥有的房产
  double home_num=0,tot_area=0;
  int color=0;//属于几号家庭
  person() {}
  
};
int n;
set<int>S;
map<int, person>M;//一个编号对应了一个人
struct Family {
int min_id=INF,num_person=0;
double tot_taoshu=0, tot_area=0;
bool operator <(const Family&b)const {
  if (tot_area != b.tot_area)return tot_area > b.tot_area;
  else return min_id < b.min_id;
}
}family[N_MAX];
///////////////并查集
int par[N_MAX];
int Rank[N_MAX];
void init(const int &n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    Rank[i] = 0;
  }
}
int find(const int&x) {
  if (par[x] == x)
    return x;
  else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)return;
  if (Rank[x] < Rank[y]) {
    par[x] = y;
  }
  else {
    par[y] = x;
    if (Rank[x] == Rank[y])Rank[x]++;
  }
}
bool same(const int &x, const int&y) {
  return find(x) == find(y);
}

///////////////////


int main() {
  while (scanf("%d",&n)!=EOF) {
    init(N_MAX);
    for (int i = 0; i < n; i++) {//input
      int id, dad, mom, child, num;double  home, area;
      scanf("%d%d%d%d",&id,&dad,&mom,&num);
      if (dad != -1) { unite(id, dad);S.insert(dad); }//!!!!
      if (mom != -1) { unite(id, mom);S.insert(mom); }//!!!!!
      S.insert(id);//记录id
      for (int j = 0; j < num;j++) {
        scanf("%d",&child);
        S.insert(child);
        unite(child, id);
      }
      scanf("%lf%lf",&home,&area);
      M[id].home_num = home, M[id].tot_area = area;
    }
    
    int co = 0;
    for (set<int>::iterator it = S.begin(); it != S.end();it++) {
      int tmp = *it;
      if (M[tmp].color == 0) {//还没被染色
        co++;
        M[tmp].color = co;
        family[co].min_id = tmp;
        family[co].num_person++;
        family[co].tot_taoshu += M[tmp].home_num;
        family[co].tot_area += M[tmp].tot_area;
        for (set<int>::iterator it2 = S.begin(); it2 != S.end(); it2++) {
          int tmp2 = *it2;
          if (tmp != tmp2&&same(tmp, tmp2)) {
            M[tmp2].color = co;
            family[co].min_id = min(family[co].min_id,tmp2);
            family[co].num_person++;
            family[co].tot_taoshu += M[tmp2].home_num;
            family[co].tot_area += M[tmp2].tot_area;
          }
        }
      }
    }
    for (int i = 1; i <= co;i++) {
      family[i].tot_area /= family[i].num_person;
      family[i].tot_taoshu /= family[i].num_person;
    }
    sort(family + 1, family + co+1);
    printf("%d
",co);
    for (int i = 1; i<=co;i++) {
      printf("%04d %d %.3f %.3f
",family[i].min_id,family[i].num_person,family[i].tot_taoshu,family[i].tot_area);
    }
  }
  return 0;
}

 
 
原文地址:https://www.cnblogs.com/ZefengYao/p/8094838.html