G.Code the Tree

Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 18 [Submit][Status][Web Board]

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 new obtained tree, 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)))

Sample Output

5 2 5 2 6 2 8
2 3

HINT

 

Source

第七届河南省赛

题解:

是一个叫什么Prufer树的东西,这个树有一定规律,就是每次找树枝节点最小的那个数的父节点,去掉这个枝;队友提供了思路,想了想敲了敲,就ac了,由于数字可以读取多位,错了几次;

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
const int MAXN=1010;
char s[2010];
int mp[MAXN][MAXN];
int usd[MAXN];
int ans[MAXN];
int vis[MAXN];
int main(){
stack<int>S;
int t;
while(mem(s,0),gets(s)){
int mx=0;
mem(usd,0);
int len=strlen(s),temp=0;
for(int i=0;i<len;i++){
if(s[i]=='(')temp=1;
if(isdigit(s[i])){
if(temp){
t=0;
while(isdigit(s[i]))t=t*10+s[i]-'0',i++;
mx=max(mx,t);
if(!S.empty())mp[t][S.top()]=mp[S.top()][t]=1,usd[S.top()]++,usd[t]++;
S.push(t);
temp=0;
}
}
if(s[i]==')')if(!S.empty())S.pop();
}
int k=0;
mem(vis,0);
int num=mx;
for(int i=1;i<=mx;i++)usd[i]--;
while(true){
temp=INF;
for(int i=1;i<=mx;i++)
if(!vis[i])if(!usd[i])temp=min(i,temp);
if(temp==INF)break;
num--;
for(int i=1;i<=mx;i++){
if(!vis[i]&&mp[temp][i]){
ans[k++]=i;break;
}
}
vis[temp]=1;
for(int i=1;i<=mx;i++)
if(!vis[i]&&mp[temp][i])mp[temp][i]=mp[i][temp]=0,usd[i]--;
}
for(int i=0;i<k;i++){
if(i)printf(" ");
PI(ans[i]);
}puts("");
}
return 0;
}

  

最新文章

  1. Expert 诊断优化系列------------------内存不够用么?
  2. linux中test与[ ]指令的作用
  3. iOS block从零开始
  4. MySQL的mysql_insert_id和LAST_INSERT_ID(转)
  5. iOS应用之间跳转
  6. Mule ESB 社区版 企业版 资源下载 包含3.5和3.6
  7. java.sql.SQLException: 对只转发结果集的无效操作: last
  8. DBUtils开源JDBC类库,对JDBC简单封装(作用是:简化编码工作量,同时不会影响程序的性能)
  9. linux下使用tar命令(转)
  10. Mybatis 实现传入参数是表名
  11. 【Qt】Qt之自定义界面(添加自定义标题栏)【转】
  12. Android Sqlite 使用 注意事项
  13. [DevExpress]ChartControl之基准线示例
  14. [转] Linux抓包工具tcpdump详解
  15. yii2 日志(log)的配置与使用
  16. C primer plus 读书笔记第十一章
  17. 基于html5 canvas 的强大图表插件【Chart.js】
  18. Linux开机启动(bootstrap)上
  19. c# 语法要点速览
  20. TreeMap,HashMap,LinkedHashMap区别,很简单解释

热门文章

  1. Bower —— 一个Web的包管理工具
  2. php程序员的弱点
  3. codeforces 652D . Nested Segments 线段树
  4. GetMemory()函数
  5. POJ 2400 最小权匹配
  6. 【虚拟化实战】容灾设计之四VPLEX
  7. js笔记-DOM基础
  8. append与after
  9. Android中进行流量统计
  10. Objective-C中的block块语法