题目链接

题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去。这个题有个坑编号可能为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分) 并查集的更多相关文章

  1. L2-013 红色警报 (25分) 并查集复杂度

    代码: 1 /* 2 这道题也是简单并查集,并查集复杂度: 3 空间复杂度为O(N),建立一个集合的时间复杂度为O(1),N次合并M查找的时间复杂度为O(M Alpha(N)), 4 这里Alpha是 ...

  2. 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 ...

  3. PAT甲题题解-1114. Family Property (25)-(并查集模板题)

    题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...

  4. PAT-1107 Social Clusters (30 分) 并查集模板

    1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...

  5. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  6. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  7. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  8. PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性

    题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...

  9. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

随机推荐

  1. sa-token 之权限验证

    权限验证 核心思想 所谓权限验证,验证的核心就是当前账号是否拥有一个权限码 有:就让你通过.没有:那么禁止访问 再往底了说,就是每个账号都会拥有一个权限码集合,我来验证这个集合中是否包括我需要检测的那 ...

  2. Assuming that agent dropped connection because of access permission

    Assuming that agent dropped connection because of access permission

  3. ctfhub技能树—文件上传—00截断

    什么是00截断 相关教程:http://www.admintony.com/%E5%85%B3%E4%BA%8E%E4%B8%8A%E4%BC%A0%E4%B8%AD%E7%9A%8400%E6%88 ...

  4. ctfshow—web—web7

    打开靶机 发现是SQL注入,盲注 过滤了空格符,可以用/**/绕过,抓包 直接上脚本 import requestss=requests.session()url='https://46a0f98e- ...

  5. 浅谈JavaScript代码性能优化

    可以通过https://jsbench.me/测试网站完成性能测试. 一.慎用全局变量 1.全局变量定义在全局执行上下文,是所有作用域链的顶端,在局部作用域中没找到的变量都会到全局变量中去查找,所以说 ...

  6. uni-app开发经验分享十八:对接第三方h5

    1.uni-app中对接第三方为了防止跳出app使用了webview <template> <view> <web-view :src="url" @ ...

  7. ajax异步实现文件分片上传

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JSAAS BPM快速开发平台-企业管理软件,专属你的企业管家

    前言: 2020年,企业该如何去选择合适的信息化规划管理软件,基于目前社会软件杂乱无章,选择企业业务贴近的管理软件,甚是困难,市场上一些大品牌公司的产品,定位高,价格高,扩展难,等等一系列的问题,对于 ...

  9. IDEA_2019.1版本中Protobuf的使用

    一.Protobuf是什么 Protobuf 是 Google 发布的开源项目,全称 Google Protocol(/'prəʊtəkɒl/,协议,草案) Buffers,是一种轻便高效的结构化数据 ...

  10. missing required library sqlite.dll最终解决办法

    missing required library sqlite.dll最终解决办法 昨天电脑还是好的,今天早晨打开navicat连接Mysql无缘无故报错"missing required ...