带权并查集之旅(未完待续......)

本文解析了并查集在解决复杂数据结构问题中的应用,包括查找同类项、食物链关系判断、立方堆叠等典型场景,展示了并查集算法在处理连通性和分类问题上的高效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明一下:POJ的cin,cout很难用,有时候会TLE,解除同步也不行。推荐使用scanf,printf。

1、Find them, Catch them (POJ 1703)

方法一:带权并查集

#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 1e5 + 5;
int s[MAXN], d[MAXN]; //d 0 同派,1不同派别
void init(int n)//初始化
{
    for (int i = 1; i <= n; i++)
    {
        s[i] = i;
        d[i] = 0;
    }
}
int find_set(int x)//压缩状态
{
    if (x != s[x])
    {
        int t = s[x];
        s[x] = find_set(s[x]);
        d[x] = (d[x] + d[t]) % 2;//更新关系,动手画下图
    }
    return s[x];
}
void union_set(int x, int y)//合并
{
    int rx = find_set(x), ry = find_set(y);
    if (rx != ry)
    {
        s[rx] = ry;
        d[rx] = (d[y] + 1 - d[x]) % 2;//更新状态
    }
}
bool some_set(int x, int y)//检查是否入过并查集,没如果就是不确定
{
    return find_set(x) == find_set(y);
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n, m, x, y;
        char ch;
        scanf("%d%d",&n,&m);
        init(n);
        while (m--)
        {
            scanf(" %c %d%d",&ch,&x,&y);
            if (ch == 'D') //union
                union_set(x, y);
            else
            {
                bool tmp = some_set(x, y);
                if (tmp && (d[x] == d[y]))
                    printf("In the same gang.\n");
                else if (tmp && (d[x] != d[y]))
                    printf("In different gangs.\n");
                else
                    printf("Not sure yet.\n");
            }
        }
    }
    return 0;
}

方法二:种类并查集

#include <iostream>
using namespace std;
const int MAXN = (1e5 + 5) * 2;
int s[MAXN], high[MAXN]; //存根
void itin(int n)         //初始化
{
    for (int i = 1; i <= 2 * n; i++)
    {
        s[i] = i;
        high[i] = 1;
    }
}

int getroot(int x) //压缩状态
{
    if (x != s[x])
        s[x] = getroot(s[x]);
    return s[x];
}
void merge(int x, int y) //合并优化
{
    x = getroot(x);
    y = getroot(y);
    if (x == y)
        return;
    if (high[x] == high[y])
    {
        s[y] = x;
        high[x]++;
    }
    else
    {
        if (high[x] > high[y])
            s[y] = x;
        else
            s[x] = y;
    }
}
bool same(int x, int y) //判断函数
{
    x = getroot(x);
    y = getroot(y);
    return x == y;
}
int main()
{
    int t, n, m, x, y;
    char ch;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        itin(n);
        while (m--)
        {
            cin >> ch;
            cin >> x >> y;
            if (ch == 'A') //put
            {
                if (same(x, y) || same(x + n, y + n))
                    cout << "In the same gang." << endl;
                else if (same(x + n, y) || same(x, y + n))
                    cout << "In different gangs." << endl;
                else
                    cout << "Not sure yet." << endl;
            }
            else //merge
            {
                merge(x + n, y);
                merge(x, y + n);
            }
        }
    }
    return 0;
}

2、A Bug’s Life POJ 2492

跟上一题的更新关系是一样的。

#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 2010;
int s[MAXN], sex[MAXN];
bool flag;
void init(int n)
{
    flag = 0;
    for (int i = 1; i <= n; i++)
    {
        s[i] = i;
        sex[i] = 0;
    }
}
int find_set(int x)
{
    if (x != s[x])
    {
        int t = s[x];
        s[x] = find_set(s[x]);
        sex[x] = (sex[t] + sex[x]) % 2;
    }
    return s[x];
}
void union_set(int x, int y)
{
    int rootx = find_set(x), rooty = find_set(y);
    if (rootx == rooty)
    {
        if (sex[x] == sex[y])
            flag = 1;
    }
    else
    {
        s[rootx] = rooty;
        sex[rootx] = (sex[x] - sex[y] + 1) % 2;
    }
}
int main()
{
    //ios::sync_with_stdio(false);
    int t, m, n;
    int k = 0, x, y;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
        init(n);
        while (m--)
        {
            scanf("%d%d", &x, &y);
            if (flag)
                continue;
            union_set(x, y);
        }
        cout << "Scenario #" << ++k << ":\n";
        if (flag)
            cout << "Suspicious bugs found!\n\n";
        else
            cout << "No suspicious bugs found!\n\n";
    }
    return 0;
}

3、Cube Stacking POJ 1988

