1114 Family Property (25 分)

This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:

ID Father Mother k child1 child2 ... childk M Area

where ID is a unique 4-digit identification number for each person; Father and Mother are the ID's of this person's parents (if a parent has passed away, -1 will be given instead); k (0≤k≤5) is the number of children of this person; Child**i's are the ID's of his/her children; Mestate is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.

Output Specification:

For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:

\(ID M AVG_{sets} AVG_{area}\)

where ID is the smallest ID in the family; M is the total number of family members; AVGsets is the average number of sets of their real estate; and AVGarea is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID's if there is a tie.

Sample Input:

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

Sample Output:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

分析:要将每个家庭的数据分离出来,使用并查集即可实现,数据处理上有两种方法:

1.先将录入的数据按照输入的形式存在数组中,录入的同时完成集合的合并,然后再从录入的数据中挑出需要的数据,最后对挑出的数据仓晒礼输出;

2.在录入的同时进行集合的合并,并将数据按照输出的格式存储,然后进行处理

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<unordered_map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int nmax = 10100;
int fath[nmax];
void init(){
for(int i = 0; i < nmax; ++i)fath[i] = i;
}
int findF(int x){
int z = x;
while(x != fath[x])x = fath[x];
while(z != fath[z]){
int temp = fath[z];
fath[z] = x;
z = temp;
}
return x;
}
void Union(int a, int b){
int fa = findF(a), fb = findF(b);
if(fa < fb)fath[fb] = fa;
else if(fa > fb)fath[fa] = fb;
}
struct node{
int id, people;
double num, area;
bool flag = false;
bool operator < (node &a)const{
return area != a.area ? area > a.area : id < a.id;
}
}ans[nmax];
struct data{
int id, fid, mid, k;
int child[6];
int num, area;
}v[nmax];
bool vis[nmax] = {false};
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
init();
int N;
scanf("%d", &N);
//录入数据并合并,由于序号不连续,要标记哪些序号已经出现过
for(int i = 0; i < N; ++i){
int id, fid, mid, k;
scanf("%d%d%d%d", &id, &fid, &mid, &k);
v[id].id = id;
v[id].fid = fid;
v[id].mid = mid;
v[id].k = k;
vis[id] = true;
if(fid != -1){
Union(id, fid);
vis[fid] = true;
}
if(mid != -1){
Union(id, mid);
vis[mid] = true;
}
for(int j = 0; j < k; ++j){
scanf("%d", &v[id].child[j]);
Union(id, v[id].child[j]);
vis[v[id].child[j]] = true;
}
scanf("%d%d", &v[id].num, &v[id].area);
}
//统计并抽出需要的数据,用flag标记该根节点是否已经出现
int cnt = 0;
for(int i = 0; i < nmax; ++i){
if(vis[i] == true){
int index = findF(i);
ans[index].id = index;
ans[index].people++;
ans[index].num += v[i].num;
ans[index].area += v[i].area;
if(ans[index].flag == false)cnt++;
ans[index].flag = true;
}
}
//处理
for(int i = 0; i < nmax; ++i){
if(ans[i].flag == true){
ans[i].num /= ans[i].people;
ans[i].area /= ans[i].people;
}
}
//排序输出
sort(ans, ans + nmax);
printf("%d\n", cnt);
for(int i = 0; i < cnt; ++i){
printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num, ans[i].area);
}
return 0;
}
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int Nmax = 10000;
int father[Nmax];
bool vis[Nmax] = {false};
struct node{
int id, peoNum;
double Sav, Aav;
};
void init(){
for(int i = 0; i < Nmax; ++i)father[i] = i;
}
int findF(int x){
int z = x;
while(x != father[x])x = father[x];
while(z != father[z]){
int temp = father[z];
father[z] = x;
z = temp;
}
return x;
}
void Union(int a, int b){
int fa = findF(a), fb = findF(b);
if(fa != fb)father[fb] = fa;
}
bool cmp(node a, node b){
bool flag = false;
if(a.Aav / a.peoNum > b.Aav / b.peoNum){
flag = true;
}else if(a.Aav / a.peoNum == b.Aav / b.peoNum && a.id < b.id){
flag = true;
}
return flag;
}
int main(){
init();
int N = 0;
vector<node>v, v2;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
int f, m, id, k, idm = 10000;
int peonum = 0;
scanf("%d%d%d%d", &id, &f, &m, &k);
if(f == -1 && m == -1)idm = id;
if(f == -1 && m != -1)idm = min(id, m);
if(f != -1 && m == -1)idm = min(id, f);
if(f != -1 && m != -1)idm = min(min(m, f), id);
if(!vis[id]){
peonum = 1;
vis[id] = true;
}
if(f != -1){
Union(f, id);
if(!vis[f]){
peonum++;
vis[f] = true;
}
}
if(m != -1){
Union(m, id);
if(!vis[m]){
peonum++;
vis[m] = true;
} }
for(int j = 0; j < k; ++j){
int child;
scanf("%d", &child);
idm = min(idm, child);
Union(id, child);
if(!vis[child]){
peonum++;
vis[child] = true;
}
}
int setNum, area;
scanf("%d%d", &setNum, &area);
int flag = false;
for(int j = 0; j < v.size(); ++j){
if(findF(v[j].id) == findF(idm)){
v[j].id = min(idm, v[j].id);
v[j].peoNum += peonum;
v[j].Sav += (double)setNum;
v[j].Aav += (double)area;
flag = true;
break;
}
}
if(!flag)v.push_back({idm, peonum , (double)setNum, (double)area});
}
//录入数据的时候只合并了一部分家庭,再合并一遍
v2.push_back(v[0]);
for(int i = 1; i < v.size(); ++i){
int flag = false;
for(int j = 0; j < v2.size(); ++j){
if(findF(v[i].id) == findF(v2[j].id)){
v2[j].id = min(v[i].id, v2[j].id);
v2[j].peoNum += v[i].peoNum;
v2[j].Sav += v[i].Sav;
v2[j].Aav += v[i].Aav;
flag = true;
break;
}
}
if(!flag)v2.push_back(v[i]);
}
cout<<v2.size()<<endl;
sort(v2.begin(), v2.end(), cmp);
for(int i = 0; i < v2.size(); ++i){
printf("%04d %d %.3f %.3f\n", v2[i].id, v2[i].peoNum, v2[i].Sav / v2[i].peoNum, v2[i].Aav / v2[i].peoNum);
}
return 0;
}

