QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3502|回复: 1
打印 上一主题 下一主题

ACM真题----Codethe Tree

[复制链接]
字体大小: 正常 放大

937

主题

117

听众

3万

积分

升级  0%

  • TA的每日心情

    2020-10-25 11:55
  • 签到天数: 264 天

    [LV.8]以坛为家I

    自我介绍
    内蒙古大学计算机学院

    社区QQ达人 金点子奖 助人为乐奖 风雨历程奖

    群组2013年数学建模国赛备

    跳转到指定楼层
    1#
    发表于 2013-8-8 12:00 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    Codethe TreeA 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.
    7 |& S1 Z7 L' \# `( ~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:
    1 S- z0 w' W: ], l7 D1 C$ Z! U  d# t2 h. N) b3 H
    T ::= "(" N S ")"( ]& M6 {& z# @5 r. z
    S ::= " " T S7 W' k3 g- s( j9 K0 C" n
    | empty
    , B- z1 x: r% A6 ^N ::= number
    ! I; U! e3 v9 r/ f, Y
    . ]! K9 K. u. r( l& [7 \1 bThat 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. 2 |3 R5 F, s- e6 y: j3 Q# W
    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".
    " G- ?! M' c& _# t2 F0 p: U: w5 t4 p, y! e

    , H7 R4 |3 ^5 t$ P8 \% g/ G
    " X5 m$ d0 q- V8 {2 ^Input Specification. J' g3 ]+ S( h" U3 I+ o9 F: ]
    1 j! }/ K: @2 e2 y. {3 b6 A
    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. & x- j1 r: s& n) m
    + A) d' M/ y( t
    Output Specification
    * R4 V+ G+ d& Z% C: [
    5 n: b  U& L' x  IFor 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.
    7 _  @# }& N. u  K
    ) q6 Z7 [( O# c  _; K) L6 DSample Input1 ~: ?  H$ j2 ]" ~: G" x
    (2 (6 (7)) (3) (5 (1) (4)) (8))9 `. t. W2 N) j. Y" K! H
    (1 (2 (3)))
    3 k& H& {* ~7 S: q' u(6 (1 (4)) (2 (3) (5)))
    2 K# W$ g' t2 P# U/ L& Q2 J( R) X
    6 F' B  e0 r5 W) `Sample Output. y/ B; A3 K( @0 v/ o$ U6 q
    5 2 5 2 6 2 8  n% B0 R) s) T/ p8 h
    2 3
    $ P- m6 m5 _9 ?( w  y: Z9 n' ~9 ]2 1 6 2 65 m2 ~0 ]* q0 V8 J/ \, u7 n% V. q! G
    zan
    转播转播 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信

    7

    主题

    9

    听众

    779

    积分

    升级  44.75%

  • TA的每日心情
    开心
    2016-5-19 15:19
  • 签到天数: 200 天

    [LV.7]常住居民III

    自我介绍
    我是一个勇于挑战自我的人。

    社区QQ达人 新人进步奖

    群组西安交大数学建模

    群组学术交流B

    群组第一期sas基础实训课堂

    群组认证人才交流群

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2024-4-19 13:53 , Processed in 0.370230 second(s), 56 queries .

    回顶部