HDU3635(并查集)

该博客探讨了HDU3635问题,涉及到并查集数据结构的应用。内容包括处理n个龙珠和q个操作,操作类型为合并龙珠城市和查询龙珠状态,具体涉及龙珠所在城市、城市内龙珠总数及龙珠移动次数的查询。
摘要由CSDN通过智能技术生成

题目大意:n个龙珠,q个操作。操作T是将第Ath所在的城市里的所有龙珠移向第Bth龙珠所在的城市;操作Q是询问第Ath龙珠所在的城市、所在城市的龙珠总数,以及第Ath龙珠被移动的次数。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
const int maxn = 11111;
struct node
{
    int fa, step, sum;
}p[maxn];
int n, m;
void init()
{
    for(int i=0; i<=n; i++)
    {
        p[i].fa = i;
        p[i].sum = 1;
        p[i].step = 0;
    }
}
int find_fa(int x)
{
    if(x == p[x].fa)
        return x;
    int tot = p[x].fa;
    p[x].fa = find_fa(p[x].fa);
    p[x].step += p[tot].step;
    return p[x].fa;
}
void un(int x, int y)
{
    int fx = find_fa(x);
    int fy = find_fa(y);
    if(fx != fy)
    {
        p[fx].fa = fy;
        p[fx].step++;
        p[fy].sum += p[fx].sum;
        p[fx].sum = 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值