UVA 548 Tree

这道题不算难,跟UVA 536 Tree Recovery这道题类似,但输入略麻烦。

给出中序遍历和后续遍历,求从根到叶节点的路径上所有节点权值之和的最小值的叶节点的值,如果有多个,就输出叶节点最小的那个。

输入的时候一个字符一个字符的读就行啦,分别处理EOF, ‘ ’,和‘\n’。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<set>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 100000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 11111;
const int M = 33;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

struct tree
{
    int num;
    tree *lson, *rson;
}root;

int ans, m;
int a1[N], a2[N];

void build(tree *p, int a, int b, int c)
{
    p -> num = a2[a];
    p -> lson = p -> rson = NULL;
    if(c - b == 1)return;
    FF(i, b, c)if(a2[a] == a1[i])
    {
        if(i > b)
        {
            tree *t = new tree();
            p -> lson = t;
            build(t, a - c + i, b, i);
        }
        if(i < c - 1)
        {
            tree *t = new tree();
            p -> rson = t;
            build(t, a - 1, i + 1, c);
        }
        return;
    }
}

void find(tree *p, int sum)
{
    if(p -> lson != NULL)find(p -> lson, sum + p -> num);
    if(p -> rson != NULL)find(p -> rson, sum + p -> num);
    if(p -> lson == NULL && p -> rson == NULL)
    {
        int q = sum + p -> num;
        if(m > q)m = q, ans = p -> num;
        else if(m == q && ans > p -> num)ans = p -> num;
        return;
    }
}

void del(tree *p)
{
    if(p -> lson != NULL)del(p -> lson);
    if(p -> rson != NULL)del(p -> rson);
    if(p != &root)delete p;
}

int readInt(int &x)
{
    char c;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
    {
        if(c == EOF)return -1;
        if(c == '\n')return 0;
    }
    for (x = c - '0', c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    return c == ' ';
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n, t;
    while(1)
    {
        n = 1;
        t = readInt(a1[0]);
        if(t == -1)break;
        if(t)while(readInt(a1[n++]));
        REP(i, n)RI(a2[i]);
        m = INFI;
        build(&root, n - 1, 0, n);
        find(&root, 0);
        del(&root);
        PI(ans);
        getchar();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值