PAT A1114 Family Property
用并查集处理每个家庭的信息,注意标记~
#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的更多相关文章
- PAT甲级——A1114 Family Property【25】
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- 【刷题-PAT】A1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- A1114. Family Property
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- PAT 1114 Family Property[并查集][难]
1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...
- PAT 1114 Family Property
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT_A1114#Family Property
Source: PAT A1114 Family Property (25 分) Description: This time, you are supposed to help us collect ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
随机推荐
- python 第三方库安装
1.首先安装pip 2.在cmd中找到pip的安装路径,(一般在python的scripts文件中) 3.pip install 第三方库名称
- Java环境配置与编译运行详解
这篇文章主要为大家详细介绍了Java环境配置与编译运行的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 一.开篇 通过对之前Java之路的了解之后,相信初学者们都对Java有了一个比较深印 ...
- 安装rpm包时提示错误:依赖检测失败的解决方法
安装rpm包时提示错误:依赖检测失败 解决方法: 命令末尾加上--nodeps --force
- spring 基于XML的申明式AspectJ通知的执行顺序
spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...
- python 在linux上面安装beautifulsoup4(bs4) No module named 'bs4'
续费了我的服务器 重做系统成了Linux服务器 然后想把Windown上的Python脚本放上去运行 但是出现了 No module named 'bs4' 的问题 pip install bs4 试 ...
- Excel数据可视化方法
目录: Excel图表基础: 1.选择要为其创建图表的数据,如: 2.单击“插入”菜单中的“推荐的图表”(也可点击右下角的下拉箭头),点击后选择所有图表即可查看所有的图标类型 3.选择所要的图表,单击 ...
- 解决ifarme在ios下无法使用
在第一层的config 添加 <access origin="*" /><allow-navigation href="*" />< ...
- 基于springboot实现轮询线程自动执行任务
本文使用: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时 ...
- netty笔记-:EpollEventLoopGroup:Caused by: java.lang.ExceptionInInitializerError:Caused by: java.lang.IllegalStateException: Only supported on Linux
今天在翻看netty的源码的时候发现netty对EventLoopGroup的实现有不止常用的NIOEventLoopGroup ,一共有以下几种. EpollEventLoopGroup NioEv ...
- dp饭卡
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...