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 Nlines follow, each gives the infomation of a person who owns estate in the format:

ID Father Mother k Child​1​​⋯Child​k​​ M​estate​​ 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; M​estate​​ 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; AVG​sets​​ is the average number of sets of their real estate; and AVG​area​​ 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

题目大意:找出一个家庭中人均有几套房产,和人均面积;输入中为 当前人的ID  父亲ID  母亲ID  当前人拥有几套 当前人拥有总面积。如果父母去世了,则用-1表示。输出要求按平均房产套数递减,如果相同,那么按ID递增排列。

//确实是使用并查集。

//遇到问题:遇到像8888这种单户的,该怎么处理,我写的里边似乎处理不了。

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include<cstdio>
using namespace std;
struct Peo{
int father,estate,area;
Peo(){father=-;}
}peo[];
map<int,int> mp;
struct Far{
int id,mem;
double avge,avga;
};
int findF(int a){
if(peo[a].father==-)return a;
int k=peo[a].father;
while(peo[k].father!=-){
peo[a].father=peo[k].father;
a=k;
k=peo[a].father;
//cout<<"wwww";
}
//cout<<"fff";
return k;
}
void unionF(int a,int b){
int fa=findF(a);
int fb=findF(b);
// if(fa>fb)peo[fa].father=fb;
// else peo[fb].father=fa;//这里出现了问题啊,得判断是否等于,等于的话,啥也不用了。
if(fa>fb)peo[fa].father=fb;
else if(fa<fb) peo[fb].father=fa;
//cout<<"uuuu";
} bool cmp(Far & a,Far & b){
return a.avge!=b.avge?a.id<b.id:a.avge>b.avge;
}
int main() {
int n;
cin>>n;
//fill(father,father+10000,-1);
int id,fa,mo,k,child;
for(int i=;i<n;i++){
cin>>id>>fa>>mo>>k;
//先将当前的人和父亲母亲合并
if(fa!=-)
unionF(id,fa);
if(mo!=-)
unionF(id,mo);
for(int j=;j<k;j++){
cin>>child;
unionF(id,child);
}
// peo[id].father=id;
cin>>peo[id].estate>>peo[id].area;
} vector<int> vt[];
for(int i=;i<;i++){
if(peo[i].father!=-){
//cout<<"hh";
mp[peo[i].father]++;
//怎么记录每个簇里有谁呢?
vt[peo[i].father].push_back(i);
}
} //最终还得sort一下。
cout<<mp.size()<<'\n';
vector<Far> far;
for(auto it=mp.begin();it!=mp.end();it++){
int id=it->first;
int mem=vt[id].size();
int tote,tota;
tote=peo[id].estate;
tota=peo[id].area;
for(int i=;i<vt[id].size();i++){
tote+=peo[vt[id][i]].estate;
tota+=peo[vt[id][i]].area;
}
far.push_back(Far{id,mem,tote*1.0/mem,tota*1.0/mem});
}
sort(far.begin(),far.end(),cmp);
for(int i=;i<far.size();i++){
printf("%04d %d %.3f %.3f\n",far[i].id,far[i].mem,far[i].avge,far[i].avga);
} return ;
}

//写成了这样,最终决定放弃!

代码转自:https://www.liuchuo.net/archives/2201

#include <cstdio>
#include <algorithm>
using namespace std;
struct DATA {
int id, fid, mid, num, area;
int cid[];//最多有10个孩子。
}data[];
struct node {
int id, people;
double num, area;
bool flag = false;
}ans[];
int father[];
bool visit[];
int find(int x) {
while(x != father[x])//这样真的好简便,我为啥写那么复杂呢。
x = father[x];
return x;
}
void Union(int a, int b) {
int faA = find(a);
int faB = find(b);
if(faA > faB)
father[faA] = faB;
else if(faA < faB)
father[faB] = faA;
}
int cmp1(node a, node b) {
if(a.area != b.area)
return a.area > b.area;
else
return a.id < b.id;
}
int main() {
int n, k, cnt = ;
scanf("%d", &n);
for(int i = ; i < ; i++)
father[i] = i;//将父亲设置为自己。
for(int i = ; i < n; i++) {
scanf("%d %d %d %d", &data[i].id, &data[i].fid, &data[i].mid, &k);
//直接读进来,不使用中间变量。
//并没有使用下标作为id啊。
visit[data[i].id] = true;//标记出现过了。
if(data[i].fid != -) {
visit[data[i].fid] = true;
Union(data[i].fid, data[i].id);//将id作为并查集中的关键字合并。
}
if(data[i].mid != -) {
visit[data[i].mid] = true;
Union(data[i].mid, data[i].id);
}
for(int j = ; j < k; j++) {
scanf("%d", &data[i].cid[j]);
visit[data[i].cid[j]] = true;
Union(data[i].cid[j], data[i].id);
}
scanf("%d %d", &data[i].num, &data[i].area);
}
for(int i = ; i < n; i++) {
int id = find(data[i].id);//找到当前人的父亲,
ans[id].id = id;//现在这个ans中使用id作为下标索引了!
ans[id].num += data[i].num;
ans[id].area += data[i].area;
ans[id].flag = true;
}
for(int i = ; i < ; i++) {
if(visit[i])//如果它出现过。
i ans[find(i)].people++;
if(ans[i].flag)//标记有几簇人家。
cnt++;
}
for(int i = ; i < ; i++) {
if(ans[i].flag) {
ans[i].num = (double)(ans[i].num * 1.0 / ans[i].people);
ans[i].area = (double)(ans[i].area * 1.0 / ans[i].people);
//并没有多个num属性,都是用一个存的,一开始就设为double。
//我居然还分开定义了。
}
}
sort(ans, ans + , cmp1);
printf("%d\n", cnt);
for(int i = ; i < cnt; i++)
printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num, ans[i].area);
return ;
}

