nyoj1254 - Code the Tree

26 篇文章 0 订阅
4 篇文章 0 订阅

Code the Tree

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述

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".


输入
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
输出
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)))
样例输出
5 2 5 2 6 2 82 3
来源
第七届河南省程序设计大赛
上传者
516108736

分析:

难在题目的理解和字符的处理

题意:

根据字符生成一个图(看成图而不是树更好理解吧),然后把只有一条边的最小顶点对应的父结点输出出来,删除点和边。


比如样例2,

点1和点3都只连了1条边,点1,较小,输出2,删除点1和对应边

此时点2,和3都只连了1条边,点2,较小,输出3,删除点2和对应边

此时只剩下一个点,满足“this procedure is repeated, until there is only one vertex left”,结束。

字符处理:

当遇到数字时,入栈,当遇到'('和' '时,不用管,当遇到')'时

出栈一个元素,把它和栈顶元素建立关系,注意,最后一个')'是多余的,循环时循环到i<len-1即可,不然会使得空栈做出栈操作

然后就是vector的使用了,各种方法记起来~

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<vector>
using namespace std;

int main(){
    string s;
    int m1,m2;
    while(getline(cin,s)){
        int n=0;
        vector<int> rel[55];
        stack<int> shu;
        int len=s.length();

        for(int i=0;i<len-1;){
            if(s[i]<='9'&&s[i]>='0'&&i<len){
                int t1=0,t2=10;
                while(s[i]<='9'&&s[i]>='0'&&i<len){
                    t1=t1*t2+s[i]-'0';
                    i++;
                }
                n++;
                shu.push(t1);
            }
            if(s[i]==' '||s[i]=='(')i++;
            if(s[i]==')'){
                m1=shu.top();shu.pop();
                m2=shu.top();
                rel[m1].push_back(m2);
                rel[m2].push_back(m1);
                i++;
            }
        }
        for(int i=1;i<=n;i++){
            sort(rel[i].begin(),rel[i].end());
        }
        int q=n-2,t1;
        for(int i=1;i<=n;i++){
            if(rel[i].size()==1){
                t1=rel[i][0];
                printf("%d",t1);
                rel[i].resize(0);
                vector<int>::iterator iter;
                iter=lower_bound(rel[t1].begin(),rel[t1].end(),i);
                rel[t1].erase(iter);
                break;
            }
        }

        while(q--){//每次删除一个叶子结点
            for(int i=1;i<=n;i++){
                if(rel[i].size()==1){
                    t1=rel[i][0];
                    printf(" %d",t1);
                    rel[i].resize(0);
                    vector<int>::iterator iter;
                    iter=lower_bound(rel[t1].begin(),rel[t1].end(),i);
                    rel[t1].erase(iter);
                    break;
                }
            }
        }
        printf("\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值