L2-007 家庭房产 (25分) 并查集
题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去。这个题有个坑编号可能为0000,会错数据3和5。
1 #include<bits/stdc++.h>
2 using namespace std;
3
4 struct node
5 {
6 int id,num,area,fa,ma;
7 int ch[10];
8 }p[100100];
9
10 struct fz
11 {
12 int id,all;
13 double num,area;
14 }q[100100];
15
16 int par[10100];
17 int s[10010];
18 int vis[10010];
19
20 bool cmp(fz x,fz y)
21 {
22 if(x.area==y.area) return x.id<y.id;
23 return x.area>y.area;
24 }
25
26 void init()
27 {
28 for(int i=0;i<10100;i++)
29 par[i]=i;
30 }
31
32 int find(int x)
33 {
34 if(x!=par[x]) par[x]=find(par[x]);
35 return par[x];
36 }
37
38 void unionn(int a,int b)
39 {
40 int fa=find(a),fb=find(b);
41 if(fa>fb) par[fa]=fb;
42 else par[fb]=fa;
43 }
44
45 int main()
46 {
47 init();
48 int n;
49 cin>>n;
50 for(int i=0;i<n;i++){
51 int k;
52 cin>>p[i].id;
53 s[p[i].id]=1;
54 cin>>p[i].fa>>p[i].ma>>k;
55 if(p[i].fa!=-1){
56 unionn(p[i].id,p[i].fa);
57 s[p[i].fa]=1;
58 }
59 if(p[i].ma!=-1){
60 unionn(p[i].id,p[i].ma);
61 s[p[i].ma]=1;
62 }
63 for(int j=0;j<k;j++){
64 cin>>p[i].ch[j];
65 if(p[i].ch[j]!=-1){
66 unionn(p[i].id,p[i].ch[j]);
67 s[p[i].ch[j]]=1;
68 }
69 }
70 cin>>p[i].num>>p[i].area;
71 }
72 for(int i=0;i<10010;i++)
73 q[i].id=-1;
74 int cnt=0;
75 for(int i=0;i<n;i++){
76 int x=find(p[i].id);
77 if(!vis[x]) cnt++;
78 vis[x]=1;
79 q[x].id=x;
80 q[x].num+=p[i].num;
81 q[x].area+=p[i].area;
82 }
83 for(int i=0;i<10010;i++)
84 if(s[i]) q[find(i)].all++;
85 for(int i=0;i<10010;i++){
86 if(q[i].id!=-1){
87 q[i].num=q[i].num/1.0/q[i].all;
88 if(q[i].all) q[i].area=q[i].area/1.0/q[i].all;
89 }
90 }
91 sort(q,q+10010,cmp);
92 printf("%d\n",cnt);
93 for(int i=0;i<cnt;i++){
94 printf("%04d %d %.3f %.3f\n",q[i].id,q[i].all,q[i].num,q[i].area);
95 }
96 return 0;
97 }
L2-007 家庭房产 (25分) 并查集的更多相关文章
- L2-013 红色警报 (25分) 并查集复杂度
代码: 1 /* 2 这道题也是简单并查集,并查集复杂度: 3 空间复杂度为O(N),建立一个集合的时间复杂度为O(1),N次合并M查找的时间复杂度为O(M Alpha(N)), 4 这里Alpha是 ...
- PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT甲题题解-1114. Family Property (25)-(并查集模板题)
题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...
- PAT-1107 Social Clusters (30 分) 并查集模板
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- PAT A 1118. Birds in Forest (25)【并查集】
并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性
题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
随机推荐
- 【渲染教程】使用3ds Max和ZBrush制作卡通风格的武器模型(上)
克里斯蒂娜·马丁(CristinaMartín)介绍了她的项目灵剑(Spirit Sword)的制作过程,并详细的展示了使用3ds Max和ZBrush制作模型,纹理绘画和最终展示的过程. 介绍 克里 ...
- 【Docker】1、 前后端分离项目 下载启动运行
人人开源前后端分离项目下载与配置 文章目录 人人开源前后端分离项目下载与配置 前后端分离框架介绍 后端项目下载与配置 1.renren-fast后台项目介绍 2.开发环境搭建 3.下载后端renren ...
- 【EXPDP/IMPDP】数据泵导入导出遇到目录没有权限问题
当执行数据泵导出的时候,报了如下错误: ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-39087: ...
- 【ORA】Specified value of MEMORY_TARGET is too small, needs to be at least 3072M解决办法
今天安装EM12C的时候遇到了一个报错: 修改好数据库中的参数大小后,重新启动报错 Specified value of MEMORY_TARGET is too small, needs to be ...
- 浏览器performance工具介绍及内存问题表现与监控内存的几种方式
一.GC的目的 为了实现内存空间的良性循环,performance提供多种监控方式监控内存 分析内存相关信息 当代码出现问题的时候及时定位到出现问题的代码块, 提高执行效率. preforcemanc ...
- LeetCode1022. 从根到叶的二进制数之和
题目 class Solution { public: int ans = 0; int sumRootToLeaf(TreeNode* root) { dfs(root,0); return ans ...
- 修改主机名后VCS的修改
转:https://blog.csdn.net/nauwzj/article/details/6733135 一. 单机改主机名需更改以下文件: /etc/hosts /etc/hostname.hm ...
- 查看Java的汇编指令
在IDEA配置VM options,打印汇编指令 -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly windows系统 下载插件 hsdis-amd6 ...
- Python Debug工具
最近在github上冒出了一个python的debug神器PySnooper,号称在debug时可以消灭print.那么该工具有哪些优点呢,如何使用该工具呢.本文就介绍该工具的优缺点和使用方式. 前言 ...
- 【9k字+】第二篇:进阶:掌握 Redis 的一些进阶操作(Linux环境)
九 Redis 常用配置文件详解 能够合理的查看,以及理解修改配置文件,能帮助我们更好的使用 Redis,下面按照 Redis 配置文件的顺序依次往下讲 1k 和 1kb,1m 和 1mb .1g 和 ...