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 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 (≤1000). Then Nlines 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≤k≤5) 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  当前人拥有几套 当前人拥有总面积。如果父母去世了,则用-1表示。输出要求按平均房产套数递减,如果相同,那么按ID递增排列。

//确实是使用并查集。

//遇到问题:遇到像8888这种单户的,该怎么处理,我写的里边似乎处理不了。

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include<cstdio>
using namespace std;
struct Peo{
    int father,estate,area;
    Peo(){father=-1;}
}peo[10000];
map<int,int> mp;
struct Far{
    int id,mem;
    double avge,avga;
};
int findF(int a){
    if(peo[a].father==-1)return a;
    int k=peo[a].father;
    while(peo[k].father!=-1){
        peo[a].father=peo[k].father;
        a=k;
        k=peo[a].father;
        //cout<<"wwww";
    }
    //cout<<"fff";
    return k;
}
void unionF(int a,int b){
    int fa=findF(a);
    int fb=findF(b);
//    if(fa>fb)peo[fa].father=fb;
//    else peo[fb].father=fa;//这里出现了问题啊,得判断是否等于,等于的话,啥也不用了。
    if(fa>fb)peo[fa].father=fb;
    else if(fa<fb) peo[fb].father=fa;
    //cout<<"uuuu";
}

bool cmp(Far & a,Far & b){
    return a.avge!=b.avge?a.id<b.id:a.avge>b.avge;
}
int main() {
    int n;
    cin>>n;
    //fill(father,father+10000,-1);
    int id,fa,mo,k,child;
    for(int i=0;i<n;i++){
        cin>>id>>fa>>mo>>k;
        //先将当前的人和父亲母亲合并
        if(fa!=-1)
            unionF(id,fa);
        if(mo!=-1)
            unionF(id,mo);
        for(int j=0;j<k;j++){
            cin>>child;
            unionF(id,child);
        }
       // peo[id].father=id;
        cin>>peo[id].estate>>peo[id].area;
    }

    vector<int> vt[10000];
    for(int i=0;i<10000;i++){
        if(peo[i].father!=-1){
            //cout<<"hh";
            mp[peo[i].father]++;
            //怎么记录每个簇里有谁呢?
            vt[peo[i].father].push_back(i);
        }
    }

    //最终还得sort一下。
    cout<<mp.size()<<‘\n‘;
    vector<Far> far;
    for(auto it=mp.begin();it!=mp.end();it++){
        int id=it->first;
        int mem=vt[id].size();
        int tote,tota;
        tote=peo[id].estate;
        tota=peo[id].area;
        for(int i=0;i<vt[id].size();i++){
            tote+=peo[vt[id][i]].estate;
            tota+=peo[vt[id][i]].area;
        }
        far.push_back(Far{id,mem,tote*1.0/mem,tota*1.0/mem});
    }
    sort(far.begin(),far.end(),cmp);
    for(int i=0;i<far.size();i++){
        printf("%04d %d %.3f %.3f\n",far[i].id,far[i].mem,far[i].avge,far[i].avga);
    }

    return 0;
}

//写成了这样,最终决定放弃!

代码转自:https://www.liuchuo.net/archives/2201

