uva 699

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map>

using namespace std;
const int INF = 0xffffff;
const double Pi = 4 * atan(1);

struct Node{
    int val;
    Node * right;
    Node * left;
};
int cnt;
int a[10000];
int arr[50000];
int n,ll,rr;
void b_build(Node * &tmp){
    if(n >= cnt)
        return;
    if(a[n] == -1)
        return;
    tmp = new Node;
    tmp->left = NULL;
    tmp->right = NULL;
    tmp->val = a[n];
    if(n+1 < cnt){
        ++n;
        b_build(tmp->left);
    }
    if(n+1 < cnt){
        ++n;
        b_build(tmp->right);
    }
}

void dfs(Node * node,int num){
    if(node == NULL)
        return;
    ll = min(ll,num);
    rr = max(rr,num);
    arr[num] += node->val;
    dfs(node->left,num-1);
    dfs(node->right,num+1);
    return;
}

int main()
{
   // freopen("inpt.txt","r",stdin);
    int cas = 0;
    while(1){
        int cntN = 0;
        int cntT = 0;
        int tmp;
        cnt = 0;
        cin >> tmp;
        if(tmp == -1)
            break;
        a[cnt++] = tmp;
        cntT++;
        while(cin >> tmp){
            if(tmp == -1){
                cntN++;
            }
            else{
                cntT++;
            }
            a[cnt++] = tmp;
            if(cntN == cntT + 1)
                break;
        }
        Node * root = NULL;
        n = 0;
        b_build(root);
        memset(arr,0,sizeof(arr));
        ll = 25000;
        rr = 25000;
        dfs(root,25000);
        cout << "Case " << ++cas << ":" << endl;
        for(int i = ll;i < rr;i++)
            cout << arr[i] << ' ';
        cout << arr[rr] << endl;
        cout << endl;
    }
    return 0;
}

uva699   前序遍历建树然后dfs深搜用数组标记偏移……2333333

很简单的水题,数组开小了,一直wa……!

posted @ 2015-01-13 17:15  寒饼干  阅读(184)  评论(0编辑  收藏  举报