1. 程式人生 > >PAT 1114 Family Property[並查集][難]

PAT 1114 Family Property[並查集][難]

aps vga sin 簡便 關鍵字 flag vector mat list

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 (0k5) 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;
}
View Code

//寫成了這樣,最終決定放棄!

代碼轉自: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,在經過排序之後,再通過簇進行控制輸出,其他的不輸出。

總之,學習了。

PAT 1114 Family Property[並查集][難]