POJ 3437 Tree Grafting(有序树转化为二叉树)

Tree Grafting

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1834   Accepted: 795

Description

Trees have many applications in computer science. Perhaps the most commonly used trees are rooted binary trees, but there are other types of rooted trees that may be useful as well. One example is ordered trees, in which the subtrees for any given node are
ordered. The number of children of each node is variable, and there is no limit on the number. Formally, an ordered tree consists of a finite set of nodes T such that

  • there is one node designated as the root, denoted root(T);
  • the remaining nodes are partitioned into subsets T1, T2, ..., Tm, each of which is also a tree (subtrees).

Also, define root(T1), ..., root(Tm) to be the children of root(T), with root(Ti) being the i-th child. The nodes root(T1), ..., root(Tm) are siblings.

It is often more convenient to represent an ordered tree as a rooted binary tree, so that each node can be stored in the same amount of memory. The conversion is performed by the following steps:

  1. remove all edges from each node to its children;
  2. for each node, add an edge to its first child in T (if any) as the left child;
  3. for each node, add an edge to its next sibling in T (if any) as the right child.

This is illustrated by the following:

         0                             0
       / | \                          /
      1  2  3       ===>             1
        / \                                  4   5                           2
                                      /                                      4   3
                                                                             5

In most cases, the height of the tree (the number of edges in the longest root-to-leaf path) increases after the conversion. This is undesirable because the complexity of many algorithms on trees depends on its height.

You are asked to write a program that computes the height of the tree before and after the conversion.

Input

The input is given by a number of lines giving the directions taken in a depth-first traversal of the trees. There is one line for each tree. For example, the tree above would give dudduduudu, meaning 0 down to 1, 1 up to 0, 0 down to 2, etc. The input is
terminated by a line whose first character is #. You may assume that each tree has at least 2 and no more than 10000 nodes.

Output

For each tree, print the heights of the tree before and after the conversion specified above. Use the format:

 Tree t: h1 => h2 

where t is the case number (starting from 1), h1 is the height of the tree before the conversion, and h2 is the height of the tree after the conversion.

Sample Input

dudduduudu
ddddduuuuu
dddduduuuu
dddduuduuu
#

Sample Output

Tree 1: 2 => 4
Tree 2: 5 => 5
Tree 3: 4 => 5
Tree 4: 4 => 4

Source

Rocky Mountain 2007

#include <iostream>
#define maxn 20005
using namespace std;
char s[maxn];
int height1,height2;
int i;

void dfs(int a,int b)
{
    int tempson=0;
    while(s[i]=='d')
    {
        i++;
        tempson++;  //tempson 代表第几个 儿子
        dfs(a+1,b+tempson);
    }
    i++;
	height1=a>height1?a:height1;
	height2=b>height2?b:height2;
}

int main()
{
	int n=1;
    while(cin>>s)
    {

        if(s[0]=='#')
            break;

		i=0;
        height1=height2=0;
        dfs(0,0);
		cout<<"Tree "<<n  <<": "<<height1<<" => "<<height2<<endl;
		n++;
    }

	return 0;
}

这道题我一定会回来再做!

POJ 3437 Tree Grafting(有序树转化为二叉树)

时间: 2025-01-20 15:11:52

POJ 3437 Tree Grafting(有序树转化为二叉树)的相关文章

LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh

POJ 2378 Tree Cutting(树的重心)

题目链接:http://poj.org/problem?id=2378 题目: Description After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses. Bessie, fee

POJ 2378 Tree Cutting (树的重心,微变形)

题意: 给定一棵树,n个节点,若删除点v使得剩下的连通快最大都不超过n/2,则称这样的点满足要求.求所有这样的点,若没有这样的点,输出NONE. 思路: 只需要拿“求树的重心”的代码改一行就OK了.因为依然是在判别最大连通块的点数. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath&g

POJ 2255 Tree Recovery(树的遍历)

给定前序遍历和中序遍历,写出后序遍历. #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <assert.h> #include <algorithm> #define MAX 1234567890 #define MIN

如何将一棵树转化成二叉树

要点: 从这棵树的根结点开始,从上到下,看每一个结点,把你正在看的结点的孩子放在左子树,兄弟放在右子树. 口诀: 1. 将 节点的孩子 放在左子树: 2. 将 节点的兄弟 放在右子树. 关于这个问题,最好的办法就是记住一道例题了,因为语言不是很好描述,也不容易看懂描述. 例题: 或者有另一种理解方法: 步骤: 1.在所有兄弟结点之间加一连线 2.对每个结点,除了保留与其长子的连线外,去掉该结点与其它孩子的连线. 如下图所示: ————————————————版权声明:本文为CSDN博主「猫萌萌」

Poj 1741 Tree (树的分治)

题目链接: Poj 1741 Tree 这个题目Tle的好苦啊,原来一直是树的重心没找对,Tle好长时间,终于对了,好感动,先贴个代码. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 10010; 8 struct node 9 { 10 int

POJ 3723 Tree(树链剖分)

POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后,交换即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 10005; const int INF = 0x3f3f3f3f; int dep[N],

poj 3237 Tree(树链拆分)

题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询ab路径上节点权值的最大值. 解题思路:树链剖分.然后用线段树维护节点权值,成端更新查询. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int max

poj 1741 Tree(树的点分治)

poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出来.接着对于每一个子树再次运算.如果不用点分治的技巧,时间复杂度可能退化成\(O(n^2)\)(链).如果对于子树重新选根,找到树的重心,就一定可以保证时间复杂度在\(O(nlogn)\)内. 具体技巧是:首先选出树的重心,将重心视为根.接着计算出每个结点的深度,以此统计答案.由于子树中可能出现重复