Tree Reconstruction

Problem Description

You have just finished a compiler design homework question where you had to find the parse tree of an expression. Unfortunately you left your assignment in the library, but luckily your friend picked it up for you. Instead of e-mailing you the parse tree so that you can rewrite the solution, your friend decides to play a practical joke and sends you just the DFS and BFS trace. Rather than try to redo the entire question you decide to reconstruct the tree.

思路分析

首先这不是一道普通的根据BFS和DFS建树,因此我们需要分析这种树的遍历的性质。普通性质无需多说,这里只说必要的。

对于BFS:

同层的节点BFS编号顺序在DFS中也是按顺序出现的。根据这个我们可以一次遍历算出节点 的深度。

对于DFS:

一个节点的子孙肯定在他后面,在他同层相邻节点的前面。

对于一个节点的子孙,如果他们有的深度恰好等于这个节点的深度+1,那么这些子孙肯定是这个节点的儿子。

有了这些性质我们就可以递归建树了。

代码展现

    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<ctime>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<list>
    #include<deque>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<algorithm>
    //#pragma GCC optimize(2)
    using namespace std;
    #define ll long long
    const int maxn=1010;
    const int INF=0x7fffffff;
    inline void read(int&x){
        int data=0,w=1;
        char ch=getchar();
        while(ch!='-'&&!isdigit(ch))
            ch=getchar();
        if(ch=='-')
            w=-1,ch=getchar();
        while(isdigit(ch))
            data=10*data+ch-'0',ch=getchar();
        x=data*w;
    }
    void write(int x){
        if(x<0)
            putchar('-'),x=-x;
        if(x>9)
            write(x/10);
        putchar('0'+x%10);
    }
    struct vertex{
        int depth;
        int bfsrank;
        int dfsrank;
        list<int> edge;
        void clear(){
            depth=bfsrank=dfsrank=0;
            edge.clear();
        }
        void addedge(int v){
            edge.push_back(v);
        }
    }v[maxn];
    int bfsans[maxn],dfsans[maxn];
    #define root bfsans[1]
    void build(int o,int l,int r){
    //    clog<<"in o:"<<o<<" l:"<<l<<" r:"<<r<<endl;
        for(int i=l;i<=r;++i)
            if(v[dfsans[i]].depth==v[o].depth+1)
                v[o].addedge(dfsans[i]);
        if(v[o].edge.size()){
            list<int>::iterator i=v[o].edge.begin(),ed=v[o].edge.end();
            --ed;
            while(i!=ed){
                build(*(i++),v[*(--i)].dfsrank+1,v[*(++i)].dfsrank-1);
            }
            build(*ed,v[(*ed)].dfsrank+1,r);
        }
    }
    int main()
    {
    //  freopen(".in","r",stdin);
    //  freopen(".out","w",stdout);
        int n,whodep;
        while(cin>>n){
            for(int i=1;i<=n;++i)
                v[i].clear();
            for(int i=1;i<=n;++i){
                read(bfsans[i]);
                v[bfsans[i]].bfsrank=i;
            }
            for(int i=1;i<=n;++i){
                read(dfsans[i]);
                v[dfsans[i]].dfsrank=i;
            }
    /*        clog<<"dfsrank:"<<endl;
            for(int i=1;i<=n;i++)
                clog<<i<<": "<<v[i].dfsrank<<endl;*/
            v[root].depth=0;
            whodep=1;
            for(int i=2;i<=n;++i){
                if(v[bfsans[i]].dfsrank<v[bfsans[i-1]].dfsrank)
                    ++whodep;
                v[bfsans[i]].depth=whodep;
            }
    /*        clog<<"depths: "<<endl;
            for(int i=1;i<=n;++i)
                clog<<i<<": "<<v[i].depth<<endl;*/
            build(root,2,n);
            for(int i=1;i<=n;++i){
                write(i);putchar(':');putchar(' ');
                if(v[i].edge.size()){
                    list<int>::iterator j=v[i].edge.begin();
                    while(j!=v[i].edge.end()){
                        write(*j);putchar(' ');
                        ++j;
                    }
                }
                putchar('\n');
            }
        }
    //  fclose(stdin);
    //  fclose(stdout);
        return 0;
    }

认真看了的同学应该会注意到67行我的奇怪写法。为何不写成build(\*i,v[\*i].dfsrank+1,v[\*(++i)].dfsrank-1);呢?
这背后就是我写这篇博客的主要原因。上述代码是错的。

