PAT 1114 Family Property[并查集][难]
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 Child1⋯Childk Mestate 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; Childi'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
AVGsets AVGarea
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
题目大意:找出一个家庭中人均有几套房产,和人均面积;输入中为 当前人的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[并查集][难]的更多相关文章
- PAT甲级——1114 Family Property (并查集)
此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- PAT 1114 Family Property
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- PAT L2-013 红色警报(并查集求连通子图)
战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- PAT甲级1013题解——并查集+路径压缩
题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...
- PAT甲级1004题解——并查集思想改
题目分析:本题开始一直在考虑如何将每一个节点通过一种合适的数据结构存储起来(一对多的关系),最后发现借助并查集的思想可以用一个数组p,p[i]存放i节点的父节点,每次查询编号为i的节点属于第几层且判断 ...
- 【algo&ds】【pat】5.并查集及其应用
1.并查集的定义 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题.有一个联合-查找算法(union-find algorithm)定义了两 ...
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
随机推荐
- tiny4412 ubuntudesktop更新源(old)
1.报错:404 Not Found [IP: 91.189.88.151 80] 2. deb http://old-releases.ubuntu.com/ubuntu/ raring main ...
- rac_grid自检提示缺少cvuqdisk包
原创作品,出自 "深蓝的blog" 博客,欢迎转载.转载时请务必注明下面出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...
- lua知识点整理
1. lua全局环境和局部环境 local cf = loadstring(" local i=0 i=i+1 print(i) ") --从后面两个输出我们可以看出,生成的函数的 ...
- C++11新特性之一——Lambda表达式
C++11新特性总结可以参考:http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html#section_6.8 C++ ...
- 微信accesstoken回调
errcode=-1的时候,开发文档中说明是系统异常,至于具体原因不明 不过有一种原因是AppID以及AppSecret错误 其它可能原因还待发现
- poj_2352 Treap
题目大意 对于二维平面上的n个点,给出点的坐标.定义一个点A覆盖的点的个数为满足以下条件的点B的个数:点B的x <= 点A的x坐标,点B的y坐标 <= 点A的y坐标. 给出N个点的 ...
- Change Base
Given an integer m in base B (2 ≤ B ≤ 10) (m contains no more than 1000 digits), find the value of t ...
- CentOS下PostgreSQL的安装与配置
一.CentOS下PostgreSQL的yum安装: #安装yum源,默认源存在对版本的支持不好,下载不到等等问题. yum install http://yum.postgresql.org/9.5 ...
- Android中集成QQ登陆和QQ好友分享及QQ空间分享
extends : http://blog.csdn.net/arjinmc/article/details/38439957 相关官方文档及下载地址: 如果只用分享和登陆,用lite包就可以,体积小 ...
- xpath定位方法小结(转载)
1.实例化一个浏览器WebDriver driver = new FirefoxDriver(); 2.driver.get() get传参数到浏览器中 3.常用定位方法webelement XX=d ...