左偏树 板子
来源:互联网 发布:php网络爬虫软件 编辑:程序博客网 时间:2024/05/16 01:54
大神说左偏树很有用,就学了下,贴个板子
例题:BZOJ1455 罗马游戏
code:
#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<vector>#include<string>#include<bitset>#include<cstdio>#include<cstdlib>#include<cstring>#include<climits>#include<complex>#include<iostream>#include<algorithm>#define LL long longusing namespace std; const int maxn = 1100000;struct node{ int dist,c,lc,rc;}tree[maxn];int fa[maxn];bool dead[maxn];int n,m; int find_( int x ){ if( fa[x] == x ) return x; return fa[x] = find_( fa[x] );}int merge( int x,int y ){ if( !x || !y ) return x|y; if( tree[x].c > tree[y].c ) swap( x,y ); tree[x].rc = merge( tree[x].rc,y ); fa[tree[x].rc] = x; tree[x].dist = tree[tree[x].rc].dist+1; if( tree[tree[x].lc].dist < tree[tree[x].rc].dist ) swap( tree[x].lc,tree[x].rc ); return x;} int main(){ char st[110]; int x,y; scanf("%d",&n); for( int i=1;i<=n;i++ ) { scanf("%d",&tree[i].c); dead[i] = false; fa[i] = i; tree[i].dist = 0; tree[i].lc = tree[i].rc = 0; } scanf("%d",&m); while( m-- ) { scanf("%s",st); if( st[0] == 'M' ) { scanf("%d%d",&x,&y); if( !dead[x] && !dead[y] ) { int f1 = find_( x ), f2 = find_( y ); if( f1 != f2 ) merge( f1,f2 ); } } else { scanf("%d",&x); if( !dead[x] ) { int f1 = find_( x ); printf("%d\n",tree[f1].c); dead[f1] = true; x = merge( tree[f1].lc,tree[f1].rc ); fa[x] = x; fa[f1] = x; } else printf("0\n"); } } return 0;}
0 0
- 左偏树 板子
- 板子
- 板子
- 测试板子
- 板子总结
- 入手板子
- 新板子
- Ivus 板子
- dinic 板子
- manacher板子
- NTT板子
- RMQ板子
- spfa板子
- kmp板子
- manacher板子
- LCA板子
- OI板子
- KD_tree 板子
- new与make的区别
- 流形学习
- 《Deep Learning》(2)-线性代数
- OS X入门 — xcode中如何使用git
- python开发环境搭建
- 左偏树 板子
- linux中process, kernel thread, user thread的区别
- 控制反转(Inversion of Control)与依赖注入(Dependency Injection)
- Kotlin语法(十)-扩展
- 表的抽象数据类型(abstract data type,ADT)
- String s=new String(“xyz”);创建了几个对象
- Delphi 内存映射
- HTML常用标签
- Linux运维笔记----文件管理