【刷题-PAT】A1114 Family Property (25 分)的更多相关文章

  1. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  2. 【刷题-PAT】A1126 Eulerian Path (25 分)

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  3. 【刷题-PAT】A1101 Quick Sort (25 分)

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  4. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  5. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  6. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  7. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  8. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

  9. 【刷题-PAT】A1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

随机推荐

  1. CF946B Weird Subtraction Process 题解

    Content 有两个数 \(a,b\),执行如下操作: 如果 \(a,b\) 中有一个数是 \(0\),结束操作,否则跳到操作 \(2\). 如果 \(a\geqslant 2b\),那么 \(a\ ...

  2. Python第三周 函数详解

    def 函数名(): """注释说明"""" 执行逻辑体 return 返回值 定义不带参数的函数 带参数的函数 默认参数 这个是 ...

  3. Linux(centos)安装es(elasticsearch)

    前提条件--需要安装jdk环境,不同版本的es所对应的jdk版本要求不同,es6的使用jdk1.8可以 1.下载elasticsearch压缩包 下载地址:https://www.elastic.co ...

  4. Qt5绘制仪表盘dashboard

    说明 本文演示Qt版本: Qt5.14. 本文将使用QPainter一步一步绘制仪表盘:刻度.指针.刻度值 注意: 绘制顺序,如果先绘制,则后来绘制的将会覆盖住先前绘制的. 如果需要绘制半透明, 请设 ...

  5. DirectByteBuffer实现原理分析

    1.创建DirectByteBuffer Direct ByteBuffer是通过JNI在Java虚拟机外的内存中分配了一块(所以即使在运行时通过-Xmx指定了Java虚拟机的最大堆内存,还是可能实例 ...

  6. 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...

  7. 【LeetCode】443. String Compression 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...

  8. Java基础(八)——IO流2_缓冲流、转换流

    一.缓冲流 1.介绍 缓冲流:不能直接作用在文件上,需要包一层,它是一种处理流.用于提高文件的读写效率.它在流的基础上对流的功能进行了增强.提高读写速度的原因:内部提供了一个缓冲区.缺省使用 8192 ...

  9. 「旅游信息管理系统」 · Java Swing + MySQL 开发

    代码写得烂,写博客纯属记录! 微信公众号:BugLass 码云仓库地址:https://gitee.com/ynavc/tourism_sys 源代码及文档打包下载:https://download. ...

  10. EMQX源码编译过程

    以emqx4.0.7版本为例 1.安装erlang环境 可以参考:https://www.cnblogs.com/shanfeng1000/p/11951703.html 这里需要注意一下,要按照em ...