zoukankan      html  css  js  c++  java
  • poj 2567 Code the Tree 河南第七届省赛

    Code the Tree
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2350   Accepted: 906

    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:

    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
    AC:直接暴力枚举所有情况,每次都从从节点个数最小的为1的开始;
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<queue>
     6 #include<string>
     7 #include<cmath>
     8 using namespace std;
     9 char ss[1000];
    10 int a[100][100],num[100];
    11 int ans[100];
    12 int main()
    13 {
    14  int len;
    15  while(gets(ss))
    16  {
    17       memset(a,0,sizeof(a));
    18       memset(ans,0,sizeof(ans));
    19       memset(num,0,sizeof(num));
    20       len = strlen(ss);
    21       int aa = 0,i=0,lev=0,t=0,mmax =0,kk=0;
    22         for(i = 0; i < len; i++)
    23         {
    24             if(ss[i] == '(')
    25                 lev++;
    26             else if(ss[i] == ')')
    27                 lev--;
    28             if(ss[i] >= '0' && ss[i] <= '9')
    29             {
    30                 t = t * 10 + ss[i] - 48;
    31                 if(t > mmax)
    32                     mmax = t;
    33                 if(!(ss[i + 1] >= '0' && ss[i + 1] <= '9'))
    34                 {
    35                     num[lev] = t;
    36                     a[num[lev - 1]][num[lev]] = 1;
    37                     a[num[lev]][num[lev - 1]] = 1;
    38                 }
    39             }
    40             else
    41             {
    42                 t = 0;
    43             }
    44         }
    45       int sum,j,k,m;
    46       for( i=1;i<=mmax;i++)
    47       {
    48         for( j=1;j<=mmax;j++)
    49         {  sum = 0;
    50            for(k=1;k<=mmax;k++)//统计和他相连的数的个数
    51            {
    52                 sum = sum+a[j][k];
    53            }
    54             if(sum == 1)
    55              {
    56                for(m = 1;m<=mmax; m++)
    57                  if(a[j][m] == 1)
    58                  {
    59                    ans[kk++] = m;
    60                    a[j][m] = 0;
    61                    a[m][j] = 0;
    62                    j = 0 ;
    63                     break;
    64                  }
    65              }
    66         }
    67       }
    68       for(int i=0; i<kk; i++)
    69         if(i == 0)printf("%d",ans[i]);
    70       else printf(" %d",ans[i]);
    71       printf("
    ");
    72  }
    73   return 0;
    74 }
  • 相关阅读:
    2.6 CMMI2级——供应商协议管理(Supplier Agreement Management)
    使用boch仿真器在x86 PC平台上搭建Linux0.11系统环境(windows下)
    java实现登录验证码
    Hadoop DBOutputFormat的使用
    史蒂芬·金《肖申克的救赎》读后感
    Change Base
    IOS深入学习(20)之Object modeling
    android对话框(Dialog)的使用方法
    郝萌主的微信公众号上线了
    Dell shareplex 与HVR数据复制软件
  • 原文地址:https://www.cnblogs.com/lovychen/p/4479550.html
Copyright © 2011-2022 走看看