Code the Tree(图论,树)
Time Limit: 2 Seconds Memory Limit: 65536 KB
A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built as follows: the leaf (a vertex that is incident to only one edge) with the minimal number is taken. This leaf, together with its incident edge is removed from the graph, while the number of the vertex that was adjacent to the leaf is written down. In the obtained graph, this procedure is repeated, until there is only one vertex left (which, by the way, always has number n). The written down sequence of n-1 numbers is called the Prufer code of the tree.
Your task is, given a tree, to compute its Prufer code. The tree is denoted by a word of the language specified by the following grammar:
T ::= "(" N S ")"
S ::= " " T S
| empty
N ::= number
That is, trees have parentheses around them, and a number denoting the identifier of the root vertex, followed by arbitrarily many (maybe none) subtrees separated by a single space character. As an example, take a look at the tree in the figure below which is denoted in the first line of the sample input.
Note that, according to the definition given above, the root of a tree may be a leaf as well. It is only for the ease of denotation that we designate some vertex to be the root. Usually, what we are dealing here with is called an "unrooted tree".
Input Specification
The input contains several test cases. Each test case specifies a tree as described above on one line of the input file. Input is terminated by EOF. You may assume that 1<=n<=50.
Output Specification
For each test case generate a single line containing the Prufer code of the specified tree. Separate numbers by a single space. Do not print any spaces at the end of the line.
用连接表存储树,再每次找最小的leaf即可。难点是建树。方法:
1.建一个栈用于存储节点。
2.当遇到 ( 时,输入节点编号i,(1)如果栈非空,栈顶与 i 相邻,更新邻接表中栈顶和i的相应项,再将i压入栈中;(2)如果栈为空,将i压入栈中。
3.当遇到 ) 时,弹栈。
4. 当遇到空格时,跳过。
注意当树只有根时,如(1),输出换行符即可
AC code:
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <list>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const int MAXN = ; int maxId, cntId; //maxId是最大节点,cntId是节点数
list<int> adj[MAXN]; //邻接表 void build_tree()
{
char c;
int id;
stack<int> sta;
scanf("%d", &id);
sta.push(id);
cntId = ;
maxId = id;
while(scanf("%c", &c) && c != '\n')
{
if(c == ' ') continue;
if(c == '(')
{
scanf("%d", &id);
cntId++;
if(maxId < id) maxId = id;
int f = sta.top();
adj[f].push_front(id);
adj[id].push_front(f);
sta.push(id);
}
else if(c == ')') sta.pop();
}
} int findMinLeaf()
{
int i;
for(i = ; i <= maxId; i++)
{
if(adj[i].size() == ) break;
}
int s = *adj[i].begin();
adj[i].pop_back();
list<int>::iterator it = adj[s].begin();
for(; it != adj[s].end(); it++)
{
if(*it == i) break;
}
adj[s].erase(it);
return s;
} int main()
{
char ch;
while(scanf("%c", &ch) != EOF)
{
int i;
for(i = ; i < MAXN; i++)
adj[i].clear();
build_tree();
if(cntId < )
{
puts("");
continue;
}
for(i = ; i < cntId - ; i++)
printf("%d ", findMinLeaf());
printf("%d\n", findMinLeaf());
}
return ;
}
2013-07-31 23:09:35
Code the Tree(图论,树)的更多相关文章
- POJ Code the Tree 树的pufer编号
Code the Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2259 Accepted: 859 Desc ...
- 第七届河南省赛G.Code the Tree(拓扑排序+模拟)
G.Code the Tree Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 18 [Submit][Status][Web ...
- Mysql存储引擎之TokuDB以及它的数据结构Fractal tree(分形树)
在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...
- poj 2567 Code the Tree 河南第七届省赛
Code the Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2350 Accepted: 906 Desc ...
- 页面设计--Tree目录树
Tree目录树控件属性: 根据数据集合来配置相应的信息 加载模式有自动加载.自定加载 web中显示效果图:
- Code the Tree
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2292 Accepted: 878 Description A tree ...
- [转] Splay Tree(伸展树)
好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- 【数据结构】B-Tree, B+Tree, B*树介绍 转
[数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...
随机推荐
- C/C++文件操作2
一.流式文件操作 这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下: typedef struct { int level; /* fill/empty level of ...
- Atitit..文件上传组件选型and最佳实践总结(2)----断点续传
Atitit..文件上传组件选型and最佳实践总结(2)----断点续传 1. 断点续传的原理 1 2. 如何判断一个插件/控件是否支持断点续传?? 1 3. 常用的组件选型结果::马 1 4. 自定 ...
- iOS -iPhone5、iPhone5s、iPhone6、iPhone6Plus 屏幕适配
现在由于苹果公司出了6和6Plus,让写苹果程序的哥们为了做兼容很头疼.用StoryBoard固然方便,但是后期做兼容要花费太多的时间和精力.使用AutoLayout虽然会在不同尺寸的屏幕下自动布局, ...
- raspberry pi 如何汉化显示中文
1 树莓派初装系统之后,首次启动会出现“raspi-config”工具,如下图:(若不是初次启动,在命令模式下,请输入 sudo raspi-config 命令,即可调出此界面.若在图形桌面下,打开桌 ...
- 实用的插件:跨浏览器复制jQuery-zclip
Query-zclip是一个复制内容到剪贴板的jQuery插件,使用它我们不用考虑不同浏览器和浏览器版本之间的兼容问题.jQuery-zclip插件需要Flash的支持,使用时记得安装Adobe Fl ...
- WindowsPhone-GameBoy模拟器开发六--[转]指令系统实现必读:补码
网上有同行写了些好文章,在此就不现丑了,贴上连接,放在这里为了补充系列的完整性 计算机为什么选用二进制补码 为什么补码重要?因为计算机中内存.寄存器里面存的数都是用补码表示的!
- Win7快捷方式图标不显示解决办法
问题:WIN7的系统,桌面.开始菜单以及任务栏的快捷方式图标显示不正常,看不到程序默认图标,快捷方式图标不显示. 解决方法:删除程序图标缓存即可. 将下面的内容复制到记事本保存为“图标缓存清理.b ...
- ARM中C和汇编混合编程及示例(转)
在嵌入式系统开发中,目前使用的主要编程语言是C和汇编,C++已经有相应的编译器,但是现在使用还是比较少的.在稍大规模的嵌入式软件中,例如含有OS,大部分的代码都是用C编写的,主要是因为C语言的结构比较 ...
- [Android实例] 有关spinner 的item问题 谁能给解答下??
[Android实例] 有关spinner 的item问题 (更多Android问题解决,Android开发讨论 请访问:http://www.eoeandroid.com/forum.php)
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...