poj 2567 Code the Tree 河南第七届省赛
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2350 | Accepted: 906 |
Description
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
Output

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
AC:直接暴力枚举所有情况,每次都从从节点个数最小的为1的开始;
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #include<string>
- #include<cmath>
- using namespace std;
- char ss[];
- int a[][],num[];
- int ans[];
- int main()
- {
- int len;
- while(gets(ss))
- {
- memset(a,,sizeof(a));
- memset(ans,,sizeof(ans));
- memset(num,,sizeof(num));
- len = strlen(ss);
- int aa = ,i=,lev=,t=,mmax =,kk=;
- for(i = ; i < len; i++)
- {
- if(ss[i] == '(')
- lev++;
- else if(ss[i] == ')')
- lev--;
- if(ss[i] >= '' && ss[i] <= '')
- {
- t = t * + ss[i] - ;
- if(t > mmax)
- mmax = t;
- if(!(ss[i + ] >= '' && ss[i + ] <= ''))
- {
- num[lev] = t;
- a[num[lev - ]][num[lev]] = ;
- a[num[lev]][num[lev - ]] = ;
- }
- }
- else
- {
- t = ;
- }
- }
- int sum,j,k,m;
- for( i=;i<=mmax;i++)
- {
- for( j=;j<=mmax;j++)
- { sum = ;
- for(k=;k<=mmax;k++)//统计和他相连的数的个数
- {
- sum = sum+a[j][k];
- }
- if(sum == )
- {
- for(m = ;m<=mmax; m++)
- if(a[j][m] == )
- {
- ans[kk++] = m;
- a[j][m] = ;
- a[m][j] = ;
- j = ;
- break;
- }
- }
- }
- }
- for(int i=; i<kk; i++)
- if(i == )printf("%d",ans[i]);
- else printf(" %d",ans[i]);
- printf("\n");
- }
- return ;
- }
poj 2567 Code the Tree 河南第七届省赛的更多相关文章
- POJ 2567 Code the Tree & POJ 2568 Decode the Tree Prufer序列
题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于随意一个无根树,每次去掉一个编号最小的叶子节点,并 ...
- 第七届河南省赛G.Code the Tree(拓扑排序+模拟)
G.Code the Tree Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 18 [Submit][Status][Web ...
- 第七届河南省赛10403: D.山区修路(dp)
10403: D.山区修路 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 69 Solved: 23 [Submit][Status][Web Bo ...
- 第七届河南省赛10402: C.机器人(扩展欧几里德)
10402: C.机器人 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 53 Solved: 19 [Submit][Status][Web Boa ...
- 第七届河南省赛B.海岛争霸(并差集)
B.海岛争霸 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 130 Solved: 48 [Submit][Status][Web Board] D ...
- 第七届河南省赛A.物资调度(dfs)
10401: A.物资调度 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 95 Solved: 54 [Submit][Status][Web Bo ...
- 第七届河南省赛H.Rectangles(lis)
10396: H.Rectangles Time Limit: 2 Sec Memory Limit: 128 MB Submit: 229 Solved: 33 [Submit][Status] ...
- 第七届河南省赛F.Turing equation(模拟)
10399: F.Turing equation Time Limit: 1 Sec Memory Limit: 128 MB Submit: 151 Solved: 84 [Submit][St ...
- 算法笔记_122:蓝桥杯第七届省赛(Java语言A组)试题解答
目录 1 煤球数目 2 生日蜡烛 3 搭积木 4 分小组 5 抽签 6 寒假作业 7 剪邮票 8 取球博弈 9 交换瓶子 10 压缩变换 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路 ...
随机推荐
- C/C++笔试题(很多)
微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...
- C++ little errors , Big problem
---------------------------------------------------------------------------------------------------- ...
- dll--二进制层面的复用
积木式思想其实是很自然的一个过程,从c的库函数到C++的标准库,再到dll.com.com+都是这种思想推动下的结果,和现实生活中的人们的思维方式并无二致,只不过软件是在一个虚拟的世界中,并分化出许多 ...
- Java Debugging
1, https://visualvm.java.net/oqlhelp.html#top http://docs.oracle.com/javase/7/docs/technotes/samples ...
- 【转】Python numpy库的nonzero函数用法
当使用布尔数组直接作为下标对象或者元组下标对象中有布尔数组时,都相当于用nonzero()将布尔数组转换成一组整数数组,然后使用整数数组进行下标运算. nonzeros(a) 返回数组a中值不为零的元 ...
- 非root模式下安装mysql php小记
假设你的home目录为/home/work mysql-server 安装 1. 下载mysql.tar.gz wget http://dev.mysql.com/get/Downloads/MySQ ...
- oracle参数与启停
oracle随系统启动而启动 cs65-64桌面版orcle-11.2.0.4 启动监听器,后台进程,OEM. 注意: 如果只做一和三,只能启动后台进程,监听器不启动,如果只做二和三,只能启动监听器, ...
- 无法在web服务器上启动调试。Microsoft Visual Studio 远程调试监视器(MSVSMON.EXE)似乎没有在远程计算机上运行,VS2012调试错误
1.重启(无用) 2.关闭防火墙(无用) 3.开启文件与打印机共享(无用) 4.无远程调试权限,改为本地调试.或者是IIS中此项目没有启动.或者没有在IIS中新建此项目.
- python分割数组里面重复的元素
c=[1,1,1,1,2,2,2,3,3,4,4,4,4,4,5,5,5,] a = [] x = [] for i in range(0,len(c)): if i + 1 < len(c): ...
- html之head,base,meta,title
一个简单的HTML最基本的必须的元素 用于定义文档的头部,是所有头部元素的容器.头部描述了文档的各种属性和信息,绝大多数头部的数据都不会直接显示给读者. 下面这些标签可用在head部分:base,li ...