码迷,mamicode.com
首页 > 其他好文 > 详细

[2016-02-09][UVA][699][The Falling Leaves]

时间:2016-02-09 15:52:11      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:

[2016-02-09][UVA][699][The Falling Leaves]
  • 时间:2016-02-09 13:29:10 星期二
  • 题目编号:UVA 699
  • 题目大意:
    •  给一棵树,每棵树有一个叶子,叶子的值是点权,求叶子垂直落下后,
    • (同一个方向的形成一堆),求每堆叶子的总权值
    • 位置的描述:每个左子树在根左边一个单位,右子树在根右边一个单位
  • 分析:
    • 遍历一遍二叉树,传参保存每个二叉树的位置,最后保存即可
    • 每行不超过80个字符,那么节点数不大于80个,堆数不大于80个
  • 方法:dfs,递归
  • 解题过程遇到问题:
    • 一行数据不一定就是一颗完整的树!!!!!!!
      • 开始还以为读取到第三行的-1就结束程序...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
const int maxn = 80 + 100;
int res[maxn];
void solve(int cur,int pos){
        res[pos] += cur;//add weight to res
        int lson,rson;
        scanf("%d",&lson);
        if(~lson) solve(lson,pos - 1);//lson
        scanf("%d",&rson);
        if(~rson) solve(rson,pos + 1);//rson
}
int ini(){
        int v;
        scanf("%d",&v);
        if(v == -1)     return 0;//the end of input
        CLR(res,0);
        solve(v,maxn/2);
        return 1;
}
void show_res(){
        int i = 0;
        while(!res[i])  ++i;
        printf("%d",res[i++]);
        while(res[i])   
                printf(" %d",res[i++]);
        puts("\n");  
}
int main(){
        int cntcase = 0;
        while(ini()){
                printf("Case %d:\n",++cntcase);
                show_res();
      }
    return 0;
}







[2016-02-09][UVA][699][The Falling Leaves]

标签:

原文地址:http://www.cnblogs.com/qhy285571052/p/5185558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!