并查集 带压缩路径的版本
2011-09-26 16:12
190 查看
#include <iostream>
using
namespace
std;
typedef
struct
{
int
a,b;
}road;
const
int
MAX=5001;
int
N,M,K;
int
father[MAX];
int
rank[MAX];
road rd[500000];
void
make_set()
{
int
i;
for
(i=1;i<=N;i++)
{
father[i]=i;
rank[i]=0;
}
}
int
find_set(
int
x)
{
if
(x!=father[x])
father[x]=find_set(father[x]);
return
father[x];
}
void
union_set(
int
x,
int
y)
{
x=find_set(x);
y=find_set(y);
if
(x==y)
return
;
if
(rank[x]>rank[y])
father[y]=x;
else
{
father[x]=y;
if
(rank[x]==rank[y])
rank[y]++;
}
}
int
main()
{
cin>>N>>M>>K;
int
i,j;
int
start,end;
int
input;
for
(i=1;i<=M;i++)
{
cin>>start>>end;
rd[i].a=start;
rd[i].b=end;
}
for
(i=1;i<=K;i++)
{
int
cnt=-2;
cin>>input;
make_set();
for
(j=1;j<=M;j++)
{
if
(rd[j].a!=input && rd[j].b!=input)
union_set(rd[j].a,rd[j].b);
}
for
(j=1;j<=N;j++)
{
if
(father[j]==j)
cnt++;
}
cout<<cnt<<endl;
}
return
0;
}
/**************************************************************
Problem: 1325
User: feng345feng
Language: C++
Result: Accepted
Time:800 ms
Memory:5456 kb
****************************************************************/
相关文章推荐
- BZOJ 3674 可持久化并查集加强版(路径压缩版本)
- 数据结构实现之并查集(使用按秩合并和路径压缩)
- LA3027 并查集 压缩路径+维护距离
- 并查集 & 路径压缩
- [HDOJ3635]Dragon Balls(并查集,路径压缩)
- LA 3027 Corporative Network 合作网络【并查集+路径压缩】
- 并查集模板[路径压缩]
- 并查集 路径压缩 非递归写法
- [HDU 3635]Dragon Balls[并查集][路径压缩]
- 并查集路径压缩方法
- 并查集的两种优化(按秩合并,路径压缩)
- 并查集(两个优化—按秩合并、路径压缩) poj2492
- 并查集-路径压缩
- poj1182食物链(带权并查集+路径压缩)
- 并查集(路径压缩 && 启发式合并!!!)
- HDU 1856 More is better 并查集 路径压缩
- Corporative Network UVA - 1329 加权并查集(压缩路径优化)
- [并查集压缩路径]PAT1013 Battle Over Cities
- 并查集 路径压缩(详解)
- uvalive 3027 Corporative Network 并查集+路径压缩