L2-007 家庭房产 (25 分)
 

给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。

输入格式:

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

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

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

并查集经典题 要稍微改写一下unite函数

PTA经典特色题目 算法不难 倒腾起来麻烦 还给要整个排序 还考察了STL的用法(= =)

推荐做

unite要改写一下 id最小的作为根节点

 #include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
int N, M, K;
const int si = ;
int par[si];
int cnt[si];
double area[si];
int housenum[si];
bool vis[si];
struct node {
int id, num;
double avgnum, avgarea; }no[si]; bool cmp (node a, node b) {
if (a.avgarea != b.avgarea) return a.avgarea > b.avgarea;
return a.id < b.id;
}
using namespace std; int find(int x) {
if (x == par[x]) return par[x];
return par[x] = find(par[x]);
}
bool same(int x, int y) {
return find(x) == find(y);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (x < y) {
par[y] = par[x];
}
else {
par[x] = par[y];
}
}
int main() {
cin >> N;
for (int i = ; i < si; i++) par[i] = i; int f, m, id, k, tp;
for (int i = ; i < N; i++) {
cin >> id >> f >> m >> k;
if (f != -) {
unite(id, f);
vis[f] = ;
}
if (m != -) {
unite(id, m);
vis[m] = ;
}
vis[id] = ;
for (int j = ; j < k; j++) {
scanf("%d", &tp);
unite(id, tp);
vis[tp] = ;
}
cin >> housenum[id] >> area[id];
}
set<int> st;
for (int i = ; i < si; i++) {
if (!vis[i]) continue;
int x = find(i);//这个家庭
cnt[x]++;//人口数++
if (x != i) {
area[x] += area[i];
housenum[x] += housenum[i];
}
st.insert(x);
}
set<int>:: iterator it = st.begin();
int count = ;
while (it != st.end()) {
int x = *it;
no[count].id = x;
no[count].num = cnt[x];
no[count].avgnum = housenum[x] * 1.0 / cnt[x];
no[count++].avgarea = area[x] * 1.0 / cnt[x];
it++;
}
cout << count << endl;
sort (no, no + count, cmp);
for (int i = ; i < count; i++) {
printf("%04d %d %.3f %.3f\n", no[i].id, no[i].num, no[i].avgnum, no[i].avgarea);
}
return ;
}

L2-007 家庭房产 (25 分)的更多相关文章

  1. L2-007 家庭房产 (25分) 并查集

    题目链接 题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去.这个题有个坑编号可能为0000,会错数据3和5. 1 #include<bits/stdc++.h> 2 usin ...

  2. PAT L2-007 家庭房产

    https://pintia.cn/problem-sets/994805046380707840/problems/994805068539215872 给定每个人的家庭成员和其自己名下的房产,请你 ...

  3. 【刷题-PAT】A1114 Family Property (25 分)

    1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...

  4. 团体程序设计天梯赛-练习集L2-007. 家庭房产

    L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...

  5. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  6. 1114 Family Property (25 分)

    1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...

  7. 天梯赛 L2-007. (并查集) 家庭房产

    题目链接 题目描述 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式 ...

  8. PATL2-007. 家庭房产-并查集

    L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...

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

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

随机推荐

  1. SQL Server 2005 企业版没有 Microsoft SQL Server Management

    我从网上下载的:SQL Server 2005 集成sp2的 企业版安装后没发现 Management Studio管理工具,起初以为是自己安装时没装上,昨天试了全部安装后还是没找到,很是郁闷,在网上 ...

  2. java Comparable and Comparator

    1.Comparable简介 此接口对实现它的每个类的对象强加一个总排序.这种排序被称为类的自然排序,类的compareTo方法被称为其自然比较方法.可以通过 Collections.sort(和Ar ...

  3. python - 初识面向对象

    1.初识面向对象       面向过程:一切以事务的发展流程为中心           优点:负责的问题流程化,编写相对简单         缺点:可扩展性差,只能解决一个问题,改造也会很困难,牵一发 ...

  4. orace如何创建函数并调用

    我们来定义一个oracle的函数 create or replace function 方法名(参数名1 参数类型,参数名2 参数类型,参数名3 参数类型)return 返回类型 is num_C n ...

  5. 在Ubuntu 18.04 安装 MySQL 8.0

    在Ubuntu 18.04 安装 MySQL 8.0 ① 登入 mysql 官网,在官网中下载 deb 包,点击该链接,即可下载. https://dev.mysql.com/downloads/re ...

  6. 银行卡卡bin

    卡BIN指的是发卡行识别码,英文全称是 Bank Identification Number,缩写为 BIN.中文即“银行识别代码”  银行卡的卡号是标识发卡机构和持卡人信息的号码 一般是13-19位 ...

  7. week2

    三元函数: a,b,c = 1,2,3 d = a if a>b else c print(d) #list 用法: lst = [1,2,3,4,5] print(lst[0:3]) prin ...

  8. 网络编程-day4

    #服务端 import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self): while 1: ...

  9. null 和System.DBNull.Value

    row[column]的值为DBNull.Value的话,说明它是从数据库中取到值了,对应了数据库中的空值:但如果row[column]的值为null的话,说明没有从数据库中取到值. DBNull.V ...

  10. windows版influxDB安装与配置

    一.下载链接https://portal.influxdata.com/downloads,选windows版 二.解压到安装盘,目录如下 三.修改conf文件,代码如下,直接复制粘贴(1.4.2版本 ...