树的转换
来源:互联网 发布:怎样注销淘宝账号 编辑:程序博客网 时间:2024/06/05 22:43
7:树的转换
- 查看
- 提交
- 统计
- 提问
- 总时间限制:
- 5000ms
- 内存限制:
- 65536kB
- 描述
我们都知道用“左儿子右兄弟”的方法可以将一棵一般的树转换为二叉树,如:
0 0 / | \ / 1 2 3 ===> 1 / \ \ 4 5 2 / \ 4 3 \ 5
现在请你将一些一般的树用这种方法转换为二叉树,并输出转换前和转换后树的高度。
- 输入
- 输入包括多行,最后一行以一个#表示结束。
每行是一个由“u”和“d”组成的字符串,表示一棵树的深度优先搜索信息。比如,dudduduudu可以用来表示上文中的左树,因为搜索过程为:0 Down to 1 Up to 0 Down to 2 Down to 4 Up to 2 Down to 5 Up to 2 Up to 0 Down to 3 Up to 0。
你可以认为每棵树的结点数至少为2,并且不超过10000。 - 输出
- 对于每棵树,按如下格式输出转换前和转换后树的高度:
Tree t: h1 => h2
其中t是树的编号(从1开始),h1是转换前树的高度,h2是转换后树的高度。 - 样例输入
dudduduududdddduuuuudddduduuuudddduuduuu#
- 样例输出
Tree 1: 2 => 4Tree 2: 5 => 5Tree 3: 4 => 5Tree 4: 4 => 4
#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<iomanip>#include<queue>#include<stack>#include<vector>#include<set>#include<map>using namespace std;struct Node{vector<int>Son;int father;int depth;}N[10005];string s;int num=0,len=0,h1,h2;void MakeTree(int father,int x){if(x>=len)return;if(s[x]=='d'){N[num].depth=N[father].depth+1;N[num].father=father;N[father].Son.push_back(num);if(h1<N[num].depth)h1=N[num].depth;num++;MakeTree(num-1,x+1);}else{MakeTree(N[father].father,x+1);}}void Search(int father,int x,int depth){int n=N[father].Son.size();if(x>=n)return;if(h2<depth)h2=depth;Search(father,x+1,depth+1);Search(N[father].Son[x],0,depth+1);}void Init(int root){int n=N[root].Son.size();for(int i=0;i<n;++i){Init(N[root].Son[i]);}N[root].father=0;N[root].depth=0;N[root].Son.clear();}int main(){int Test=0;while(cin>>s){ len=s.length(),h1=0,h2=0;if(s[0]=='#'){break;}Test++;Init(0);num=1;MakeTree(0,0);Search(0,0,1);cout<<"Tree "<<Test<<": "<<h1<<" => "<<h2<<endl;}return 0;}
阅读全文
0 0
- openjudge树的转换
- 树的转换
- 4081:树的转换
- 树与森林的转换
- 树和森林的转换
- 数据结构转换 协议的转换
- 树:树、森林、二叉树的转换
- 树、森林与二叉树的转换
- 树,森林,二叉树之间的转换
- 树 森林 与二叉树的转换
- 树 森林和二叉树的转换
- 树、森林和二叉树的转换
- 树、森林与二叉树的转换
- 树、森林与二叉树的转换
- 树、森林与二叉树的转换
- 树、森林和二叉树的转换
- 树、森林与二叉树的转换
- 树与二叉树的转换
- webpack-dev-server
- 有关java中子类覆盖父类的方法的返回值的问题
- 关于 vue.js 运行环境的搭建(mac)
- Tensorflow基础知识与神经网络构建--step by step 入门TensorFlow(一)
- 赋予和收回用户DBA权限
- 树的转换
- 关于java子类方法覆盖(Override)父类方法时,该子类方法返回值的问题
- Hololens 配置文件
- (UVA
- 基于Dragonboard 410c的智能遥控器(二)
- ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
- git module构建公共repo
- 浙大PAT甲级-1048
- 使用indent命令对代码进行格式化处理