1114 Family Property(并查集)

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805356599820288

题目比较麻烦,因为限时200ms,所以要用散列。

 1 #include<iostream>
 2 #include<vector>
 3 #include<unordered_map>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 struct Node {
 8     int id,num;
 9     double AVGS = 0,AVGA = 0;
10     bool flag = false;
11 };
12
13 int father[10000];
14 void init() {
15     for(int i = 0; i < 10000; ++i)
16         father[i] = i;
17 }
18
19 int findfather(int a) {
20     int b = a;
21     while(a != father[a])
22         a = father[a];
23     while(b != father[b]) {
24         int t = b;
25         b = father[b];
26         father[t] = a;
27     }
28     return a;
29 }
30
31 void Union(int a,int b) {
32     int fa = findfather(a);
33     int fb = findfather(b);
34     if(fa != fb)
35         father[max(fa,fb)] = min(fa,fb);
36 }
37 bool cmp(const Node& a ,const Node& b) {
38     if(a.flag != b.flag) return a.flag > b.flag;
39     if(a.AVGA /a.num != b.AVGA/b.num ) return a.AVGA /a.num > b.AVGA/b.num;
40     return a.id < b.id;
41 }
42
43 int main() {
44     init();
45     int n;
46     cin>>n;
47     unordered_map<int,vector<int> > mp1;//记录当前ID的信息
48     unordered_map<int,int> mp2; //记录所有ID
49     for(int i = 0; i < n; ++i) {
50         int id,fa,mo,k,child,estate,area;
51         scanf("%d%d%d%d",&id,&fa,&mo,&k);
52         mp2[id] = 1;
53         if(fa != -1) Union(id,fa),mp2[fa] = 1;
54         if(mo != -1) Union(id,mo),mp2[mo] = 1;
55         for(int j = 0; j < k; ++j) {
56             scanf("%d",&child);
57             Union(id,child);
58             mp2[child] = 1;
59         }
60         scanf("%d%d",&estate,&area);
61         mp1[id].push_back(estate);
62         mp1[id].push_back(area);
63     }
64     vector<Node> v(10000);
65     int cnt = 0;
66     for(auto it:mp2) {
67         int fa = findfather(it.first);
68         v[fa].id = fa;
69         v[fa].num++;
70         if(v[fa].num == 1) cnt++; //统计不同家庭的个数
71         if(mp1.count(it.first)) {
72             v[fa].AVGS += mp1[it.first][0];
73             v[fa].AVGA += mp1[it.first][1];
74         }
75         v[fa].flag = true;
76     }
77     sort(v.begin(),v.end(),cmp);
78     printf("%d\n",cnt);
79     for(int i = 0; i < cnt; ++i) {
80         printf("%04d %d %.3f %.3f\n",v[i].id,v[i].num,v[i].AVGS/v[i].num,v[i].AVGA/v[i].num);
81     }
82     return 0;
83 }

原文地址:https://www.cnblogs.com/keep23456/p/12544224.html

时间: 2024-10-15 22:55:50

1114 Family Property(并查集)的相关文章

PAT 1114 Family Property[并查集][难]

1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average

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

题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房产面积. 并查集,合并的时候编号小的作为父亲节点,最后父亲节点一样的即属于一个家庭,其它都是细节处理没啥好说了. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

(DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)

解题思路: 并查集 A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to which wecall a binding pair. If N > 2, then

POJ 2762 tarjan缩点+并查集+度数

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494   Accepted: 4100 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

[LA] 3644 - X-Plosives [并查集]

A secret service developed a new kind of explosive that attain its volatile property only when a specicassociation of products occurs. Each product is a mix of two di?erent simple compounds, to which wecall a binding pair. If N > 2, then mixing N di?

URAL(timus)1709 Penguin-Avia(并查集)

Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other Antarctic airlines, experiences financial difficulties because of the world's economic crisis. People of Antarctica economize on flights and use trains

UVALive - 3644 - X-Plosives (并查集!!)

X-Plosives A secret service developed a new kind of explosive that attain its volatile property only when a specific association of products occurs. Each product is a mix of two different simple compounds, to which we call a binding pair. If N>2, the

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a

并查集(个人模版)

并查集: 1 int find(int a) 2 { 3 int r=a; 4 while(f[r]!=r) 5 r=f[r]; 6 int i=a; 7 int j; 8 while(i!=r) 9 { 10 j=f[i]; 11 f[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 int merge(int a,int b) 17 { 18 int A,B; 19 A=find(a); 20 B=find(b); 21 if(A!=B) 22 { 23 f[B