poj 2567 Code the Tree 【还原树 求prufer序列】
2016-03-04 17:21
344 查看
Code the Tree
Description
A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built as follows: the leaf (a vertex that is incident to only one edge) with the minimal number is taken. This leaf,
together with its incident edge is removed from the graph, while the number of the vertex that was adjacent to the leaf is written down. In the obtained graph, this procedure is repeated, until there is only one vertex left (which, by the way, always has number
n). The written down sequence of n-1 numbers is called the Prufer code of the tree.
Your task is, given a tree, to compute its Prufer code. The tree is denoted by a word of the language specified by the following grammar:
That is, trees have parentheses around them, and a number denoting the identifier of the root vertex, followed by arbitrarily many (maybe none) subtrees separated by a single space character. As an example, take a look at the tree in the figure below which
is denoted in the first line of the sample input. To generate further sample input, you may use your solution to Problem 2568.
Note that, according to the definition given above, the root of a tree may be a leaf as well. It is only for the ease of denotation that we designate some vertex to be the root. Usually, what we are dealing here with is called an "unrooted tree".
Input
The input contains several test cases. Each test case specifies a tree as described above on one line of the input file. Input is terminated by EOF. You may assume that 1<=n<=50.
Output
For each test case generate a single line containing the Prufer code of the specified tree. Separate numbers by a single space. Do not print any spaces at the end of the line.
Sample Input
Sample Output
题意:给你一个字符串表示一棵无根树。让你求出该树的purfer序列。
思路:递归建立树,然后求purfer序列。
AC代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2401 | Accepted: 923 |
A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built as follows: the leaf (a vertex that is incident to only one edge) with the minimal number is taken. This leaf,
together with its incident edge is removed from the graph, while the number of the vertex that was adjacent to the leaf is written down. In the obtained graph, this procedure is repeated, until there is only one vertex left (which, by the way, always has number
n). The written down sequence of n-1 numbers is called the Prufer code of the tree.
Your task is, given a tree, to compute its Prufer code. The tree is denoted by a word of the language specified by the following grammar:
T ::= "(" N S ")" S ::= " " T S | empty N ::= number
That is, trees have parentheses around them, and a number denoting the identifier of the root vertex, followed by arbitrarily many (maybe none) subtrees separated by a single space character. As an example, take a look at the tree in the figure below which
is denoted in the first line of the sample input. To generate further sample input, you may use your solution to Problem 2568.
Note that, according to the definition given above, the root of a tree may be a leaf as well. It is only for the ease of denotation that we designate some vertex to be the root. Usually, what we are dealing here with is called an "unrooted tree".
Input
The input contains several test cases. Each test case specifies a tree as described above on one line of the input file. Input is terminated by EOF. You may assume that 1<=n<=50.
Output
For each test case generate a single line containing the Prufer code of the specified tree. Separate numbers by a single space. Do not print any spaces at the end of the line.
Sample Input
(2 (6 (7)) (3) (5 (1) (4)) (8)) (1 (2 (3))) (6 (1 (4)) (2 (3) (5)))
Sample Output
5 2 5 2 6 2 8 2 3 2 1 6 2 6
题意:给你一个字符串表示一棵无根树。让你求出该树的purfer序列。
思路:递归建立树,然后求purfer序列。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <string> #include <vector> #include <queue> #include <stack> #define CLR(a, b) memset(a, (b), sizeof(a)) #define ll o<<1 #define rr o<<1|1 using namespace std; typedef long long LL; const int MOD = 1e9+7; const int MAXN = 1000+10; const int MAXM = 2*1e6+10; const int INF = 0x3f3f3f3f; void add(LL &x, LL y) {x += y; x %= MOD;} char str[MAXN]; int Map[60][60]; int n; int getval(int L) { int num = 0; for(int i = L+1; ; i++) { if(str[i] >= '0' && str[i] <= '9') num = num * 10 + (str[i] - '0'); else { break; } } n = max(n, num); return num; } int in[60]; void getMap(int L, int R) { if(R - L <= 3) return ; int root = getval(L); int cnt = 0; int pos; for(int i = L+1; i < R; i++) { if(str[i] == '(') { cnt++; if(cnt == 1) { pos = i; int val = getval(i); Map[root][val] = Map[val][root] = 1; in[root]++; in[val]++; } } else if(str[i] == ')') { cnt--; if(cnt == 0) getMap(pos, i); } } } int Find() { int leaf = 0, node = 0; for(int i = 1; i <= n; i++) { if(in[i] == 1) { leaf = i; for(int j = 1; j <= n; j++) { if(Map[i][j]) { node = j; break; } } } if(node) break; } in[leaf]--; in[node]--; Map[node][leaf] = Map[leaf][node] = 0; return node; } int main() { while(gets(str)) { int len = strlen(str); CLR(Map, 0); n = 0; getMap(0, len-1); queue<int> Q; for(int i = 1; i <= n-1; i++){ Q.push(Find()); } int cnt = 0; while(!Q.empty()) { if(cnt) printf(" "); printf("%d", Q.front()); Q.pop(); cnt++; } printf("\n"); } return 0; }
相关文章推荐
- AngularJS中的过滤器
- html5 canvas 绘制大转盘类似的游戏,并填充文
- Javascript进阶篇——浏览器对象—JavaScript计时器
- javascript 最简单对日期小时数进行增加
- back to top 回到顶部按钮 css+js
- css样式覆盖顺序
- 侧边导航栏css示例
- css实现table中td单元格鼠标悬浮时显示更多内容
- PhantomJs安装
- js控制TR的显示隐藏
- Jstorm 在Debian上的安装以及错误排查
- jsonp跨域请求
- 剑指offer学习笔记【二维数组的查找】
- js 解析 json
- angularjs 关于ui-router分层使用
- 【JavaScript 从零开始】表达式和运算符(1)
- HTML 讲师笔记--初次试讲
- node.js async详细教程
- JS入门
- 解决easyui和bootstrap兼容问题