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

#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. EVE-NG镜像模板资源占用统计

    转:http://www.emulatedlab.com/forum.php?mod=viewthread&tid=432&extra=page%3D1 EVE  Image fold ...

  2. hadoop学习笔记(四):HDFS文件权限,安全模式,以及整体注意点总结

    本文原创,转载注明作者和原文链接! 一:总结注意点: 到现在为止学习到的角色:三个NameNode.SecondaryNameNode.DataNode 1.存储的是每一个文件分割存储之后的元数据信息 ...

  3. shell脚本入门笔记

    转载:http://mp.weixin.qq.com/s?__biz=MzA3MTIxNzkyNg==&mid=204081791&idx=1&sn=27bb1d827e0f8 ...

  4. Web基础了解版08-JSTL-Core标签库

    JSTL JSP为我们提供了可以自定义标签库(Tag Library)的功能,用来替代代码脚本,Sun公司又定义了一套通用的标签库名为JSTL(JSP Standard Tag Library),里面 ...

  5. php将数据写入另外一个文件

    有时候,为了验证PHP的运行过程或者了解代码中的变量的使用情况,需要将变量写到另外一个文件中,方便我们查看.最近也是经常用到file_put_contents这个函数,因为只是试验用,暂时还不需要考虑 ...

  6. NTP服务安装及时间同步

    1.安装ntp服务命令 yum install -y ntp 2.常用NTP服务器地址: ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.al ...

  7. python pylab.plot() 方法使用

    Python 中用pylab模块, pylab.plot() 函数,绘制折线统计图 import pylab as pl x = [, , , ] y = [, , , ] ''' plot参数说明: ...

  8. 如何在centos里面安装php-posix

    今天在虚拟机上安装一个系统的插件,出现了You must have POSIX and PCNTL functions to use Video Process,搜了一下缺少插件,那接下来就是安装了. ...

  9. Android SDK Tools,Platform-tools,Build-tools分别有什么作用?

    SDK Tools:是下载sdk最基础的,由它再来下载Platform-tools,Build-tools platform-tools包含开发app的平台依赖的开发和调试工具,包括 adb.fast ...

  10. Windows上面搭建FlutterAndroid运行环境

    1.下载安装JDK https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.配置J ...