并查集 ,删除节点(图画展示)
来源:互联网 发布:编程解决鸡兔同笼问题 编辑:程序博客网 时间:2024/05/02 02:47
删除节点x, 找个新的节点y替换x , father[y] = x ;
图一: 删除节点4 , 4 替换成5 , 5的父亲为5 。
图二: 1,2,3的父亲还是为4
图三: 相当于做了一个映射。
1->1 , 2->2 , 3->3 , 4->5 。
father[1] = 4 , father[2] = 4 , father[3] = 4 , father[5] = 5 ,
没有father【4】 , 因为4已经被5给替换掉了。
图四: 合并 1 , 4
father【1 ->1】 = 4 ;
father【4 ->5】 = 5 ;
为了看清楚,特让father[5] = 4 ;
测试地址http://acm.hdu.edu.cn/showproblem.php?pid=2473
const int Max_N = 2000008 ;struct Node{ int father ; int Replace ;};Node node[Max_N] ;int N ;void build(){ int i ; for(i = 0 ; i < N ; i++){ node[i].father = i ; node[i].Replace = i ; }}int find_father(int x){ if(node[x].father == x) return x ; else return node[x].father = find_father(node[x].father) ;}void MerGe(int x , int y){ int f_x = find_father(x) ; int f_y = find_father(y) ; if(f_x != f_y) node[f_x].father = f_y ;}void Delete(int x){ node[x].Replace = N ; node[N].father = N ; N++ ;}int main(){ int M , n , i , x , y , cas = 1 ; char str[2] ; while(scanf("%d%d",&N ,&M)){ if(N == 0 && M == 0) break ; n = N ; build() ; while(M--){ scanf("%s" ,str) ; if(str[0] == 'M'){ scanf("%d%d" ,&x ,&y) ; MerGe(node[x].Replace , node[y].Replace) ; } else{ scanf("%d" ,&x) ; Delete(x) ; } } set<int>ans ; ans.clear() ; for(i = 0 ; i < n ; i++) ans.insert(find_father(node[i].Replace)) ; printf("Case #%d: %d\n" , cas++ , ans.size()) ; } return 0 ;}
0 0
- 并查集 ,删除节点(图画展示)
- 并查集(删除节点)
- 并查集节点删除于添加
- hdu2473 Junk-Mail Filter (并查集删除节点)
- UVA 11987 Almost Union-Find 并查集节点删除
- 合纵连横 1022 nsoj 并查集 删除节点
- HDU 2473 Junk-Mail Filter(并查集删除节点)
- Junk-Mail Filter(并查集,删除结点,虚父节点)
- ZOJ 3789 Gears (并查集 合并+删除+离根节点的距离)
- 并查集 删除节点 求集合的个数<set> hdu 2473
- zoj Gears 神奇并查集+删除+加根节点距离记录
- hdu 2473 Junk-Mail Filter(删除节点操作的并查集)
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
- HDU2473 - Junk-Mail Filter 利用虚拟数组实现删除并查集的节点
- 并查集(删除节点,就是增加一个新的爹)
- 并查集删除(转)
- 并查集的删除
- hdu 2473并查集+节点映射
- Eclipse中跟踪调试Android源代码&&Android中的菜单显示风格
- 在"运行"下运行的命令
- (五)工厂模式
- 常量指针与指针常量的区别(转帖)
- 文件上传
- 并查集 ,删除节点(图画展示)
- 项目四。工资类数组
- 文件操作java源码
- information chaos
- 天津丽芸网络科技有限公司,代理费还给我
- [Ubuntu]基于openssl的MD5加密C语言实现
- OCM_Session7_0_DBCA删除RAC数据库实例
- jdk和jre
- [FreeMarker 2.3.20] Part I 关于模版设计的介绍 ~模板~架构总览、指令