题目链接

题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去。这个题有个坑编号可能为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. Vue.nextTick()的使用

    什么是Vue.nextTick()?? 定义:在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 所以就衍生出了这个获取更新后的DOM的Vue方法.所 ...

  2. 详细的String源码解析

    我们常常把String类型的字符串作为HashMap的key,为什么要这样做呢? 因为String是不可变的,一旦初始化就不再改变了,如果被修改将会是一个新对象. @Test public void ...

  3. 基于 MPI/OpenMP 混合编程的大规模多体(N-Body)问题仿真实验

    完整代码: #include <iostream> #include <ctime> #include <mpi.h> #include <omp.h> ...

  4. Java并发包源码学习系列:挂起与唤醒线程LockSupport工具类

    目录 LockSupport概述 park与unpark相关方法 中断演示 blocker的作用 测试无blocker 测试带blocker JDK提供的demo 总结 参考阅读 系列传送门: Jav ...

  5. ctfhub技能树—RCE—过滤cat

    打开靶机 查看页面信息 构造payload 127.0.0.1 || ls 题目提示过滤了cat,但我还是想试试 果然不行 网页访问没有结果,应该和上题一样被注释了,使用和同样的方法进行解题 利用命令 ...

  6. CTFshow-萌新赛逆向_签退

    查看题目信息 下载re3.pyc文件 使用uncompyle把re3.pyc反编译为re3.py uncompyle6 re3.pyc > re3.py 查看re3.py文件 # uncompy ...

  7. CTFshow-萌新赛杂项_签到

    查看网页信息 http://game.ctf.show/r2/ 把网页源码下载后发现有大片空白 使用winhex打开 把这些16进制数值复制到文件中 把20替换为0,09替换为1后 得到一串二进制数值 ...

  8. mysql查看修改参数

    1.查看参数 show variables like '%timeout%'; 2.修改参数 会话级别修改: set session innodb_lock_wait_timeout=50; 对当前会 ...

  9. centos7+宝塔+ssrpanel v3 魔改版 前后端配置教程

    一.服务端 1.安装宝塔 登录 SSH 后,直接安装宝塔. yum install -y wget && wget -O install.sh http://download.bt.c ...

  10. vs code配置vue自动格式化

     vs code配置vue自动格式化 我他妈的要被这个vs code的格式化逼疯了.我在网上看了很多的文章,不是太老就是不好使,遇到太多坑了.在这贴出自己的配置,虽然有多余的代码,虽然可能在未来的更新 ...