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 area and number of sets of their real estate.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤). Then N lines follow, each gives the infomation of a person who owns estate in the format:

ID Father Mother k Child​1​​⋯Child​k​​ M​estate​​ Area

where ID is a unique 4-digit identification number for each person; Father and Mother are the ID's of this person's parents (if a parent has passed away, -1 will be given instead); k (0) is the number of children of this person; Child​i​​'s are the ID's of his/her children; M​estate​​ is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.

Output Specification:

For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:

ID M AVG​sets​​ AVG​area​​

where ID is the smallest ID in the family; M is the total number of family members; AVG​sets​​ is the average number of sets of their real estate; and AVG​area​​ is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID's if there is a tie.

Sample Input:

10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100

Sample Output:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

两个代码,仅供参考:

 #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
struct Node
{
int ID, nums = ;
double ss = 0.0, aa = 0.0;
};
int n;
double num[] = { 0.0 }, area[] = { 0.0 };//房子数量//房子面积
int mark[];//标记属于哪个家族
vector<set<int>>sets();//记录每个家庭有多少人
vector<Node*>res;
void combin(int my, int other)//合并家族
{
for (auto ptr = sets[other].begin(); ptr != sets[other].end(); ++ptr)//将其他成员进行同化
{
mark[*ptr] = my;
sets[my].insert(*ptr);
}
sets[other].clear();//删除其他成员的标记
}
int main()
{
cin >> n;
fill(mark, mark + , -);
for (int i = ; i < n; ++i)
{
int my, parent, k, child, nn, aa;
cin >> my;
if (mark[my] == -)//还未标记是哪个家族
{
mark[my] = my;//就以自己为标记
sets[mark[my]].insert(my);
}
for (int i = ; i < ; ++i)//添加父母
{
cin >> parent;
if (parent == -)
continue;
if (mark[parent] == -)//家人未标记
{
mark[parent] = mark[my];
sets[mark[my]].insert(parent);
}
else if (mark[parent] != - && mark[parent] != mark[my])//家人与自己标记不同
combin(mark[my], mark[parent]);//将家人同化为自己标记
}
cin >> k;
while (k--)
{
cin >> child;
if (mark[child] == -)//孩子未标记
{
mark[child] = mark[my];
sets[mark[my]].insert(child);
}
else if (mark[child] != - && mark[child] != mark[my])//孩子与自己标记不同
combin(mark[my], mark[child]);//将孩子同化为自己标记
}
cin >> nn >> aa;
num[my] = nn;
area[my] = aa;
}
for (int i = ; i < sets.size(); ++i)
{
if (sets[i].empty())
continue;
Node* temp = new Node;
temp->ID = *sets[i].begin();
temp->nums = sets[i].size();
for (auto ptr = sets[i].begin(); ptr != sets[i].end(); ++ptr)
{
temp->aa += area[*ptr];
temp->ss += num[*ptr];
}
temp->ss /= temp->nums;
temp->aa /= temp->nums;
res.push_back(temp);
}
sort(res.begin(), res.end(), [](Node*a, Node*b) {
if (abs(a->aa - b->aa) < 0.0001)
return a->ID < b->ID;
else
return a->aa > b->aa; });
cout << res.size() << endl;
for (auto v : res)
printf("%04d %d %0.3f %0.3f\n", v->ID, v->nums, v->ss, v->aa);
return ;
}
 #include<bits/stdc++.h>
