zoukankan      html  css  js  c++  java
  • 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……!

  • 相关阅读:
    jupyter安装出现问题:安装后无法打开
    GitHub上传文件问题总结
    GitHub上传文件夹
    ELK 搭建
    mysql 开放远程连接权限连不上
    mysql linux下安装
    多个springboot项目部署到tomcat,Error deploying web application archive
    mysql 新增时,唯一索引冲突时更新
    日期计算
    mysql 忘记密码
  • 原文地址:https://www.cnblogs.com/hanbinggan/p/4221852.html
Copyright © 2011-2022 走看看