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 压缩变换 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路 ...
随机推荐
- Spring源码学习之:spring注解@Transactional
在分析深入分析@Transactional的使用之前,我们先回顾一下事务的一些基本内容. 事务的基本概念 先来回顾一下事务的基本概念和特性.数据库事务(Database Transaction) ,是 ...
- expect安装去测试
1.下载expect和tcl 下载地址:http://download.csdn.net/download/tobyaries/5754943 2.安装expect tar -zxvf tcl8.4. ...
- easyui datagrid 表格组件列属性formatter和styler使用方法
明确单元格DOM结构 要想弄清楚formatter和styler属性是怎么工作的,首先要弄清楚datagrid组件内容单元格的DOM接口,注意,这里指的是内容单元格,不包括标题单元格,标题单元格的结构 ...
- 【转】asp.net Cookie值中文乱码问题解决方法
来源:脚本之家.百度空间.网易博客 http://www.jb51.net/article/34055.htm http://hi.baidu.com/honfei http://tianminqia ...
- openJudge计算概论-谁考了第k名
/*===================================== 谁考了第k名 总时间限制: 1000ms 内存限制: 65536kB 描述 在一次考试中,每个学生的成绩都不相同,现知道 ...
- 类型参数约束 : Controller where T : class,new()
这是类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct | T必须是一个结构类型 where T : class | T必须是一个类(class)类型,不能是结构( ...
- 一个平时写程序通用的Makefile样例
//需要目标名和程序名字相同 .PHONY:clean all //伪目标 CC=gcc CFLAGS=-Wall -g BIN= //目标 all:$(BIN) %.o:%.c $(CC) $(CF ...
- 解决SQLite3数据库Error: database disk image is malformed
这种错误的提示一般都是数据库文件出现了问题,具体导致问题的原因不必深究,我们只讨论这种问题的饿解决方法: 比如数据库:test.db 这里还要分两种情况: 情况一: sqlite3 test.db & ...
- Linux From Scratch [2]
1. gcc需要的一些lib GMP:A free library for arbitrary precision arithmetic, operating on signed integers, ...
- DateGridView中添加下拉框列并实现数据绑定、更改背景色
1.添加下拉框 代码实现==> using System; using System.Collections.Generic; using System.Windows.Forms; names ...