题目链接

https://www.patest.cn/contests/gplt/L2-007

思路

将一个家庭里的所有人都并进去

然后最后查找的时候 找到所有同一个家庭的人,计算出人数,人均房产套数,人均房产面积 而且 在ID 小于当前 ID 的时候 要更新

AC代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <ctype.h>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <climits>
  7. #include <ctime>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <deque>
  11. #include <vector>
  12. #include <queue>
  13. #include <string>
  14. #include <map>
  15. #include <stack>
  16. #include <set>
  17. #include <numeric>
  18. #include <sstream>
  19. #include <iomanip>
  20. #include <limits>
  21. using namespace std;
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. typedef pair <int, int> pii;
  25. typedef pair <ll, ll> pll;
  26. const double PI = 3.14159265358979323846264338327;
  27. const double E = 2.718281828459;
  28. const double eps = 1e-6;
  29. const int INF = 0x3f3f3f3f;
  30. const int maxn = 1e6 + 5;
  31. const int MOD = 1e9 + 7;
  32. int pre[10000];
  33. int find(int x)
  34. {
  35. while (pre[x] != x)
  36. x = pre[x];
  37. return x;
  38. }
  39. void join(int x, int y)
  40. {
  41. int fx = find(x), fy = find(y);
  42. if (x != fy)
  43. pre[fx] = fy;
  44. }
  45. struct Node
  46. {
  47. int id;
  48. int total;
  49. int tot;
  50. double ave, area;
  51. }temp, q[maxn];
  52. bool comp(Node x, Node y)
  53. {
  54. if (fabs(x.area - y.area) < eps)
  55. return x.id < y.id;
  56. return x.area > y.area;
  57. }
  58. int main()
  59. {
  60. for (int i = 0; i < 10000; i++)
  61. pre[i] = i;
  62. int n;
  63. cin >> n;
  64. map <int, Node> m;
  65. map <int, int> vis;
  66. for (int i = 0; i < n; i++)
  67. {
  68. int id, a, b, k;
  69. scanf("%d%d%d%d", &id, &a, &b, &k);
  70. vis[id] = 1;
  71. if (a != -1)
  72. {
  73. join(id, a);
  74. vis[a] = 1;
  75. }
  76. if (b != -1)
  77. {
  78. join(id, b);
  79. vis[b] = 1;
  80. }
  81. for (int j = 0; j < k; j++)
  82. {
  83. scanf("%d", &a);
  84. if (a != -1)
  85. {
  86. join(id, a);
  87. vis[a] = 1;
  88. }
  89. }
  90. scanf("%d%d", &a, &b);
  91. m[id].id = id;
  92. m[id].tot = a;
  93. m[id].area = b;
  94. }
  95. map <int, int>::iterator it, iter;
  96. int count = 0;
  97. for (it = vis.begin(); it != vis.end(); it++)
  98. {
  99. temp.id = 0;
  100. temp.tot = 0;
  101. temp.tot = 0;
  102. temp.ave = 0.0;
  103. temp.area = 0.0;
  104. if (it -> second == 1)
  105. {
  106. it -> second = 0;
  107. int flag = find(it -> first);
  108. temp.id = it -> first;
  109. temp.tot += m[it -> first].tot;
  110. temp.area += m[it -> first].area;
  111. temp.total = 1;
  112. for (iter = it, iter++; iter != vis.end(); iter++)
  113. {
  114. if (iter -> second == 1 && flag == find(iter -> first))
  115. {
  116. iter -> second = 0;
  117. if (m[iter -> first].id == iter -> first)
  118. {
  119. if (iter -> first < temp.id)
  120. temp.id = iter -> first;
  121. temp.tot += m[iter -> first].tot;
  122. temp.area += m[iter -> first].area;
  123. }
  124. temp.total ++;
  125. }
  126. }
  127. temp.ave = temp.tot * 1.0 / temp.total;
  128. temp.area /= temp.total;
  129. q[count++] = temp;
  130. }
  131. }
  132. sort (q, q + count, comp);
  133. printf("%d\n", count);
  134. for (int i = 0; i < count; i++)
  135. {
  136. printf("%04d %d %.3lf %.3lf\n", q[i].id, q[i].total, q[i].ave, q[i].area);
  137. }
  138. }