using namespace std;
struct Estate{//存储集合最小id、集合结点个数num、集合总sets、集合总area
int id,num=;
double sets=0.0,area=0.0;
};
bool cmp(const Estate&e1,const Estate&e2){//比较函数
return e1.area!=e2.area?e1.area>e2.area:e1.id<e2.id;
}
int father[];//并查集底层数组
int findFather(int x){//查找根节点
if(x==father[x])
return x;
int temp=findFather(father[x]);
father[x]=temp;
return temp;
}
void unionSet(int a,int b){//合并两个集合
int ua=findFather(a),ub=findFather(b);
if(ua!=ub)
father[max(ua,ub)]=min(ua,ub);//以小的id作为根节点
}
unordered_map<int,pair<double,double>>idEstate;
unordered_map<int,Estate>familyEstate;
int main(){
int N;
scanf("%d",&N);
iota(father,father+,);
while(N--){//读取数据并合并相关集合
int id,f,m,k,a;
scanf("%d%d%d%d",&id,&f,&m,&k);
if(f!=-)
unionSet(id,f);
if(m!=-)
unionSet(id,m);
while(k--){
scanf("%d",&a);
unionSet(id,a);
}
scanf("%lf%lf",&idEstate[id].first,&idEstate[id].second);//记录id和对应的sets、area
}
for(int i=;i<;++i){//遍历并查集数组
int temp=findFather(i);
if(temp!=i)//根节点不等于本身
++familyEstate[temp].num;//递增根节点下集合的结点个数
if(idEstate.find(i)!=idEstate.cend()){//累加集合的总sets、总area
familyEstate[temp].sets+=idEstate[i].first;
familyEstate[temp].area+=idEstate[i].second;
}
}
vector<Estate>v;
for(auto i=familyEstate.begin();i!=familyEstate.end();++i){//将familyEstate中的值搬迁到vector中,并更新相关信息
(i->second).id=i->first;
++(i->second).num;//集合下结点个数没有统计根节点,所以要+1
(i->second).sets/=(i->second).num;
(i->second).area/=(i->second).num;
v.push_back(i->second);
}
sort(v.begin(),v.end(),cmp);//排序
printf("%d\n",v.size());
for(int i=;i<v.size();++i)
printf("%04d %d %.3f %.3f\n",v[i].id,v[i].num,v[i].sets,v[i].area);
return ;
}

PAT甲级——A1114 Family Property【25】的更多相关文章

  1. PAT甲级1114. Family Property

    PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...

  2. PAT甲级——1114 Family Property (并查集)

    此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...

  3. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  4. 【刷题-PAT】A1114 Family Property (25 分)

    1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...

  5. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  6. PAT甲级 1121. Damn Single (25)

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...

  7. PAT甲级 1126. Eulerian Path (25)

    1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...

  8. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  9. PAT甲级 1129. Recommendation System (25)

    1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

随机推荐

  1. 2 java程序入门

    1. 第一个java  class  { public static void main(String[] args) { System.out.println("Hello World!& ...

  2. SQL Server SQLBindCol

    说明 编辑 版本引入:ODBC 1.0 遵从标准:ISO 92 功能说明: SQLBindCol将应用程序的数据缓冲绑定到结果集的各列 函数原型: SQLRETURN SQLBindCol( SQLH ...

  3. Oracle分页:工作记录

    Oracle分页:传入值为List<String> . Mybatis XXXMapper.xml SELECT * FROM ( SELECT ROWNUM rn, t1.cardnum ...

  4. 牛客网 NOIP赛前集训营-普及组(第四场)C--部分和 (高维前缀和)

    传送门 解题思路 高维前缀和模板题.首先,求前缀和有两种方式,比如说对于求二维前缀和来说. 第一种 : for(int i=1;i<=n;i++) for(int j=1;j<=n;j++ ...

  5. AtCoder ARC061E Snuke's Subway Trip 最短路

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门  Portal  原题目描述在最下面.  \(n(1 ...

  6. python 列表 实现队列

    主要用到两个函数 list.pop() 返回并删除最后一个, 也可以 list.pop(n) list.insert(0, item)

  7. 10.RabbitMQ Fanout类型交换机

    Fanout类型交换机忽略Routing Key,它将消息传递到所有与它绑定的队列上.       Producer.java package com.test.fanout;   import co ...

  8. Aizu - ALDS1_13_A-8 Queens Problem-八皇后的路径输出

    The goal of Queens Problem is to put eight queens on a chess-board such that none of them threatens ...

  9. 2019-2020 ICPC, NERC, Northern Eurasia Finals

    A. Apprentice Learning Trajectory rdc乱编的做法 考虑贪心,每次会选择结束时间最早的. 设当前时间为 \(x\),那么可以区间有两类 a) \(l_i \leq x ...

  10. 【2018ACM/ICPC网络赛】沈阳赛区

    这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K  Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...