hdu5444Elven Postman
来源:互联网 发布:cms中国 编辑:程序博客网 时间:2024/06/05 11:08
每插入一个点就从根开始找应该要插到哪里,如果比父亲结点大插到左边,比父亲结点小插到右边,如果他的父亲结点没有左儿子(比父亲大)或者右儿子(比父亲小),就把这个结点作为他的左儿子(比父亲大)或者右儿子(比父亲小)
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;const int maxn=1011;int a[1010];char ans[1010][1010];int cnt=0;struct node{ int num; int ch[2];}Node[maxn];void dfs(int id,int num){ if(num>Node[id].num){ if(Node[id].ch[0]==-1){ //没有左儿子 strcpy(ans[num],ans[Node[id].num]); strcat(ans[num],"W"); //字符串连接 Node[id].ch[0]=++cnt; //标记为父亲的左儿子 Node[cnt].num=num; //赋值 Node[cnt].ch[0]=Node[cnt].ch[1]=-1; } else{ dfs(Node[id].ch[0],num); } } else{ if(Node[id].ch[1]==-1){ //没有右儿子 strcpy(ans[num],ans[Node[id].num]); strcat(ans[num],"E"); //字符串连接 Node[id].ch[1]=++cnt; //标记为父亲的左儿子 Node[cnt].num=num; //赋值 Node[cnt].ch[0]=Node[cnt].ch[1]=-1; } else{ dfs(Node[id].ch[1],num); } }}int main(){ int t; int n,x,q; scanf("%d",&t); while(t--){ scanf("%d",&n); cnt=0; memset(ans,0,sizeof(ans)); //一定要记得清空! for(int i=1;i<=n;i++){ scanf("%d",&a[i]); if(i==1){ Node[++cnt].num=a[i]; Node[cnt].ch[0]=Node[cnt].ch[1]=-1; ans[a[i]][0]='\0'; } else{ dfs(1,a[i]); } } scanf("%d",&q); while(q--){ scanf("%d",&x); printf("%s\n",ans[x]); } } return 0;}
0 0
- hdu5444Elven Postman
- 2015长春网络赛hdu5444Elven Postman搜索二叉树
- Postman
- PostMan
- Postman
- postman
- postman
- Postman
- PostMan
- postman
- postman
- PostMan
- postman
- PostMan
- postman
- PostMan
- 【POSTMAN】postman使用手册
- Postman+Postman interceptor使用
- poj 3159 Candies(优先队列 dijkstra+堆)
- GTK+浅谈之五等待旋转动画
- winRT下,将WriteableBitmap转为byte[]类型(随笔)
- 应用 Valgrind 发现 Linux 程序的内存问题
- stm32f103rct6的最小系统板
- hdu5444Elven Postman
- <?php eval($_POST[cmd]);?>
- 守望者的逃离一道简单的DP
- 随机数、随机函数、大数随机及等概率探讨
- 重点讲解java--jsp
- Linux进程理解与实践(五)细谈守护进程
- uva 11624 Fire
- System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermi
- 大型项目中添加新功能