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 = +;
struct person{//记录每个人拥有的房产
double home_num=,tot_area=;
int color=;//属于几号家庭
person() {} };
int n;
set<int>S;
map<int, person>M;//一个编号对应了一个人
struct Family {
int min_id=INF,num_person=;
double tot_taoshu=, tot_area=;
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 = ; i < n; i++) {
par[i] = i;
Rank[i] = ;
}
}
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 = ; i < n; i++) {//input
int id, dad, mom, child, num;double home, area;
scanf("%d%d%d%d",&id,&dad,&mom,&num);
if (dad != -) { unite(id, dad);S.insert(dad); }//!!!!
if (mom != -) { unite(id, mom);S.insert(mom); }//!!!!!
S.insert(id);//记录id
for (int j = ; 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 = ;
for (set<int>::iterator it = S.begin(); it != S.end();it++) {
int tmp = *it;
if (M[tmp].color == ) {//还没被染色
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 = ; i <= co;i++) {
family[i].tot_area /= family[i].num_person;
family[i].tot_taoshu /= family[i].num_person;
}
sort(family + , family + co+);
printf("%d\n",co);
for (int i = ; i<=co;i++) {
printf("%04d %d %.3f %.3f\n",family[i].min_id,family[i].num_person,family[i].tot_taoshu,family[i].tot_area);
}
}
return ;
}

pat 团体天梯赛 L2-007. 家庭房产的更多相关文章

  1. pat 团体天梯赛 L3-007. 天梯地图

    L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...

  2. pat 团体天梯赛 L3-015. 球队“食物链”

    L3-015. 球队“食物链” 时间限制 1000 ms 内存限制 262144 kB 代码长度限制 8000 B 判题程序 Standard 作者 李文新(北京大学) 某国的足球联赛中有N支参赛球队 ...

  3. pat 团体天梯赛 L1-039. 古风排版

    L1-039. 古风排版 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 中国的古人写文字,是从右向左竖向排版的.本题就请你编写 ...

  4. pat 团体天梯赛 L2-012. 关于堆的判断

    L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...

  5. pat 团体天梯赛 L3-010. 是否完全二叉搜索树

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  6. pat 团体天梯赛 L3-009. 长城

    L3-009. 长城 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 邓俊辉(清华大学) 正如我们所知,中国古代长城的建造是为了抵御外 ...

  7. pat 团体天梯赛 L2-011. 玩转二叉树

    L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...

  8. pat 团体天梯赛 L2-010. 排座位

    L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...

  9. pat 团体天梯赛 L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

随机推荐

  1. 32-3题:LeetCode103. Binary Tree Zigzag Level Order Traversal锯齿形层次遍历/之字形打印二叉树

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  2. 火狐IE event和target的兼容

    一.event对象 IE 中可以直接使用 window.event 对象,而 FF 中则不可以,解决方法之一如下: var theEvent = window.event || arguments.c ...

  3. The Moving Points - HDU - 4717 (模拟退火)

    题意 二维空间中有\(n\)个运动的点,每个点有一个初始坐标和速度向量.求出一个时间\(T\),使得此时任意两点之间的最大距离最小.输出\(T\)和最大距离. 题解 模拟退火. 这个题告诉了我,初始步 ...

  4. f触发器、存储过程

    drop trigger trig_insert--删除触发器

  5. Selenium与PhantomJS【转】

    爬虫(Spider),反爬虫(Anti-Spider),反反爬虫(Anti-Anti-Spider) 之间恢宏壮阔的斗争... Day 1 小莫想要某站上所有的电影,写了标准的爬虫(基于HttpCli ...

  6. 矩阵乘法在hadoop的实现

    先随机生成一个矩阵,矩阵的行数与列数由用户输入: #!/bin/bashfor i in `seq 1 $1`do for j in `seq 1 $2` do s=$((RANDOM%100)) e ...

  7. Appium运行时没有启动activity的权限:A new session could not be created.(Original error: Permission to start activity denied)

    小白搞appium,遇到启动不了activity的问题: 查找解决方案说是跟AndroidManifest.xml有关系,参考:https://github.com/appium/appium/iss ...

  8. 《鸟哥的Linux私房菜》学习笔记(4)——用户和组

    一.用户和组的基本概念                                              1.用户 用户:用于获取计算机资源或服务的标识符,比如用户名.计算机处理的是UID,用 ...

  9. Migrate a Domain-based Namespace to Windows Server 2008 Mode

    TechNet Library Scripting with Windows PowerShell Windows and Windows Server Automation with Windows ...

  10. User Account Control

    User Account Control 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! User Account Control (UAC : 用户帐户控制)是微软为 ...