zoukankan      html  css  js  c++  java
  • UVA

    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 Output

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

    题意:告诉你DFS和BFS的结果,求树

    思路:从DFS入手。每次枚举它之后的点有没有可能是它的子节点,推断从BFS推断,首先深度要是+1的,还有就是两个之间必须都要是訪问多的才行

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    using namespace std;
    const int MAXN = 1010;
    
    int n, ord[MAXN], dfs[MAXN], bfs[MAXN], deep[MAXN];
    vector<int> arr[MAXN];
    queue<int> q;
    int vis[MAXN];
    
    int check(int a, int b, int curDeep) {
    	int e = dfs[a];
    	int f = dfs[b];
    	int c = ord[e];
    	int d = ord[f];
    	for (int i = a+1; i < b; i++) {
    		int tmp = dfs[i];
    		if (deep[tmp] == 0)
    			continue;
    		if (deep[tmp] != curDeep)
    			return 0;
    	}
    	for (int i = c+1; i < d; i++) {
    		int tmp = bfs[i];
    		if (!vis[tmp])
    			return 0;
    	}
    	return 1;
    }
    
    void cal(int cur, int curDeep) {
    	int cnt = dfs[cur];
    	for (int i = cur+1; i < n; i++) {
    		int tmp = dfs[i];
    		if (vis[tmp])
    			continue;
    		if (check(cur, i, curDeep+1))
    			arr[cnt].push_back(tmp);
    		else continue;
    		q.push(i);
    		vis[tmp] = 1;
    		deep[tmp] = curDeep + 1;
    	}
    }
    
    void print() {
    	for (int i = 1; i <= n; i++) {
    		printf("%d:", i);
    		for (int j = 0; j < arr[i].size(); j++)
    			printf(" %d", arr[i][j]);
    		printf("
    ");
    	}
    }
    
    int main() {
    	while (scanf("%d", &n) != EOF) {
    		memset(vis, 0, sizeof(vis));
    		memset(deep, 0, sizeof(deep));
    		memset(ord, 0, sizeof(ord));
    		memset(dfs, 0, sizeof(dfs));
    		memset(bfs, 0, sizeof(bfs));
    		for (int i = 0; i <= n; i++)
    			arr[i].clear();
    		while (!q.empty())
    			q.pop();
    		for (int i = 0; i < n; i++) {
    			int t;
    			scanf("%d", &t);
    			bfs[i] = t;
    			ord[t] = i;
    		}
    		for (int i = 0; i < n; i++)
    			scanf("%d", &dfs[i]);
    		deep[dfs[0]] = 1;
    		q.push(0);
    		while (!q.empty()) {
    			int cnt = q.front();
    			q.pop();
    			cal(cnt, deep[dfs[cnt]]);
    		}
    		print();
    	}
    	return 0;
    }



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    YII 项目部署时, 显示空白内容
    github上传项目(使用git)、删除项目、添加协作者
    Mysql 报错:#1067
    解决:500 Internal Privoxy Error
    解决bcp导出CSV文件没有表头
    asp.net 获取当前,相对,绝对路径
    BCP 运行错误
    cmd 运行bcp 提示:'bcp' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
    Android--数据持久化之SQLite
    Android--JUnit单元测试
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4883750.html
Copyright © 2011-2022 走看看