[patl2-007]家庭房产
题目大意:求并查集中集合的个数,及每个集合的详细信息
解题关键:只要不进行unite,集合的根是不会变化的。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
struct node{
int id,cnt,area;
}pe[];
struct nod{
int id,num,cnt,area;
double avcnt,avar;
}pa[];
bool vis[];
int par[];
int find1(int x){
if(x==par[x]) return x;
else return par[x]=find1(par[x]);
}
bool cmp(nod x,nod y){
if(x.avar==y.avar) return x.id<y.id;
else return x.avar>y.avar;
}
void unite(int x,int y){
x=find1(x);
y=find1(y);
if(x==y) return;
else par[x]=y;//为什么这里改变一下会有变化???
}
int main(){
int n;
scanf("%d",&n);
for(int i=;i<;i++){
par[i]=i;
}
for(int i=;i<n;i++){
int t1,t2,t3;
scanf("%d%d%d",&t1,&t2,&t3);
vis[t1]=true;
if(t2!=-){
unite(t1, t2);
vis[t2]=true;
}
if(t3!=-){
unite(t1,t3);
vis[t3]=true;
}
int k;
scanf("%d",&k);
for(int j=;j<k;j++){
int temp;
scanf("%d",&temp);
unite(t1,temp);
vis[temp]=true;
}
pe[i].id=t1;
int t4,t5;
scanf("%d%d",&t4,&t5);
pe[i].cnt=t4;
pe[i].area=t5;
} for(int i=;i<n;i++){
int id=find1(pe[i].id);
//id=find1(id);
pa[id].cnt+=pe[i].cnt;
pa[id].area+=pe[i].area;
// pa[id].num++;
} for(int i=;i<=;i++){//这里是核心
if(vis[i]){
//int t=par[i]=find1(i);
int t=find1(i);
if(!pa[t].num){
pa[t].id=i;
}
pa[t].num++;
pa[t].avar=pa[t].area*1.0/pa[t].num;//这里其实是重复覆盖的
pa[t].avcnt= pa[t].cnt*1.0/pa[t].num;
}
} sort(pa,pa+,cmp);
int ans=;
for(int i=;i<;i++){
if(pa[i].num){
ans++;
}
//else break;
}
printf("%d\n",ans);
for(int i=;i<ans;i++){
if(pa[i].num){
printf("%04d %d %.3lf %.3lf\n",pa[i].id,pa[i].num,pa[i].avcnt,pa[i].avar);
}
} }
[patl2-007]家庭房产的更多相关文章
- 团体程序设计天梯赛-练习集L2-007. 家庭房产
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...
- L2-007. 家庭房产
L2-007. 家庭房产 题目链接:https://www.patest.cn/contests/gplt/L2-007 并查集 初学,看这题的时候完全没有什么好的想法,参考了@yinzm的blog用 ...
- L2-007 家庭房产 (25 分)
L2-007 家庭房产 (25 分) 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤),随后N行,每行按下 ...
- L2-007. 家庭房产(并查集)*
L2-007. 家庭房产 参考博客 #include <iostream> #include <cstdio> #include <cstring> #includ ...
- 天梯赛 L2-007. (并查集) 家庭房产
题目链接 题目描述 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式 ...
- PATL2-007. 家庭房产-并查集
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...
- pat 团体天梯赛 L2-007. 家庭房产
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产 ...
- L2-007 家庭房产 (25 分) (并查集)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805068539215872 题目: 给定每个人的家庭成员和其自己名 ...
- PAT L2-007 家庭房产
https://pintia.cn/problem-sets/994805046380707840/problems/994805068539215872 给定每个人的家庭成员和其自己名下的房产,请你 ...
- pat 团体赛练习题集 L2-007. 家庭房产
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式给出一个人的房产: ...
随机推荐
- 安装SQL 2008失败 (win7 旗舰版 32位)
本机系统 win7 32位 旗舰版 机器已经有sql 2005了,2008 不能安装成功,而且无任何错误提示. 那么通过windows install clean up (下载 windows ins ...
- Spring boot临时文件目录报错
基本的错误信息如下: 2018-03-05 at 15:12:03 CST ERROR org.apache.juli.logging.DirectJDKLog 181 log - Servlet.s ...
- vue配置stylus
首先Vue-init webapck 随便一个name vue项目创建好之后 cnpm i (利用淘宝镜像) cnpm i stylus stylus-loader -D 进行安装和配置 styl常 ...
- hadoop2.5.2 安装与部署
主从机构 主:jobtracker 从:tasktracker 四个阶段 1. split 2. Mapper: key-value(对象) 3. shuffle a) 分区(partition,H ...
- Jupyterhub Error 503: Proxy Target Missing
Jupyterhub Error 503: Proxy Target Missing 请求太频繁
- Python环境的搭建
Window 平台安装 Python: 以下为在 Window 平台上安装 Python 的简单步骤: 打开WEB浏览器访问http://www.python.org/download/ 在下载列表中 ...
- bzoj 1016 [JSOI2008]最小生成树计数——matrix tree(相同权值的边为阶段缩点)(码力)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1016 就是缩点,每次相同权值的边构成的联通块求一下matrix tree.注意gauss里的 ...
- Linux系统下如何去掉文件的@属性
前几天从别处copy了一个memcached.so文件,发现运行不了,用ls -l 看了一下发现memcached.so多了一个@属性,如何去掉这个@属性呢? 第一步:ls -laeO@ 第二步:xa ...
- DIDAO.Common --- 项目中的常用类及其中函数
常用函数: CommonHelper.cs using System; using System.Collections.Generic; using System.IO; using System. ...
- Web中的安全性问题
根据2010年OWASP发布的Web应用程序安全风险主要是SQL注入攻击.跨网站脚本.伪造客户端请求.Cookie盗取,传输层保护不足. 1 SQL注入攻击 随着B/S框架结构在系统开发中的广泛应用 ...