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

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 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

题目大意:根据给出的数据分类出各个家庭,然后输出家庭个数;接下来输出每个家庭的成员ID(只需输出家庭成员中最小的ID)、成员数量、平均房产数量、平均房产面积,输出顺序为平均房产面积降序、ID升序。

思路:用并查集S给家庭分类,为了方便找出最小的家庭成员ID,对并查集的操作稍作修改,集合元素初始化为对应的下标,若S[X]==X,则X为根,unionSet()函数将大根集合并到小根集合里;家庭成员计数时需要对已经访问过的ID进行标记,防止重复计数,用visit数组标记每个ID。

总结:其实我是比较害怕这类问题的,看起来简单,可是信息一多思考起来就容易混乱,需要很细心很耐心地分析,用不到什么算法知识却超级消耗时间~

 #include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct node1 {
int ID, fID, mID, k, Me, area;
vector <int> child;
};
struct node2 {
int sumM = , sumSets = , sumArea = ;
};
struct node3 {
int ID, M;
float AVGs, AVGa;
};
vector <int> S();
vector <bool> visit(, false);
void initialize();//对集合S进行初始化
bool cmp(node3 &a, node3 &b);//排序规则
int getNum(node1 &a);//获取家庭成员的数量
int find(int X);
void unionSet(int X, int Y);
int main()
{
int N;
scanf("%d", &N);
vector <node1> data(N);
unordered_map <int, node2> mp;
initialize();
for (int i = ; i < N; i++) {
node1 tmp;
scanf("%d%d%d%d", &tmp.ID, &tmp.fID, &tmp.mID, &tmp.k);
if (tmp.fID != -)
unionSet(tmp.ID, tmp.fID);
if (tmp.mID != -)
unionSet(tmp.ID, tmp.mID);
if (tmp.k != ) {
for (int j = ; j < tmp.k; j++) {
int cID;
scanf("%d", &cID);
tmp.child.push_back(cID);
unionSet(tmp.ID, cID);
}
}
scanf("%d%d", &tmp.Me, &tmp.area);
data[i] = tmp;
}
for (int i = ; i < N; i++) {
int ID = find(data[i].ID);
mp[ID].sumM += getNum(data[i]);
mp[ID].sumSets += data[i].Me;
mp[ID].sumArea += data[i].area;
}
vector <node3> ans;
for (auto it = mp.begin(); it != mp.end(); it++) {
int ID = it->first,
M = it->second.sumM;
float AVGs = it->second.sumSets*1.0 / M,
AVGa = it->second.sumArea*1.0 / M;
ans.push_back({ ID,M,AVGs,AVGa });
}
sort(ans.begin(), ans.end(), cmp);
printf("%d\n", ans.size());
for (int i = ; i < ans.size(); i++)
printf("%04d %d %.3f %.3f\n", ans[i].ID, ans[i].M, ans[i].AVGs, ans[i].AVGa);
return ;
}
int getNum(node1 &a) {
int n = ;
if (!visit[a.ID]) {
n++;
visit[a.ID] = true;
}
if ((a.fID != - )&& (!visit[a.fID])) {
n++;
visit[a.fID] = true;
}
if ((a.mID != -) && (!visit[a.mID])) {
n++;
visit[a.mID] = true;
}
if (a.k != ) {
for (int i = ; i < a.k; i++) {
if (!visit[a.child[i]]) {
n++;
visit[a.child[i]] = true;
}
}
}
return n;
}
void unionSet(int X, int Y) {
int rootX = find(X),
rootY = find(Y);
if (rootX > rootY)
S[rootX] = S[rootY];
else
S[rootY] = S[rootX];
}
int find(int X) {
if (S[X] == X) {
return X;
}
else {
return S[X] = find(S[X]);//递归地压缩路径
}
}
void initialize() {
for (int i = ; i < ; i++)
S[i] = i;//将集合元素初始化为对应的下标,每个集合的根节点就是当前家庭的最小ID
}
bool cmp(node3 &a, node3 &b) {
if (a.AVGa != b.AVGa)
return a.AVGa > b.AVGa;
else
return a.ID < b.ID;
}
 

PAT甲级——1114 Family Property (并查集)的更多相关文章

  1. PAT甲级1114. Family Property

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

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

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

  3. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

  4. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  5. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

  6. pat甲级1114

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

  7. PAT甲级——A1114 Family Property【25】

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  8. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  9. PAT l2-010 排座位 【并查集】

    L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...

随机推荐

  1. error:Flash Download failed-“Cortex-M3”,“Programming Algorithm”【转】

    本文转载自:http://www.yfrobot.com/thread-11763-1-1.html 最近安装了KEIL5,在使用KEIL5和JLIN实现在线调试功能时,一定会在Utilities选项 ...

  2. bzoj5213: [Zjoi2018]迷宫

    好题!话说省选的都开始构造了吗 由于有K的倍数的限制所以不妨取模,先建K个点表示0~K-1这些数,第i个点向[i*m,i*m+m]建边.不难发现这是合法的但不一定是最优的 考虑合并等价的点,首先从直观 ...

  3. TP框架入门基础

    ThinkPHP目录: ThinkPHP主目录文件夹: Conf文件夹: Library文件夹: Library=>Think文件夹:

  4. linux 网络编程 inet_pton & inet_ntop函数

    #include <arpa/inet.h> int inet_pton(int family,const char * strptr,void * addrptr); 返回:--成功, ...

  5. Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)

    Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m ...

  6. cm 安装

    为Cloudera Manager建立数据库:/usr/share/cmf/schema/scm_prepare_database.sh mysql -h[mysql数据库的主机名] -P63751 ...

  7. MySQL_杭州拱墅区、西湖区近9-11月销售过的产品_20161125

    需求:杭州拱墅区.西湖区近9-11月销售过的产品 这个SQL如果对数据表熟悉的话可以一步到位,为了看的更清楚,时间间隔在3个月产品数量也不是很多,采取先找明细再进行汇总. 一.第一种写法 分步骤的写S ...

  8. 为VSFTP用户指定登录后的目录.原创测试通过.

    VSFTP用户目录指定1修改VSFTP配置文件Vi /etc/vsftpd/vsftp.conf  #启动chroot列表chroot_list_enable=YES#指定列表位置chroot_lis ...

  9. jvm学习五: 方法执行过程

    方法执行过程:Java各个大版本更新提供的新特性(需要简单了解)

  10. 计算机网络HTTP、TCP/IP包

    参考: TCP-IP数据包结构详解 HTTP报文格式详解 Http协议报文格式 HTTP请求/响应报文结构 [Java知识]GET和POST请求的区别