#洛谷oj:P2024 [NOI2001] 食物链


洛谷oj:P2024 [NOI2001] 食物链


#题目描述
在这里插入图片描述

#这个是带权并查集的典型题
#每当一个动物第一次出现时 这个关系因为没有参照关系 所以一定是对的 就加入并查集

#知识点
带权并查集

#代码
第一次
还是没有怎么弄懂这个之间的关系 修修改改 一直改关系 还是只有50 差一半

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int root[50008];
int relat[50008];
int n,m,a,b,c;
int Find(int a)
{
    if(root[a] == a)
    {
        relat[a] = 0;//自己是自己的同类
        return root[a];
    }
    else
    {
        int temp = root[a];
        root[a] = Find(root[a]);
        relat[a] = (relat[a] + relat[temp])%3;
        return root[a];
    }
}
int chang(int a)
{
    if(a == 0)
        return 0;
    if(a == 1)
        return 2;
    if(a == 2)
        return 1;
}
void init()
{
    for(int i=1; i<=n; i++)
    {
        root[i] = i;
        relat[i] = 0;
    }
}
int main()
{
    int ans = 0;
    cin >> n >> m;
    init();
    for(int i=1; i<=m; i++)
    {
        cin >> a >>b >> c;
        if(a == 2 && b == c)
        {
            ans ++;
            //cout << "错误" <<endl;
            continue;
        }
        if( b > n || c > n)
        {
            ans ++;
            //cout << "错误" <<endl;
            continue;
        }
        if(a == 1)//同类
        {
            int rb = Find(b),rc = Find(c);
            if(rb != rc)//有新的节点 真的
            {
                root[rb] = rc;
                relat[rb] = relat[c] - relat[b];
            }
            else
            {
                if(relat[b] != relat[c])//应该对同一节点有相同关系  不是就错误
                {
                    ans ++;
                    //cout << "1错误" <<endl;
                    continue;
                }
            }
        }
        else //b 吃 c
        {
            int rb = Find(b),rc = Find(c);
            if(rb != rc)//有新的节点 真的
            {
                root[rb] = rc;
                relat[rb] = (1 + relat[c] - relat[b])%3;
            }
            else
            {
                if(relat[b] == 0)
                {
                    if(relat[c] != 2)
                    {
                        ans ++;
                        continue;
                    }
                }else if(relat[b] == 1)
                {
                    if(relat[c] != 0)
                    {
                        ans ++;
                        continue;
                    }
                }else if(relat[b] == 2)
                {
                    if(relat[c] != 1)
                    {
                        ans ++;
                        continue;
                    }
                }
            }

        }
    }
    cout << ans <<endl;
    return 0;
}

第二次
这个关系就是像矢量那样 没弄懂就花时间捣鼓 见多了就会感觉很熟悉了
还有 这个是不是不保证全部数据都是正确的啊 还是老老实实的用if 吧 用一个else 都心慌 怕错

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int root[50008];
int relat[50008];
int n,m,a,b,c;
int Find(int a)
{
    if(root[a] == a)
    {
        return root[a];
    }
    else
    {
        int temp = root[a];///!!! 
        root[a] = Find(root[a]);//状态压缩
        relat[a] = (relat[a] + relat[temp])%3;//注意要用没有压缩过的作为跳板来算权值 
        return root[a];
    }
}

void init()
{
    for(int i=1; i<=n; i++)
    {
        root[i] = i;
        relat[i] = 0;//自己是自己的同类
    }
}
int main()// 0 同类  12 被吃
{
    int ans = 0;
    cin >> n >> m;
    init();
    for(int i=1; i<=m; i++)
    {
        cin >> a >>b >> c;
        if(a == 2 && b == c)
        {
            ans ++;
            //cout << "错误" <<endl;
            continue;
        }
        if( b > n || c > n)
        {
            ans ++;
            //cout << "错误" <<endl;
            continue;
        }
        if(a == 1)//同类
        {
            int rb = Find(b),rc = Find(c);
            if(rb != rc)//有新的节点 真的
            {
                root[rb] = rc;
                relat[rb] = (3+relat[c] - relat[b])%3;//!! 漏了  要保证不会为负数 !!!!
            }
            else if(relat[b] != relat[c])//应该对同一节点有相同关系  不是就错误
            {
                ans ++;
                //cout << "1错误" <<endl;
                continue;
            }
        }
        if(a == 2) //b 吃 c
        {
            int rb = Find(b),rc = Find(c);
            if(rb != rc)//有新的节点 真的
            {
                root[rb] = rc;
                relat[rb] = (3+1 + relat[c] - relat[b])%3;//!! 漏了
            }
            else if( 1 != (3+relat[b]-relat[c])%3)
            {
                //cout << "2错误"<<endl;
                ans ++;
                continue;
            }

//                if(relat[b] == 0)
//                {
//                    if(relat[c] != 2)
//                    {
//                        ans ++;
//                        continue;
//                    }
//                }else if(relat[b] == 1)
//                {
//                    if(relat[c] != 0)
//                    {
//                        ans ++;
//                        continue;
//                    }
//                }else if(relat[b] == 2)
//                {
//                    if(relat[c] != 1)
//                    {
//                        ans ++;
//                        continue;
//                    }
//                }

        }
    }
    cout << ans <<endl;
    return 0;
}

#总结
带权并查集 在普通的并查集中在加了 一个(关系 或者 权) 值 并通过这个权值来约束数据情况
这种并查集的find 函数 都会带权值 并且只会在要使用的使用在求出来 可以看成 运行find 函数 的附带的数据 不必要单独的用循环来求 费时费力

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值