pat 团体天梯赛 L2-007. 家庭房产
L2-007. 家庭房产
输入格式:
输入第一行给出一个正整数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. 家庭房产的更多相关文章
- pat 团体天梯赛 L3-007. 天梯地图
L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...
- pat 团体天梯赛 L3-015. 球队“食物链”
L3-015. 球队“食物链” 时间限制 1000 ms 内存限制 262144 kB 代码长度限制 8000 B 判题程序 Standard 作者 李文新(北京大学) 某国的足球联赛中有N支参赛球队 ...
- pat 团体天梯赛 L1-039. 古风排版
L1-039. 古风排版 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 中国的古人写文字,是从右向左竖向排版的.本题就请你编写 ...
- pat 团体天梯赛 L2-012. 关于堆的判断
L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...
- pat 团体天梯赛 L3-010. 是否完全二叉搜索树
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- pat 团体天梯赛 L3-009. 长城
L3-009. 长城 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 邓俊辉(清华大学) 正如我们所知,中国古代长城的建造是为了抵御外 ...
- pat 团体天梯赛 L2-011. 玩转二叉树
L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...
- pat 团体天梯赛 L2-010. 排座位
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- pat 团体天梯赛 L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
随机推荐
- 关于小程序button控件上下边框的显示和隐藏问题
问题: 小程序的button控件上下有一条淡灰色的边框,在空件上加上了样式 border:(none/0); 都没办法让button上下的的边框隐藏: 代码如下 <button class=&q ...
- IDEA整合Mybatis+Struts2+Spring(一)--新建项目
1.IDEA新建Maven项目: (1)依次点击File->New->Project,弹出如下对话框: (2)在弹出的New Project页面上,①选择Maven,② 勾选Create ...
- mysql零散操作
添加对外用户 CREATE USER 'admin'@'%' IDENTIFIED BY '!QAZ2wsx'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%'; ...
- 扒一扒 EventServiceProvider 源代码
Ajax用一句话来说就是无须刷新页面即可从服务器取得数据.注意,虽然Ajax翻译过来叫异步JavaScript与XML,但是获得的数据不一定是XML数据,现在服务器端返回的都是JSON格式的文件. 完 ...
- python3爬虫之Urllib库(一)
上一篇我简单说了说爬虫的原理,这一篇我们来讲讲python自带的请求库:urllib 在python2里边,用urllib库和urllib2库来实现请求的发送,但是在python3种在也不用那么麻烦了 ...
- Flow Problem HDU - 3549
Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...
- Linux命令之---touch
命令简介 linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 命令格式 touch [选项]... 文件... 命令参数 -a 或 ...
- 笔记-python-装饰器
笔记-python-装饰器 1. 装饰器 装饰器的实质是返回的函数对象的函数,其次返回的函数对象是可以调用的,搞清楚这两点后,装饰器是很容易理解的. 1.1. 相关概念理解 首先,要理解在Pyth ...
- 【Keepalived+MySQL】MySQL双主互备+高可用
一.基本信息说明 [DB1] IP: 192.168.102.144 hostname: LVS-Real1 [DB2] IP: 192.168.102.145 hostname: LVS-Real2 ...
- Three Steps to Migrate Group Policy Between Active Directory Domains or Forests Using PowerShell
Three Steps Ahead Have you ever wished that you had three legs? Imagine how much faster you could ru ...