1.将Father初始化为了自己,那么find函数就简单了,学习了

2.使用一个bool数组来标记出现过的点和没出现过的点,就解决了一家只有一口的情况。

3.在求ans向量的时候,仍使用了find,其实不用map的,find找到父亲都是可以用的。

4.因为data[i].id里存的是已经出现过的id,对那些没出现过的ans.flag肯定不会被标记为true的!

这是错误理解:ans里存储的所有的10000个人的情况,对于那些没有出现过的id,ans[i].flag也是true,只不过它所有的数据都是0,在经过排序之后,再通过簇进行控制输出,其他的不输出。

总之,学习了。

PAT 1114 Family Property[并查集][难]的更多相关文章

  1. PAT甲级——1114 Family Property (并查集)

    此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...

  2. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  3. PAT 1114 Family Property

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  4. PAT L2-013 红色警报(并查集求连通子图)

    战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...

  5. 1107 Social Clusters[并查集][难]

    1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...

  6. PAT甲级1013题解——并查集+路径压缩

    题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...

  7. PAT甲级1004题解——并查集思想改

    题目分析:本题开始一直在考虑如何将每一个节点通过一种合适的数据结构存储起来(一对多的关系),最后发现借助并查集的思想可以用一个数组p,p[i]存放i节点的父节点,每次查询编号为i的节点属于第几层且判断 ...

  8. 【algo&ds】【pat】5.并查集及其应用

    1.并查集的定义 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题.有一个联合-查找算法(union-find algorithm)定义了两 ...

  9. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

随机推荐

  1. linux系统中,tee命令的使用

    需求描述: 今天在看nginx内容的过程,遇到了tee这个命令,所以查询了下,在这里记录下使用方法. 操作过程: 1.执行以下的命令 [root@testvm ~]# uname -n | tee h ...

  2. 安装Phoenix时./sqlline.py执行报错File "./sqlline.py", line 27, in <module> import argparse ImportError: No module named argparse解决办法(图文详解)

    不多说,直接上干货! 前期博客 Apache版Phoenix的安装(图文详解) 问题现象 Traceback (most recent call last): File , in <module ...

  3. 改善C#程序的建议5:引用类型赋值为null与加速垃圾回收

    http://www.cnblogs.com/luminji/archive/2011/04/07/2007205.html 在标准的Dispose模式中(见前一篇博客“C#中标准Dispose模式的 ...

  4. linux添加自启服务(程序)

    修改 /etc/rc.d/rc.local 文件,加入启动程序的脚本命令就可以了 例如: /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongo ...

  5. /etc/logrotate.conf

    /etc/logrotate.conf 是 Logrotate 工具的一个配置文件,这个工具用来自动切割系统日志,Logrotate 是基于 cron 来运行的,如下: [root@localhost ...

  6. 嵌入式Linux下Qt的中文显示

    一般情况下,嵌入式Qt界面需要中文显示,下面总结自己在项目中用到的可行的办法 1,下载一种中文简体字体,比如我用的是”方正准圆简体“,把字体文件放在ARM开发板系统的Qt字库中,即/usr/lib/f ...

  7. Receiver type for instance message is a forward

    本文转载至 http://my.oschina.net/sunqichao/blog?disp=2&catalog=0&sort=time&p=3 这往往是引用的问题.ARC要 ...

  8. iOS 使用AFN 进行单图和多图上传 摄像头/相册获取图片,压缩图片

    图片上传时必要将图片进行压缩,不然会上传失败 首先是同系统相册选择图片和视频.iOS系统自带有UIImagePickerController,可以选择或拍摄图片视频,但是最大的问题是只支持单选,由于项 ...

  9. 如何在Linux系统通过命令行生成随机文件

    版权声明:本文由胡恒威原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/86 来源:腾云阁 https://www.qclou ...

  10. nexus 增加代理仓库 无法搜到snapshot的jar包 解决方法

    如题, nexus 私服 增加了另一个 私服,  但是无法搜到 版本中带有 snapshot字样的 jar包. 环境情况: 1.老私服: 首先版本中带有 snapshot字样的 jar包,是发布在 老 ...