uva 699 The Falling Leaves(建树)

来源:互联网 发布:贵州广电网络通讯录 编辑:程序博客网 时间:2024/05/22 08:37

  The Falling Leaves 

Each year, fall in the North region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?


We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

The nodes containing 5 and 6 have the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 is one unit to their right. When the "leaves" drop from these nodes, three piles are created: the leftmost one contains 7 leaves (from the leftmost node), the next contains 11 (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it is true that only leaf nodes in a tree would logically have leaves, we ignore that in this problem.)

Input 

The input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the description of the right subtree. If a subtree is empty, the value -1 is supplied. Thus the tree shown above is specified as 5 7 -1 6 -1 -1 3 -1 -1. Each actual tree node contains a positive, non-zero value. The last test case is followed by a single -1 (which would otherwise represent an empty tree).

Output 

For each test case, display the case number (they are numbered sequentially, starting with 1) on a line by itself. On the next line display the number of "leaves" in each pile, from left to right, with a single space separating each value. This display must start in column 1, and will not exceed the width of an 80-character line. Follow the output for each case by a blank line. This format is illustrated in the examples below.

Sample Input 

 

5 7 -1 6 -1 -1 3 -1 -18 2 9 -1 -1 6 5 -1 -1 12 -1-1 3 7 -1 -1 -1-1

 

Sample Output 

 

Case 1:7 11 3Case 2:9 7 21 15

 

 

 

题目大意:垂直方向的相加并且输出,-1带表空。(例如:A左节点的右节点与A的右节点的做节点是同一个,并且和A处于同一垂直线)

 

解题思路:生成网状结构的树,并记录最深分支的深度(dep)。然后遍历第dep行和dep-1行(需间隔遍历)。

 

#include<iostream>#include<string.h>#include<math.h>using namespace std;#define N 10005int tree[N];//树.int dep=0;//记录深度.int num[N];int sq(int n)//计算本行的头下标。{int sum=0;int i;for(i = 1;i <= n; i++)sum+=i;return sum+1;}void grow(int k,int n)//生成树。{if(n>dep)dep=n;int t;cin>>t;if(t!=-1){tree[k]+=t;intp=sq(n),q=sq(n+1); grow(k-p+q,n+1);grow(k-p+q+1,n+1);}}int find(int m,int h)//寻找隔层同方向。{if(h<=1)return tree[m];int f=m;int i;for(i=0;i<2;i++)//换行公式。(跳行需换行两次){f=f-sq(h)+sq(h-1);h--;}f--;if(f<sq(h+1)&&f>=sq(h))//判断跳行后下标是否满足。return tree[m]+find(f,h);elsereturn tree[m];}int main(){//Init.memset(tree,0,sizeof(tree));memset(num,0,sizeof(num));int t=1;//计数器。int i;int k=0;//记录落叶堆数while(cin>>tree[1],tree[1]!=-1){grow(2,1);//生成树。grow(3,1);int h=--dep;int g=sq(dep+1);for(i = sq(dep);i < g; i++){int o;//辅助输出。int m=i-sq(h)+sq(h-1);if(o=find(i,h))//遍历n行。num[k++]=o;if(i==g-1)break;if(o=find(m,h-1))//遍历n-1行。num[k++]=o;}cout<<"Case "<<t++<<":"<<endl;//输出。for(i = 0;i < k-1; i++)cout<<num[i]<<" ";cout<<num[i]<<endl<<endl;//Init.memset(tree,0,sizeof(tree));memset(num,0,sizeof(num));dep=0;k=0;}return 0;}