poj 1962 带权并查集
来源:互联网 发布:vim python 语法高亮 编辑:程序博客网 时间:2024/05/16 10:35
注意路径压缩时的权值合并
View Code
#include<cstdio>
#include<cstring>
#include<stdlib.h>
const int MAX = 20010;
int p[MAX],w[MAX];
void init(int n)
{
for(int i=0;i<=n;i++)
{
p[i]=i;
w[i]=0;
}
}
void unio(int x,int y)
{
p[x]=y;
w[x]=(abs(x-y)%1000);
}
int find(int x)
{
if(p[x]==x) return x;
else
{
find(p[x]);w[x]+=w[p[x]];
p[x]=find(p[x]);
}
}
int main()
{
int t,n,x,y;
char ch;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init(n);
getchar();
while(scanf("%c",&ch)&&ch!='O')
{
if(ch=='E')
{
scanf("%d",&x);
find(x);
printf("%d\n",w[x]);
}
else
{
scanf("%d%d",&x,&y);
unio(x,y);
}
getchar();
}
}
return 0;
}
- poj 1962 带权并查集
- poj 1182 (带权并查集)
- POJ 2492[带权并查集]
- poj 1733带权并查集
- poj 1182 带权并查集
- poj 1182(带权并查集)
- poj 2912(带权并查集)
- poj 1733(带权并查集)
- poj 1182 带权并查集
- POJ 1703 带权并查集
- 带权并查集 poj 1182
- poj 1984 带权并查集
- POJ 1703 带权并查集
- poj 1962 Corporative Network(带权并查集)
- POJ - 1962 Corporative Network(带权并查集)
- poj 3728 tarjan+带权路径并查集
- poj 1733 Parity game(带权并查集)
- [带权并查集] poj 1733 Parity game
- 从 hdu 3449 看一类最简单的依赖背包
- poj 1787 多重背包记录路径
- poj 2248 Addition Chains dfs
- 常用Shell命令之一
- zstu 1052 水题。。模拟
- poj 1962 带权并查集
- poj 2513 很多RE,很多WA
- AC小结
- hdu 1251 字典树水题(还是没hold住,又刷题了,哎~)
- 最小生成树 终极版本
- hdu 1879 简单最小生成树
- hdu 3371 最小生成树(有重边)
- hdu 1247 字典树
- hdu 2486 字典树