用并查集处理每个家庭的信息,注意标记~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
bool visit[maxn]={false};
int N;
struct node {
int id;
int child[];
double num;
double area;
}Node[maxn];
struct family {
int id;
double num;
double area;
int renshu;
}f[maxn];
int father[maxn];
void init () {
for (int i=;i<maxn;i++)
father[i]=i;
}
int findfather (int x) {
int a=x;
while (x!=father[x])
x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union (int a,int b) {
int faA=findfather (a);
int faB=findfather (b);
if (faA<faB) father[faB]=faA;
else father[faA]=faB;
};
bool cmp (family a,family b) {
if (a.area!=b.area) return a.area>b.area;
else return a.id<b.id;
}
int main () {
scanf ("%d",&N);
int id,fatherid,motherid,k;
double num,area;
init ();
for (int i=;i<N;i++) {
scanf ("%d %d %d %d",&id,&fatherid,&motherid,&k);
Node[i].id=id;
visit[id]=true;
if (fatherid!=-) {
visit[fatherid]=true;
Union (id,fatherid);
}
if (motherid!=-) {
visit[motherid]=true;
Union (id,motherid);
}
for (int j=;j<k;j++) {
scanf ("%d",&Node[i].child[j]);
visit[Node[i].child[j]]=true;
Union (id,Node[i].child[j]);
}
scanf ("%lf %lf",&Node[i].num,&Node[i].area);
}
for (int i=;i<N;i++) {
f[findfather(Node[i].id)].area+=Node[i].area;
f[findfather(Node[i].id)].num+=Node[i].num;
}
int ans=;
for (int i=;i<maxn;i++) {
if (visit[i]==true) {
f[findfather(i)].renshu++;
f[findfather(i)].id=findfather(i);
}
}
for (int i=;i<maxn;i++)
if (f[i].renshu!=) {
ans++;
f[i].area/=f[i].renshu;
f[i].num/=f[i].renshu;
}
sort (f,f+maxn,cmp);
printf ("%d\n",ans);
for (int i=;i<ans;i++) {
printf ("%04d %d %.3f %.3f\n",f[i].id,f[i].renshu,f[i].num,f[i].area);
}
return ;
}

PAT A1114 Family Property的更多相关文章

  1. PAT甲级——A1114 Family Property【25】

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

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

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

  3. A1114. Family Property

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

  4. PAT 1114 Family Property[并查集][难]

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

  5. PAT 1114 Family Property

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

  6. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  7. PAT_A1114#Family Property

    Source: PAT A1114 Family Property (25 分) Description: This time, you are supposed to help us collect ...

  8. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  9. PAT甲级1114. Family Property

    PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...

随机推荐

  1. CSS学习(4)常见样式声明

    1.文本 color 文字颜色 预设值:定义好的单词,如red blue 光学的三原色(红,绿,蓝),如 rgb(32,45,255) HEX十六进制,如#008CFF(#112233可以简写为#12 ...

  2. ARM64架构下登录mysql出错:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file:

    Linux下登录mysql出错:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared obj ...

  3. Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException

    实体类缺少无参构造方法,序列化对象需要无参构造方法 @NoArgsConstructor

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019 B. so easy (unordered_map+并查集)

    这题单用map过不了,太慢了,所以改用unordered_map,对于前面删除的点,把它的父亲改成,后面一位数的父亲,初始化的时候,map里是零,说明它的父亲就是它本身,最后输出答案的时候,输出每一位 ...

  5. 在虚拟机安装centos7

    因为工作需要,要经常用到虚拟机,以I前老让别人给装,可是老问人家也不好,自己整理一份比较适合小白用的教程,有点繁琐: 一.工具:VMware      CentOS7 的 ISO 文件 二.开始安装 ...

  6. 迭代器,for循环本质,生成器,常用内置方法,面向过程编程

    一.迭代器 1.迭代:更新换代(重复)的过程,每次的迭代都必须基于上一次的结果 迭代器:迭代取值的工具 2.迭代器给你提供了一种不依赖于索引取值的方式 3.可以迭代取值的对象:字符串,列表,元组,字典 ...

  7. java文件上传判断文件夹是否存在

    判断文件夹是否存在,不存在创建文件夹 File file =new File("E:/workspace/net/src/main/webapp/upload"); //如果文件夹 ...

  8. 【译】高级T-SQL进阶系列 (三)【中篇】:理解公共表表达式(CTEs)

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 一个简单的CTE例子 如前所述,CTE‘s提供给你了一个方法来更容易的书写复杂的代码以提高其可读性.假设你有列表 ...

  9. package.json中的script选项作用

    npm不仅可以用于模块管理,还可以用于执行脚本.package.json文件有一个scripts字段,可以用于指定脚本命令,供npm直接调用. 接下来做个简单测试: (1)在项目根目录下创建demo. ...

  10. 【STM32H7教程】第58章 STM32H7的硬件JPEG应用之图片解码显示

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第58章       STM32H7的硬件JPEG应用之图片解 ...