PAT 天梯赛 L2-007. 家庭房产 【并查集】的更多相关文章

  1. PAT 天梯赛 L2-010. 排座位 【并查集】

    题目链接 https://www.patest.cn/contests/gplt/L2-010 思路 因为 题意中 朋友的朋友 就是朋友 那么 朋友的关系 用 并查集 保存 但是 敌对关系 只有直接的 ...

  2. PATL2-007. 家庭房产-并查集

    L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...

  3. 天梯赛决赛 L2-1.红色警报 并查集

    L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...

  4. 天梯赛 L2-010 排座位 (并查集)

    布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位.无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编写程序告诉主人他们是否能被安排同席. 输入格式: ...

  5. L2-007. 家庭房产(并查集)

    #include <cstdio> #include <set> #include <vector> #include <algorithm> usin ...

  6. PAT天梯赛L2-007 家庭房产

    题目链接:点击打开链接 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列 ...

  7. PAT 天梯赛 L2-007 家庭房产

    建图+DFS 题目链接:https://www.patest.cn/contests/gplt/L2-007 题解 在热身赛的时候没有做出来,用的并查集的思想,但是敲残了,最后也没整出来.赛后听到别人 ...

  8. PAT天梯赛 L1-049 天梯赛座位分配

    题目链接:点击打开链接 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] ...

  9. PAT天梯赛L3-007 天梯地图

    题目链接:点击打开链接 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线:一条是最短距离的路线.题目保证对任意的查询请求,地图上都至 ...

  10. PAT天梯赛练习题——L3-007. 天梯地图(多边权SPFA)

    L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...

随机推荐

  1. angularjs与server交互

    真正的应用须要和真实的server进行交互,移动应用和新兴的Chrome桌面应用可能是个例外,可是对于此外的全部应用来说,不管你是想把数据持久化到云端.还是须要与其它用户进行实时交互.都须要让应用与s ...

  2. spring集成PHPRPC及使用

    PHPRPC,它的商业版本是Hprose.这里仅记录其使用方法.其它相关内容可自行搜索. 对于开源的东西,建议大家看看其源码. 1.需要引入的jar包:phprpc_spring.jar,http:/ ...

  3. c语言字符串赋值

    char *p="asdf";能运行 定义一个字符指针,并用它指向常量字符串"asdf"的首地址 char *p;p="asdf"; 能运行 ...

  4. iOS时间间隔判断

    如何计算两个NSDate之间的时间间隔呢? timeIntervalSinceDate: Returns the interval between the receiver and another g ...

  5. 介绍C#结构体与类区别

    1. 结构体与类定义方式 结构体定义使用struct类定义使用class 结构体: struct testDemo{ int num; void action(){ } } 类: class test ...

  6. 转:HTTP协议--- multipart/form-data请求分析

    转自:http://blog.csdn.net/five3/article/details/7181521 首先来了解什么是multipart/form-data请求: 根据http/1.1 rfc ...

  7. sqlserver修改表主键自增

    alter table tname add id int  identity(1,1)

  8. 【LeetCode-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

    [118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...

  9. 如何提高Linux下块设备IO的整体性能?

    编辑手记:本文主要讲解Linux IO调度层的三种模式:cfp.deadline和noop,并给出各自的优化和适用场景建议. 作者简介: 邹立巍 Linux系统技术专家.目前在腾讯SNG社交网络运营部 ...

  10. PHP里的socket_recv方法解释

    以前一直经为PHP里没有低级的socket帧接收函数,看来是没看仔细,不过那些说明也太少了,(更令人气的里在英文版说明里的例子下有一句话:这个程序不能运行,因为没用listen函数,但在中文版里却没了 ...