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 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
随机推荐
- Go递归
1. 递归介绍 package main import ( "fmt" ) func test(n int) { if n > 2 { n-- test(n) } fmt.P ...
- 多租户SaaS的数据库设计模式
前言 在设计多租户SaaS应用程序时,您必须仔细选择最适合您应用程序需求的租户模型.租户模型确定每个租户的数据如何映射到存储.您选择的租户模式会影响应用程序设计和管理.以后切换到另一个模型有时代价昂贵 ...
- 2017年陕西省网络空间安全技术大赛——人民的名义-抓捕赵德汉2——Writeup
下载下来的文件是一个jar包,用die和binwalk检查,确实是一个纯正的jar包 java -jar FileName运行jar包,观察文件的外部特征,发现也是判断password的题目 用查 ...
- mybatis用mysql数据库自增主键,插入一条记录返回新增记录的自增主键ID
今天在敲代码的时候遇到一个问题,就是往数据库里插入一条记录后需要返回这个新增记录的ID(自增主键), 公司框架用的是mybatis的通用Mapper接口,里面的插入方法貌似是不能把新纪录的ID回填到对 ...
- django template 模板
九.Template模板 Template 模板是根据view传过来数据在html展示的功能,典型python 模板jinjia2库提供丰富的上下文展示func 创建template位置在项目下与ap ...
- jQuery的ajax方法的属性以及jsonp的随机数问题
原文地址[ http://www.cnblogs.com/know/archive/2011/10/09/2204005.html ] <!DOCTYPE html PUBLIC "- ...
- 7、Maven插件
什么是maven插件? maven 实际上是一类依赖插件执行的框架,每个任务实际上是由插件完成,Maven插件通常被用来 创建jar文件 创建war文件 编译代码文件 代码单元测试 创建工程文档 创建 ...
- Ubuntu 16 安装Nginx+Php+Mysql
嗯哼,结束外派,我胡汉三又回来了,回来第一件事,就是重新装服务器,搭环境,以前用的apache,最近改了nginx,来吧,从头开始 因为以前一直用apache,这次换一个nginx试试. 1.更新系统 ...
- 吴裕雄 python 神经网络——TensorFlow实现搭建基础神经网络
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def add_layer(inputs, in_ ...
- PHP高级面试题
在网上看到一些高级php 的面试题目.. 最近接连面试了几家公司,有些重要问题记录一下,督促自己学习提高,同时希望给朋友们一些帮助.内容很多,一点点完善,一步步学习..有些是面试被问,有些是招聘要求, ...