UVA 10410 - Tree Reconstruction(树)

UVA 10410 - Tree Reconstruction

题目链接

题意:给定一个树的dfs序列和bfs序列,求出这颗树

思路:拿dfs的序列,分成若干段,每一段相当一个子树,这样就可以利用bfs的序列去将dfs的序列分段,然后利用一个队列去存放每一段,不断求出子树即可。一开始以为parse tree一定是二叉树,原来不一定啊。

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const int N = 1005;

int n, b[N], d[N], now;
vector<int> g[N];

struct Seg {
    int l, r;
    Seg(){}
    Seg(int l, int r) {
	this->l = l;
	this->r = r;
    }
};

void bfs() {
    now = 1;
    queue<Seg> Q;
    Q.push(Seg(0, n));
    while (!Q.empty()) {
	Seg p = Q.front();
	Q.pop();
	int u = d[p.l];
	if (now == n || p.l + 1 == p.r) continue;
	int pre = p.l + 1;
	g[u].push_back(d[pre]);
	now++;
	for (int i = p.l + 2; i < p.r; i++) {
	    if (b[now] == d[i]) {
		g[u].push_back(d[i]);
		Q.push(Seg(pre, i));
		pre = i;
		now++;
	    }
	}
	if (pre < p.r)
	    Q.push(Seg(pre, p.r));
    }
}

int main() {
    while (~scanf("%d", &n)) {
	for (int i = 0; i < n; i++) {
	    g[i + 1].clear();
	    scanf("%d", &b[i]);
	}
	for (int i = 0; i < n; i++)
	    scanf("%d", &d[i]);
	bfs();
	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-08-27 07:23:50

UVA 10410 - Tree Reconstruction(树)的相关文章

uva 10410 - Tree Reconstruction(栈)

题目链接:uva 10410 - Tree Reconstruction 题目大意:给定一个树的BFS和DFS,求这棵树. 解题思路:用栈维护即可.对应BFS序列映射出了每个节点和根节点的距离,遍历dfs序列,对当前节点和栈顶节点比较,如果该节点距离根节点更远,则说明该节点为栈顶节点个孩子节点,则记录后将节点放入栈中.否则弹掉栈顶元素继续比较.需要注意一点,即当元素与栈顶元素的距离值大1的时候要视为相等,因为它们属于兄弟节点 #include <cstdio> #include <cst

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 - 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 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

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

UVA10410 - Tree Reconstruction(队列)

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

【构造题 贪心】cf1041E. Tree Reconstruction

比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from 11 to nn. For every edge ee of this tree, Monocarp has written two numbers: the ma

[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