万恶之源是函数参数压栈顺序(CALLBACK),他是从右到左压栈的。这样的话上述代码问题就很严重了。我之前就是那样写的,想不到这种注意事项让我碰上了。卡了我半中午啊o(╥﹏╥)o。谁让 std::list::iterator 不支持+1操作呢?

教训惨重......

UVa 10410 树重建的更多相关文章

  1. UVa 10410树重建

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. UVA 536 TreeRocvery 树重建 (递归)

    根据先序历遍和中序历遍输出后序历遍,并不需要真的建树,直接递归解决 #include<cstdio> #include<cstring> ; char preOrder[N]; ...

  3. UVA - 10410 Tree Reconstruction (根据dfs序和bfs序恢复一颗树)

    题意: 分析: 这题一开始完全没有思路, 一直没有找出规律. 参考了http://www.cnblogs.com/Wade-/p/6358859.html 和 http://www.cnblogs.c ...

  4. 剑指offer 4.树 重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  5. UVa 112 树求和

    题意:给定一个数字,以及一个描写叙述树的字符序列,问存不存在一条从根到某叶子结点的路径使得其和等于那个数. 难点在于怎样处理字符序列.由于字符间可能有空格.换行等. 思路:本来想着用scanf的(后发 ...

  6. uva 12086 树状数组

    树状数组 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #i ...

  7. UVa 1220 (树的最大独立集) Party at Hali-Bula

    题意: 有一棵树,选出尽可能多的节点是的两两节点不相邻,即每个节点和他的子节点只能选一个.求符合方案的最大节点数,并最优方案判断是否唯一. 分析: d(u, 0)表示以u为根的子树中,不选u节点能得到 ...

  8. UVA 10410 Tree Reconstruction

    题意: 给定一个树的BFS和DFS,求这棵树. 分析: 拿dfs的序列,分成若干段,每一段相当一个子树,这样就可以利用bfs的序列去将dfs的序列分段,然后利用一个队列去存放每一段,不断求出子树即可. ...

  9. Uva 122 树的层次遍历 Trees on the level lrj白书 p149

    是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担 ...

随机推荐

  1. GetContent

    Sub GetContent(ByVal URL As String, ByVal SheetName As String) Dim strText As String Dim i As Long D ...

  2. 20170728xlVba简单的匹配

    Sub MatchData() Dim i As Long, EndRow As Long, Key As String Dim Rng As Range Dim Dic As Object Set ...

  3. Confluence 6 启用嵌套用户组

    一些目录服务器能够允许你在一个组中定义另外一个组.在这种结构下的用户组称为用户组嵌套.嵌套组的配置能够让子用户组继承上级用户组的权限,使系统的权限配置变得简单. 这个页面描述了 Confluence ...

  4. java MongoDB查询(一)简单查询

    前言 MongoDB的java驱动提供了查询的功能,查询条件也是bson对象,这篇就看下怎么进行简单的数据查询 1.数据结构 集合:firstCollection 数据内容: { "_id& ...

  5. 诡异的小bug 自动生成font标签包裹span标签中的文字

    某天测试自己写的网站的时候突然发现页面上一些文字排版出现了一些奇怪的错乱,在控制台发现错乱的文字被font标签包裹着 ,但是代码中根本没用用到font标签 后来发现是因为自己不小心点了谷歌浏览器地址栏 ...

  6. Java网络编程和NIO详解开篇:Java网络编程基础

    Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...

  7. datafile相关(add、rename、drop)

    --case 1 add14:25:04 FPYJ(150_9)@test> alter tablespace fpyj_data02 add datafile '/oradata02/test ...

  8. 一、final关键字

    final关键字修饰:类,方法,基本类型变量,引用,具有不同的意思 1.final修饰类 表示该类不能被继承 package property; public final class Hero ext ...

  9. POJ 2109 巧妙解法

    Int最大是10^9.所以一般思路是二分+高精度.但是double 范围是10^(-307)-10^308所以可以用double型.k^n=p.所以有k=p^(1/n). 见代码: #include& ...

  10. IOS多线程编程:概述

    什么是多线程 多线程是一个比较轻量级的方法来实现单个应用程序内多个代码执行路径.从技术角度来看,一个线程就是一个需要管理执行代码的内核级和应用级数据结构组合.内核级结构协助调度线程事件,并抢占式调度一 ...