L2-007 家庭房产 (25 分)
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。
输入格式:
输入第一行给出一个正整数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
邻接表的使用,实现关联成员搜索
AC Code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<cctype>
#include<map>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#define INF 0x3f3f3f3f
using namespace std;
static const int MAX_N = 1e5;
typedef long long ll;
struct Node{ //最小编号、成员数、人均房产、人均地产
int minl;
int num;
double house;
double area;
}info[MAX_N];
struct Node res[MAX_N];
int cnt; //家庭数
vector<int> V[MAX_N]; //邻接表存储关联节点
bool vis[MAX_N], used[MAX_N];
bool cmp(const Node &a, const Node &b) { //优先级排序
return a.area > b.area || (a.area == b.area && a.minl < b.minl);
}
void dfs(int s) { //搜索关联节点
vis[s] = true;
res[cnt].minl = min(s, res[cnt].minl);
res[cnt].num++;
res[cnt].house += info[s].house;
res[cnt].area += info[s].area;
for (int i = 0; i < V[s].size(); i++) {
if (!vis[V[s][i]]) dfs(V[s][i]);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int vnum, vfa, vmo, vk;
scanf("%d%d%d%d", &vnum, &vfa, &vmo, &vk);
used[vnum] = true;
if (vfa != -1) { V[vnum].push_back(vfa); V[vfa].push_back(vnum); }
if (vmo != -1) { V[vnum].push_back(vmo); V[vmo].push_back(vnum); }
for (int j = 0; j < vk; j++) {
int vch;
scanf("%d", &vch);
V[vnum].push_back(vch);
V[vch].push_back(vnum);
}
scanf("%lf%lf", &info[vnum].house, &info[vnum].area);
}
cnt = 0;
for (int i = 0; i < 10000; i++) {
if (used[i]) {
if (!vis[i]) {
res[cnt].minl = MAX_N;
res[cnt].num = res[cnt].house = res[cnt].area = 0;
dfs(i);
res[cnt].house /= res[cnt].num;
res[cnt].area /= res[cnt].num;
cnt++;
}
}
}
sort(res, res + cnt, cmp);
printf("%d\n", cnt);
for (int i = 0; i < cnt; i++) {
printf("%04d %d %.3f %.3f\n", res[i].minl, res[i].num, res[i].house, res[i].area);
}
return 0;
}