#include <cstdio>
#include <algorithm>
using namespace std;
struct DATA {
    int id, fid, mid, num, area;
    int cid[10];//最多有10个孩子。
}data[1005];
struct node {
    int id, people;
    double num, area;
    bool flag = false;
}ans[10000];
int father[10000];
bool visit[10000];
int find(int x) {
    while(x != father[x])//这样真的好简便,我为啥写那么复杂呢。
        x = father[x];
    return x;
}
void Union(int a, int b) {
    int faA = find(a);
    int faB = find(b);
    if(faA > faB)
        father[faA] = faB;
    else if(faA < faB)
        father[faB] = faA;
}
int cmp1(node a, node b) {
    if(a.area != b.area)
        return a.area > b.area;
    else
        return a.id < b.id;
}
int main() {
    int n, k, cnt = 0;
    scanf("%d", &n);
    for(int i = 0; i < 10000; i++)
        father[i] = i;//将父亲设置为自己。
    for(int i = 0; i < n; i++) {
        scanf("%d %d %d %d", &data[i].id, &data[i].fid, &data[i].mid, &k);
        //直接读进来,不使用中间变量。
        //并没有使用下标作为id啊。
        visit[data[i].id] = true;//标记出现过了。
        if(data[i].fid != -1) {
            visit[data[i].fid] = true;
            Union(data[i].fid, data[i].id);//将id作为并查集中的关键字合并。
        }
        if(data[i].mid != -1) {
            visit[data[i].mid] = true;
            Union(data[i].mid, data[i].id);
        }
        for(int j = 0; j < k; j++) {
            scanf("%d", &data[i].cid[j]);
            visit[data[i].cid[j]] = true;
            Union(data[i].cid[j], data[i].id);
        }
        scanf("%d %d", &data[i].num, &data[i].area);
    }
    for(int i = 0; i < n; i++) {
        int id = find(data[i].id);//找到当前人的父亲,
        ans[id].id = id;//现在这个ans中使用id作为下标索引了!
        ans[id].num += data[i].num;
        ans[id].area += data[i].area;
        ans[id].flag = true;
    }
    for(int i = 0; i < 10000; i++) {
        if(visit[i])//如果它出现过。
       i     ans[find(i)].people++;
        if(ans[i].flag)//标记有几簇人家。
            cnt++;
    }
    for(int i = 0; i < 10000; i++) {
        if(ans[i].flag) {
            ans[i].num = (double)(ans[i].num * 1.0 / ans[i].people);
            ans[i].area = (double)(ans[i].area * 1.0 / ans[i].people);
            //并没有多个num属性,都是用一个存的,一开始就设为double。
            //我居然还分开定义了。
        }
    }
    sort(ans, ans + 10000, cmp1);
    printf("%d\n", cnt);
    for(int i = 0; i < cnt; i++)
        printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num, ans[i].area);
    return 0;
}

1.将Father初始化为了自己,那么find函数就简单了,学习了

2.使用一个bool数组来标记出现过的点和没出现过的点,就解决了一家只有一口的情况。

3.在求ans向量的时候,仍使用了find,其实不用map的,find找到父亲都是可以用的。

4.因为data[i].id里存的是已经出现过的id,对那些没出现过的ans.flag肯定不会被标记为true的!

这是错误理解:ans里存储的所有的10000个人的情况,对于那些没有出现过的id,ans[i].flag也是true,只不过它所有的数据都是0,在经过排序之后,再通过簇进行控制输出,其他的不输出。

总之,学习了。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9583668.html

时间: 2024-11-04 09:35:35

PAT 1114 Family Property[并查集][难]的相关文章

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最

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

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

树上统计treecnt(dsu on tree 并查集 正难则反)

题目链接 \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\(L\sim R\)号点两两连通,最少需要选择的边的数量. 求\[\sum_{l=1}^n\sum_{r=l}^nTree[l,r]\] \(Solution\) 枚举每条边,计算它的贡献. 那么我们要判断有多少连续区间的点跨过这条边,并不好算,反过来去求在这条边的两侧分别有多少个连续区间. 那么显然有\(O(n^2)\)的做法,即对每条边DFS它的两侧,枚

LuoguP2700逐个击破【并查集/生成树/正难则反】By cellur925

题目传送门 题目大意:给你一棵树,求把其中k个点相互隔离(不连通)所需要的边权代价. 这题我开始是想要求出把k个点联通的最小代价的,但后来发现还是实现起来比较困难,题解里貌似也没有这种做法,于是就鸽了.但是大体的思考方向还是不直接去想把k个点隔离,而是把问题转化. 花费最小代价删边->花费最大代价建边.而建边的时候如果遇到一条两边都是敌人的边,我们显然是不需要建的,所以这其实我们需要维护敌人的网络,用并查集来维护. 首先我们标记敌人点,再把边从大到小排序.枚举所有的边,如果它两端点都是敌人,那肯

PAT甲级——1118 Birds in Forest (并查集)

此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. Yo

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls mad

PAT Advanced 1107 Social Clusters (30) [并查集]

题目 When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to

【并查集】并查集专题总结

先贴一个很好的专题总结: http://blog.csdn.net/chenguolinblog/article/details/9749935 贴一个我的题表: 其实主要就是带权并查集比较难.

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