UVA10410-Tree Reconstruction(BFS序和DFS序的性质)

Problem UVA10410-Tree Reconstruction

Accept:708  Submit:4330

Time Limit: 3000 mSec

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.

 

 Input

The input file contains several test cases as described below.

The first line of a input is the number n (1 <= n <= 1000) of nodes in the tree. The nodes in the tree are numbered 1, 2, ..., n. The remaining numbers are the BFS traversal followed by the DFS traversal. Note that when a parent was expanded the children were traversed in ascending order.

 

 Output

The output for each case should consist of n lines, one for each node. Each line should start with the node number followed by a colon followed by a list of children in ascending order. If there is more than one solution, any correct answer is acceptable.

 

 Sample Input

8
4 3 5 1 2 8 7 6
4 3 1 7 2 6 5 8
 
 

 Sample Ouput

1: 7
2: 6
3: 1 2
4: 3 5
5: 8
6:
7:
8:

 

题解:一道好题!我自己模拟的时候思路很凌乱,始终找不到一个可以实现的算法,翻看了很多题解,发现有很多题解和代码都是错的,写的比较好的博客链接如下:

http://www.cnblogs.com/jerryRey/p/4622927.html

我们先来列举两条性质:

1.在BFS序中,与点a相邻的下一个点可能有三种身份:(1)节点a的孩子 (2)节点a的第一后兄弟 (3)节点a的兄弟的孩子

2.在DFS序中,与点a相邻的下一个点可能有三种身份:(1)节点a的孩子(2)节点a的第一后兄弟(3)啥也不是(意思是说直接回到父辈及以上了)

用这两条性质我们来推出一个重要结论,设节点u,v,他们的BFS序分别设为bfs(u),bfs(v).

设这两个点在DFS序中是相邻的并且v是u的下一个节点。

结论:如果bfs(v) = bfs(u)+1 并且v>u那么,v必定可以视为u的第一后兄弟。

关键字是视为。这是相当于u,v在两个序列里都相邻,对比上文所说的三种身份,三种身份只剩两种了。如果v是u的孩子,由于二者在BFS序中相邻,因此和u在同一层的节点都遍历到了,如果u的同一层还有别的节点,那么v一定是u前面的兄弟的孩子,而此时v时u的孩子,因此,u所在的那一层只有u一个,我们把v及其子树提上来,让v变成u的后兄弟,显然不改变BFS序和DFS序,因此就证明了刚才的结论。

有了这个结论就好办多了,如果bfs(v) > bfs(u)+1那么它不可能是u的后兄弟,只能是u的孩子,证明很简单,对比三种身份,v不能是节点u的第一后兄弟,因为他们的BFS序不相邻,v不可能啥也不是,因为v的BFS序在u后面,如果v回到u的父辈及以上了,它必定出现早于u。如果bfs(v) < bfs(u)那就只能是啥也不是的情况了,这个时候怎么办,在DFS序中v回到了u的父辈及以上。那就代表着u及其子树被处理完了,忽略u就好,具体实现时弹栈就好。这里并没有把所有情况都涵盖,但是简单思考就知道那些情况对于一棵合法的树来说是不可能的。

还要注意一个细节就是栈顶元素时root时,直接加孩子,入栈。

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <stack>
 7 using namespace std;
 8 
 9 const int maxn = 1000+10;
10 int bfs_order[maxn];
11 vector<int> child[maxn];
12 
13 int main()
14 {
15     //freopen("input.txt","r",stdin);
16     int n;
17     while(~scanf("%d",&n)){
18         int x;
19         for(int i = 1;i <= n;i++){
20             child[i].clear();
21             scanf("%d",&x);
22             bfs_order[x] = i;
23         }
24         stack<int> sta;
25         int root;
26         scanf("%d",&root);
27         sta.push(root);
28         for(int i = 1;i < n;i++){
29             scanf("%d",&x);
30             while(true){
31             int temp = sta.top();
32                 if((bfs_order[x]>bfs_order[temp]+1) || (bfs_order[x]==bfs_order[temp]+1 && temp>x) || (temp==root)){
33                     child[temp].push_back(x);
34                     sta.push(x);
35                     break;
36                 }
37                 else sta.pop();
38             }
39         }
40         for(int i = 1; i <= n; i++) {
41             printf("%d:",i);
42             for(int j = 0;j < child[i].size();j++){
43                 printf(" %d",child[i][j]);
44             }
45             printf("\n");
46         }
47     }
48     return 0;
49 }

 

posted on 2018-08-24 21:00  随缘&不屈  阅读(845)  评论(0编辑  收藏  举报

导航