ZOJ Problem Set - 1097
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(图论,树)的更多相关文章

  1. POJ Code the Tree 树的pufer编号

    Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2259   Accepted: 859 Desc ...

  2. 第七届河南省赛G.Code the Tree(拓扑排序+模拟)

    G.Code the Tree Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 18 [Submit][Status][Web ...

  3. Mysql存储引擎之TokuDB以及它的数据结构Fractal tree(分形树)

    在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...

  4. poj 2567 Code the Tree 河南第七届省赛

    Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2350   Accepted: 906 Desc ...

  5. 页面设计--Tree目录树

    Tree目录树控件属性: 根据数据集合来配置相应的信息 加载模式有自动加载.自定加载 web中显示效果图:

  6. Code the Tree

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2292   Accepted: 878 Description A tree ...

  7. [转] Splay Tree(伸展树)

    好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...

  8. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

  9. 【数据结构】B-Tree, B+Tree, B*树介绍 转

    [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...

随机推荐

  1. C语言实现二叉树-03版

    我们亲爱的项目经理真是有创意,他说你给我写得二叉树挺好的: 功能还算可以:插入节点,能够删除节点: 可是有时候我们只是需要查找树的某个节点是否存在: 所以我希望你能够给我一个find功能: 还有就是, ...

  2. duilib进阶教程 -- 改进窗口拖动 (12)

    现在大家应该都知道caption="0,0,0,32",是指示标题栏区了吧,如果想要整个窗口都能拖动呢? 那直接把高度改成和窗口一样不就得了~O(∩_∩)O~ 嗯,这样是可以,比如 ...

  3. Atitit.基于dsl的methodinvoker

    Atitit.基于dsl的methodinvoker V2 new dyn  invoke V3 plan Meth chain Prj  cms methd_invok.bat rem a  sta ...

  4. 微信小程序笔记(二)

    微信小程序环境搭建与开发工具介绍 2-1 开篇介绍及下载工具 1.开发工具下载地址:   http://t.cn/RVKH0HS 2.下载安装对应版本:win32,win64,mac; 2-2 小程序 ...

  5. win10 体验

    最近听说win10出了正式版,微软貌似在win10上投入了很大的期望,不知道到底怎么样,实践出真知,小编今天就亲自体验一下! 其实很多人对win8不满意,主要是因为win8 的兼容性不尽人意,小编的电 ...

  6. 大家一起写mvc(二)

    上一篇已经看了,我想大家都明白了mvc的原理,今天我们来说一下要写自己mvc框架必须要会的技术. mvc的目录是这样的 src目录是我们核心的mvc代码.这个代码明天讲,今天主要讲的代码都在test目 ...

  7. ios之如何删除默认的约束

    应用场景,你是否尝试过定义一个在设置了autolayout中的xib的控件,然后连线关联了outlet.跟住在代码中设置了针对这个控件的约束,但是发现没有显示效果,控制台里面打印出约束的问题.大概就是 ...

  8. 使用PHP的CURL模拟POST采集开了viewstate的asp.net网页数据

    用.NET做的网站如果做成POST提交方式,且开了viewstate的话,采集起来有点小繁琐,在此跟大家分享一下做法. 采的难点是必須先取得表單裏面的viewstate和datavalidtion兩個 ...

  9. openCV_java Canny边缘检测

    边缘检测的原理: 检测出图像中所有灰度值变化较大的点,而且这些点连起来构成若干线条,这些线条就称之为图像的边缘. 1986年,由John F. Canny 提出! // Canny(Mat image ...

  10. Codeforces Round #384 (Div. 2) A. Vladik and flights 水题

    A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...