#include <iostream>
using namespace std;
const int MAXN = 30005;
int s[MAXN], fro[MAXN], sum[MAXN];
void init()
{
    for (int i = 1; i <= MAXN; i++)
    {
        s[i] = i;
        sum[i] = 1;
    }
}
int find_set(int x) //路径压缩
{
    if (x != s[x])
    {
        int t = s[x];
        s[x] = find_set(s[x]);
        fro[x] += fro[t]; //当前点到根节点距离
    }
    return s[x];
}
void union_set(int x, int y)
{
    int rootx = find_set(x), rooty = find_set(y);
    if (rootx != rooty)
    {
        s[rooty] = rootx;
        fro[rooty] = sum[rootx];
        sum[rootx] += sum[rooty]; //总树的长度
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int p, x, y;
    char ch;
    init();
    cin >> p;
    while (p--)
    {
        cin >> ch;
        if (ch == 'M') //并
        {
            cin >> x >> y;
            union_set(x, y);
        }
        else //统计
        {
            cin >> x;
            int t = find_set(x);
            cout << (sum[t] - fro[x] - 1) << endl;
        }
    }
    return 0;
}

4、食物链 POJ 1182

#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 50005;
int s[MAXN], rea[MAXN]; //0同级,1 吃 2 被吃
int ans;
void init(int n)
{
    for (int i = 1; i <= n; i++)
    {
        s[i] = i;
        rea[i] = 0;
    }
}
int find_set(int x)
{
    if (x != s[x])
    {
        int rx = s[x];
        s[x] = find_set(s[x]);
        rea[x] = (rea[x] + rea[rx]) % 3; //更新关系
    }
    return s[x];
}
void union_set(int x, int y, int jud)
{
    int rootx = find_set(x), rooty = find_set(y);
    if (rootx == rooty)
    {
        if ((jud - 1) != (rea[x] - rea[y] + 3) % 3)
            ans++;
    }
    else //union
    {
        s[rootx] = rooty;
        rea[rootx] = (rea[y] - rea[x] + (jud - 1)) % 3;
    }
}
int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    init(n);
    while (k--)
    {
        int tmp, x, y;
        scanf("%d%d%d", &tmp, &x, &y);
        if (x > n || y > n || (tmp == 2 && x == y))
        {
            ans++;
            continue;
        }
        union_set(x, y, tmp);
    }
    printf("%d\n", ans);
    return 0;
}

5、Dragon Balls HDU 3635

#include <iostream>
using namespace std;
const int MAXN = 10005;
int s[MAXN], sum[MAXN], move1[MAXN];
void init(int n)
{
    for (int i = 1; i <= n; i++)
    {
        s[i] = i;
        sum[i] = 1;
        move1[i] = 0;
    }
}
int find_set(int x)
{
    if (x != s[x])
    {
        int t = s[x];
        s[x] = find_set(s[x]);
        move1[x] += move1[t];
    }
    return s[x];
}
void union_set(int x, int y)
{
    int rx = find_set(x), ry = find_set(y);
    if (rx != ry)
    {
        s[rx] = ry;
        sum[ry] += sum[rx];
        sum[rx] = 0;
        move1[rx] = 1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t, tmp = 0, n, q;
    cin >> t;
    while (t--)
    {
        char ch;
        int x, y; //x->y
        cin >> n >> q;
        init(n);
        cout << "Case " << ++tmp << ":\n";
        while (q--)
        {
            cin >> ch;
            if (ch == 'T') //move
            {
                cin >> x >> y;
                union_set(x, y);
            }
            else
            {
                cin >> x;
                int k = find_set(x);
                cout << s[k] << " " << sum[k] << " " << move1[x] << endl;
            }
        }
    }
    return 0;
}

6、More is better HDU 1856

求树的最大子的个数。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int MAXN = 10000000;
int s[MAXN], sum[MAXN], ans = 1;

void init()
{
    for (int i = 1; i <= MAXN; i++)
    {
        s[i] = i;
        sum[i] = 1;
    }
}

int find_set(int x)
{
    if (x != s[x])
        s[x] = find_set(s[x]);
    return s[x];
}

void union_set(int x, int y)
{
    int rx = find_set(x), ry = find_set(y);
    if (rx != ry)
    {
        s[rx] = ry;
        sum[ry] += sum[rx];//树的子的个数
        ans = (sum[ry] > ans ? sum[ry] : ans);
    }
}
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        init();
        int x, y;
        ans = 1;
        while (n--)
        {
            scanf("%d%d", &x, &y);
            union_set(x, y);
        }
        cout << ans << endl;
    }

    return 0;
}

7、Is It A Tree? HUD 1325

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int MAXN = 5005;
int s[MAXN], vis[MAXN], maxx = 0, tp;
bool flag;
void init()
{
    for (int i = 1; i <= MAXN; i++)
    {
        s[i] = i;
        vis[i] = 0;
    }
    maxx = 0;
    flag = 0;
    tp = 0;
}
int find_set(int x)
{
    if (x != s[x])
        s[x] = find_set(s[x]);
    return s[x];
}
void union_set(int x, int y)
{
    int rx = find_set(x), ry = find_set(y);
    if (rx == ry || ry != y) //有向边,子节点时根节点,否则会树会乱
    {
        flag = 1;
        return;
    }
    if (rx != ry)
        s[ry] = rx;
}

int main()
{
    int x, y, tmp = 0, x1, y1;
    while (~scanf("%d%d", &x, &y))
    {
        if (x < 0 && y < 0)
            break;
        if (x == 0 && y == 0)
            printf("Case %d is a tree.\n", ++tmp);
        else
        {
            init();
            union_set(x, y);
            maxx = max(maxx, max(x, y));
            vis[x] = vis[y] = 1;
            while (~scanf("%d%d", &x1, &y1) && (x1 + y1))
            {
                vis[x1] = vis[y1] = 1;
                maxx = max(maxx, max(x1, y1));
                union_set(x1, y1);
            }
            for (int i = 1; i <= maxx; i++)//求树的个数,只能是1或0棵
                if (vis[i] && s[i] == i)
                    ++tp;
            if (!flag && tp <= 1)
                printf("Case %d is a tree.\n", ++tmp);
            else
                printf("Case %d is not a tree.\n", ++tmp);
        }
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值