PAT甲级——A1114 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 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 Child1⋯Childk Mestate 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; Childi's are the ID
's of his/her children; Mestate 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
AVGsets AVGarea
where ID
is the smallest ID in the family; M
is the total number of family members; AVGsets is the average number of sets of their real estate; and AVGarea 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】的更多相关文章
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- PAT甲级——1114 Family Property (并查集)
此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- 【刷题-PAT】A1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT甲级 1121. Damn Single (25)
1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT甲级 1129. Recommendation System (25)
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
随机推荐
- jq-demo-点击选择(英雄联盟)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- thinkphp 上传安全
网站的上传功能也是一个非常容易被攻击的入口,所以对上传功能的安全检查是尤其必要的. 大理石平台支架 系统提供的上传类Think\Upload提供了安全方面的支持,包括对文件后缀.文件类型.文件大小以及 ...
- NX二次开发-UFUN获取图层的状态UF_LAYER_ask_status
NX11+VS2013 #include <uf.h> #include <uf_ui.h> #include <uf_layer.h> UF_initialize ...
- string替换所有指定字符串(C++)【转载】
转载自https://blog.csdn.net/a_222850215/article/details/79985504 C++的string提供了replace方法来实现字符串的替换,但是对于将字 ...
- C#利用栈实现字符串运算解析
附上参考文章链接:https://blog.csdn.net/qq_34831781/article/details/80104219 本人整合修复一些bug后的代码 using System; us ...
- /lib/libmysqlcppconn.so: undefined reference to `mysql_stmt_execute@libmysqlclient_18' 解决方法
sudo apt-get install libmysqlcppconn-dev
- PAT_A1086#Tree Traversals Again
Source: PAT A1086 Tree Traversals Again (25 分) Description: An inorder binary tree traversal can be ...
- Harbor任意管理员注册漏洞复现CVE-2019-16097
注册时抓包 添加poc "has_admin_role":true 管理员权限 POC POST /api/users HTTP/1.1 Host: 127.0.0.1 Conte ...
- Metasploit 模块和位置
Metasploit Framework由许多的模块组成的. 一.Exploits(漏洞模块) 定义为使用“有效载荷(payloads)”的模块 没有“有效载荷”的攻击是辅助模块 二.Payloads ...
- 802.11ac wave2的前世今生
2015年下半年,高通.博通.RTL等芯片厂商相继发布了满足802.11ac wave2要求的芯片,WLAN及终端厂商也迅速跟进推出相应的产品和终端.802.11ac wave2在多方推动下于2015 ...