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 ...
随机推荐
- VRay for SketchUp渲染图黑原因及解决方案
很多人都遇到用Vray for SketchUp云渲染的时候,渲染出来的图片是全黑或者是局部是黑色, 这是什么原因呢? 1.有一种情况是,SketchUp的文件储存机制和其他的软件有些不同,它是把模型 ...
- LeetCode150 逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- Error: Could not request certificate: No route to host - connect(2)
[root@puppetclient ~]# puppet agent --server 192.168.127.137 --testInfo: Creating a new SSL key for ...
- Java高并发与多线程(二)-----线程的实现方式
今天,我们开始Java高并发与多线程的第二篇,线程的实现方式. 通常来讲,线程有三种基础实现方式,一种是继承Thread类,一种是实现Runnable接口,还有一种是实现Callable接口,当然,如 ...
- 【Oracle】更改oracle中的用户名称
修改oracle中的用户名,要需要修改oracle基表中的相关内容, 1.查看user#, select user#,name from user$ s where s.name='用户修改前的'; ...
- CTFHub - Web(五)
eval执行: 1.进入网页,显示源码, <?php if (isset($_REQUEST['cmd'])) { eval($_REQUEST["cmd"]); } els ...
- pod管理调度约束、与健康状态检查
pod的管理 [root@k8s-master ~]# vim pod.yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: ...
- SpringBoot WebSocket技术
最近看了Spring in Action,了解了一下WebSocket和Stomp协议相关技术,并搭建了一个项目.网上的例子不完整或者描述不清,所以自己记录一下以作备忘. 一.配置 Spring Bo ...
- oracle新增ID主键列,如何补全旧数据的ID值
1.创建SEQUENCE CREATE SEQUENCE MONKEY.TEST_ADD_IDCOL_ID CACHE 100; 2.新增表栏位 ALTER TABLE MONKEY.TEST_ADD ...
- 【Not BUG】微软Winform窗体中设计上的Bug,会导致程序编译失败?不,这不是BUG!
这不是BUG!!! 原文地址: https://www.cnblogs.com/thanks/p/14302011.html 现在让我们回忆一下原文 原文的操作步骤: 1. 新建一个Window Fo ...