|算法讨论|并查集 学习笔记

题目
[树状数组, 并查集]BZOJ 3211:并查集辅助树状数组求值
[左偏树, 并查集]Hdu 1512: 左偏树模板题
[LCA, 并查集]Hdu 2574:森林中的LCA,用并查集辅助


模板及讲解

参考资料:http://www.tuicool.com/articles/UjqUziE

知识点:
1. 拆点
2. 带权
3. 辅助数组

1 拆点
题目:http://blog.csdn.net/darost/article/details/53013154

2 带权
带权并查集判断权值
带权并查集算r[x]

题目:http://blog.csdn.net/darost/article/details/53453483

#include<cstdio>  
#include<algorithm>  
#include<cstring>  
#include<queue>  
#define ms(i,j) memset(i,j, sizeof i);  
using namespace std;   
int n,k;   
int father[150005];  
int r[150005];//每个点指出的权值   
int ans = 0;  
int find(int x)  
{  
    if (father[x]==x) return x;  
    int tmp = father[x];   
    father[x] = find(father[x]);//递归并且路径压缩   
    r[x] = (r[tmp]+r[x])%3;//用公式算出x指出的权值   
    return father[x];  
}  
int main()  
{  
    scanf("%d%d", &n ,&k);  
    for (int i=1;i<=n;i++) father[i] = i;  
    for (int i=1;i<=k;i++)  
    {  
        int ty;  
        int a,b;  
        scanf("%d%d%d", &ty, &a, &b);  
        if (a<=0||b<=0||a>n||b>n)//不符合范围   
        {  
            ans++;  
            continue;  
        }  
        int x = find(a), y = find(b);//找根   
        if (x!=y)//不在一个并查集里   
        {  
            father[x] = y;//合并   
            r[x] = (r[b]-r[a]+ty-1+66666)%3; //用公式算出权值   
        }else  
        {  
            if ((r[b]-r[a]+ty-1)%3) ans++;//用公式验证是否关系正确   
        }  
    }  
    printf("%d\n", ans);   
    return 0;  
}  

3 数组辅助 题目:http://blog.csdn.net/Darost/article/details/53446883

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值