L2-007 家庭房产 (25 分) (并查集经典题型)

传送门

代码及注释:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

stringstream ss;
typedef long long ll;
map mp;

const int N = 10010;

int n;
int p[N]; // 父节点
bool st[N], st2[N]; // st记录每个成员是否存在, st2在最后记录人数时记录家庭编号

struct Note{
    int id, num; // id为编号, num为成员数量
    double sum, area; // sun为房产套数, area为房产面积
};

bool cmp(Note a, Note b)
{
    // 按题目要求排序
    if(a.area != b.area) return a.area>b.area;
    return a.idy)
        {
            p[x] = y;
        }else p[y] = x;
    }
}
int main()
{
    cin>>n;
    for(int i = 0; i>a[i].id>>p1>>p2;
        st[a[i].id] = 1;
        if(p1 != -1) // 若父母存在则存入家庭集合
        {
            make(a[i].id, p1);
            st[p1] = 1; // 更新状态
        }
        if(p2 != -1)
        {
            make(a[i].id, p2);
            st[p2] = 1;
        }
        int k;
        cin>>k;
        while(k -- )
        {
            int son;
            cin>>son;
            if(son != -1)
            {
                // 若孩子存在则存入家庭集合
                make(son, a[i].id);
                st[son] = 1;
            }
        }
        cin>>a[i].sum>>a[i].area;
    }

    Note r[N]; // 统计合并家庭信息
    for(int i = 0; i

你可能感兴趣的:(天梯赛训练,数据结构,算法,c++)