题目链接

题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去。这个题有个坑编号可能为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. 科来网络通讯协议图2019版(OSI七层模型)

    来源:http://www.colasoft.com.cn/download/protocols_map.php 自己把它转成了图片,好做查看:https://www.lanzous.com/ib5h ...

  2. Java虚拟机常用的性能监控工具

    基础故障处理工具 jps: 虚拟机进程状况工具 功能:来处正在运行的虚拟机进程,并显示虚拟机执行主类名称,以及本地虚拟机唯一ID. 它是使用频率最高的命令行工具,因为其他JDK工具大多需要输入他查询到 ...

  3. Java并发编程实战(5)- 线程生命周期

    在这篇文章中,我们来聊一下线程的生命周期. 目录 概述 操作系统中的线程生命周期 Java中的线程生命周期 Java线程状态转换 运行状态和阻塞状态之间的转换 运行状态和无时限等待状态的切换 运行状态 ...

  4. python学习笔记 | PyCharm创建文件时自动添加头文件

    File Settings Editor File and Code Templates Python Script 然后在右边的框中写入信息就可以啦: # -*- coding: utf-8 -*- ...

  5. 【Linux】扩大swap分区

    今天安装oracle的时候,提示我swap分区过小.需要最少3g以上 但是安装系统了,想要扩大swap分区怎么办呢 下面来介绍如何扩大swap分区 按步骤介绍 Red Hat linux 如何增加sw ...

  6. kubectl工具管理应用

    kubectl工具管理应用 创建一个pod [root@k8s-master ~]# kubectl run nginx --replicas=3 --labels="app=nginx-e ...

  7. UNDO表空间切换步骤

    1.新建UNDO表空间 create undo tablespace UNDOTBS2 datafile '/data01/testdb/undotbs01.dbf' size 1G; alter d ...

  8. 修改conda和pip源

    修改conda源为中科大源 Windows修改C:\Users\user(user替换为当前登陆系统的用户)目录下的.condarc文件 Linux修改家目录下的.condarc文件 channels ...

  9. Python爬虫:数据分析小能手:JSON库的用法

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 给大家推荐一个Python交流的q裙,大家在学习遇到了什么问题都可以进群一起交流,大家 ...

  10. 深度学习论文翻译解析(十七):MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

    论文标题:MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications 论文作者:Andrew ...