uva 10410 - Tree Reconstruction(栈)

题目链接:uva 10410 - Tree Reconstruction

题目大意:给定一个树的BFS和DFS,求这棵树。

解题思路:用栈维护即可。对应BFS序列映射出了每个节点和根节点的距离,遍历dfs序列,对当前节点和栈顶节点比较,如果该节点距离根节点更远,则说明该节点为栈顶节点个孩子节点,则记录后将节点放入栈中。否则弹掉栈顶元素继续比较。需要注意一点,即当元素与栈顶元素的距离值大1的时候要视为相等,因为它们属于兄弟节点

#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 1005;

int N, pos[maxn];
vector<int> g[maxn];

int main () {
    while (scanf("%d", &N) == 1) {
        int x;
        for (int i = 1; i <= N; i++) {
            scanf("%d", &x);
            g[i].clear();
            pos[x] = i;
        }

        int root;
        stack<int> sta;
        scanf("%d", &root);
        sta.push(root);

        for (int i = 1; i < N; i++) {
            scanf("%d", &x);

            while (true) {
                int u = sta.top();

                if (u == root || pos[u] + 1 < pos[x]) {
                    g[u].push_back(x);
                    sta.push(x);
                    break;
                } else
                    sta.pop();
            }
        }

        for (int i = 1; i <= N; i++) {
            printf("%d:", i);
            for (int j = 0; j < g[i].size(); j++)
                printf(" %d", g[i][j]);
            printf("\n");
        }
    }
    return 0;
}
时间: 2024-10-14 04:27:11

uva 10410 - Tree Reconstruction(栈)的相关文章

UVA 10410 - Tree Reconstruction(树)

UVA 10410 - Tree Reconstruction 题目链接 题意:给定一个树的dfs序列和bfs序列,求出这颗树 思路:拿dfs的序列,分成若干段,每一段相当一个子树,这样就可以利用bfs的序列去将dfs的序列分段,然后利用一个队列去存放每一段,不断求出子树即可.一开始以为parse tree一定是二叉树,原来不一定啊. 代码: #include <cstdio> #include <cstring> #include <vector> #include

UVA - 10410 Tree Reconstruction

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 yo

UVA - 10410 Tree Reconstruction (树重建)

题意:给定一个树的bfs和dfs序列,升序输出每个结点的子结点列表. 分析:因为建树不唯一,假定若bfs[u] = bfs[v] + 1,则u是v的兄弟结点,否则是孩子结点.用栈维护. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #in

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

题意: 分析: 这题一开始完全没有思路, 一直没有找出规律. 参考了http://www.cnblogs.com/Wade-/p/6358859.html 和 http://www.cnblogs.com/jerryRey/p/4622927.html 在dfs序列中,相邻的两个结点u,v之间(dfs_pos(u) + 1 =  dfs_pos(v)),有父子,兄弟,其他3种关系. 父子关系:在bfs中u,v并不是相邻, bfs_pos(v) > bfs_pos(u) , 那么u为v父亲, v为

UVA - 112 - Tree Summing (数的求和!栈的应用!)

UVA - 112 Tree Summing Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest

UVa 10410 树重建

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

[2016-02-08][UVA][548][Tree]

UVA - 548 Tree Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root

UVA10410 - Tree Reconstruction(队列)

题目:UVA10410 - Tree Reconstruction(队列) 题目大意:给出一棵树的BFS和DFS遍历,求这棵数,如果有多种情况输出一种就可以了. 解题思路:利用BFS将DFS串分段,分成一棵一棵子树.然后将子树用队列存储下来,因为先被分出来的子树,在下一层中也是最先被分段.注意:一定要将根节点分离出去,它不属于它的子树.这棵树不一定是二叉树. 代码: #include <cstdio> #include <queue> #include <vector>

uva 112 - Tree Summing

 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, which are the fundamental data structures in LISP, can easily be adapted to represe