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. getchar输入多行字符,原格式输出(包含换行符)

    #include<stdio.h> int main() { FILE fp; ]; ; char ch; while((ch=getchar())!=EOF){ str[k++]=ch; ...

  2. mysql 查询条件 默认不区分大小写

    mysql查询默认是不区分大小写的 如: 1 2 select * from some_table where str=‘abc'; select * from some_table where st ...

  3. 配置vim nginx.conf高亮

    #!/bin/bashwget http://www.vim.org/scripts/download_script.php?src_id=14376 -O nginx.vimmv nginx.vim ...

  4. javascript sprintf方法

    转载自: http://demon.tw/programming/javascript-sprintf.html function str_repeat(i, m) { for (var o = [] ...

  5. VirtualBox下vim无法正常使用问题解决

    由原来的使用VMware转到使用Virtual Box,发现其vim编辑器不是特别好用,需要进行一下更改设置: 1.使用命令删除vim,sudo apt-get remove vim-common 2 ...

  6. 千万不要错过这几道Python面试题,Python面试题No16

    第1题: python下多线程的限制以及多进程中传递参数的方式? python多线程有个全局解释器锁(global interpreter lock),简称GIL,这个GIL并不是python的特性, ...

  7. 20181225 基于TCP/IP和基于UDP/IP的套接字编程

    一.TCP/IP的套接字编程 服务器端代码: import  socket​server = socket.socket() # 默认是基于TCP# 基于TCP的对象serve=socket.sock ...

  8. LightOJ - 1341 Aladdin and the Flying Carpet(数论)

    题意 有一块矩形(也可能是正方形)的飞毯. 给定飞毯的面积\(n\)和最小可能的边长\(a\),求可能有多少种不同边长的飞毯.(\(1<=a<=n<=1e12\)) 如面积\(n=6 ...

  9. Google Authenticator(谷歌身份验证器)C#版

    摘要:Google Authenticator(谷歌身份验证器),是谷歌公司推出的一款动态令牌工具,解决账户使用时遭到的一些不安全的操作进行的"二次验证",认证器基于RFC文档中的 ...

  10. MongoDB快速入门学习笔记7 MongoDB的用户管理操作

    1.修改启动MongoDB时要求用户验证加参数 --auth 即可.现在我们把MongoDB服务删除,再重新添加服务 mongod --dbpath "D:\work\MongoDB\dat ...