zoukankan      html  css  js  c++  java
  • Code the Tree

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2292   Accepted: 878

    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
    难点是 字符串的分离, (A)根据这个特点,当读到‘('时,下一个一定是数字,我们遇到’)‘就结束。见代码

     1 #include"iostream"
     2 #include"cstdio"
     3 #include"map"
     4 #include"queue"
     5 #include"vector"
     6 #include"set"
     7 #include"cstring"
     8 #include"algorithm"
     9 using namespace std;
    10 void solve(vector< set<int> > &v,int p=0)
    11 {
    12     int x;
    13     cin>>x;
    14     if(p)
    15     {
    16         v[x].insert(p);
    17         v[p].insert(x);
    18     }
    19     while(1)
    20     {
    21         char ch;
    22         cin>>ch;
    23         if(ch==')')
    24             break;
    25         solve(v,x);
    26     }
    27     return ;
    28 }
    29 int main()
    30 {
    31     int i,j,n,k;
    32     char ch;
    33     while(cin>>ch)
    34     {
    35         vector< set<int> > vec(1024,set<int>());
    36         priority_queue<int,vector<int>,greater<int> > que;
    37         n=0;
    38         solve(vec);
    39         for(i=1;i<vec.size();i++)
    40         {
    41             if(vec[i].size())
    42             {
    43                 n++;
    44                 if(vec[i].size()==1)
    45                     que.push(i);
    46             }
    47         }
    48         for(i=1;i<n;i++)
    49         {
    50             int t=que.top();
    51             que.pop();
    52             int p=*vec[t].begin();
    53             if(i>1)
    54                 printf(" ");
    55             printf("%d",p);
    56             vec[p].erase(t);
    57             if(vec[p].size()==1)
    58                 que.push(p);
    59         }
    60         printf("
    ");
    61     }
    62     return 0;
    63 }
     1 #include"iostream"
     2 #include"cstdio"
     3 #include"map"
     4 #include"queue"
     5 #include"vector"
     6 #include"set"
     7 #include"cstring"
     8 #include"algorithm"
     9 using namespace std;
    10 void solve(map<int,set<int> > &m,int p=0)
    11 {
    12     int x;
    13     cin>>x;
    14     if(p)
    15     {
    16         m[p].insert(x);
    17         m[x].insert(p);
    18     }
    19     while(1)
    20     {
    21         char ch;
    22         cin>>ch;
    23         if(ch==')')
    24             break;
    25         solve(m,x);
    26     }
    27     return ;
    28 }
    29 int main()
    30 {
    31     int i,j,n,k;
    32     char ch;
    33     while(cin>>ch)
    34     {
    35         priority_queue<int,vector<int>,greater<int> > que;
    36         map<int,set<int> > mp;
    37         solve(mp);
    38         for(i=1;i<=mp.size();i++)
    39         {
    40             if(mp[i].size()==1)
    41             {
    42                     que.push(i);
    43             }
    44         }
    45         for(i=1;i<mp.size();i++)
    46         {
    47             int t=que.top();
    48             que.pop();
    49             int p=*mp[t].begin();
    50             if(i>1)
    51                 printf(" ");
    52             printf("%d",p);
    53             mp[p].erase(t);
    54             if(mp[p].size()==1)
    55                 que.push(p);
    56         }
    57         printf("
    ");
    58     }
    59     return 0;
    60 }


  • 相关阅读:
    HTML特殊字符编码对照表(备记)
    【java线程】锁机制
    java判断一个字符串是否为空,isEmpty和isBlank的区别
    对Java中properties类的理解
    使用redis的zset实现高效分页查询(附完整代码)
    ServiceStack.Redis高效封装和简易破解
    3.NetDh框架之缓存操作类和二次开发模式简单设计(附源码和示例代码)
    2.NetDh框架之简单高效的日志操作类(附源码和示例代码)
    1.NetDh框架之数据库操作层--Dapper简单封装,可支持多库实例、多种数据库类型等(附源码和示例代码)
    SQL Server索引原理解析
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4007059.html
Copyright © 2011-2022 走看看