题目
[树状数组, 并查集]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 带权
题目: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