G.Code the Tree

本文介绍了一种根据给定的树形结构生成普费尔编码的算法实现过程。通过解析特定语法描述的树结构,逐步移除叶子节点并记录其相邻节点编号,最终形成普费尔编码。此算法适用于理解树形结构数据处理及编码生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



主要是题意,了解题意就很简单,给出一组广义表,得到图形表示,每次去掉只有一条边相连的点(值最小的那一个)输出和它相连的那个点并将这条边去掉

与2相连的最开始有6,3,5,4,8
与6相连有2,7
与3相连只有2
与5相连有1,2,4
与1相连只有5,
与4相连只有5,
与7相连只有6
与8相连只有2
所以最后答案 5 2 5 2 6 2 8
#include<bits/stdc++.h>
using namespace std;
int main()
{
    char st[10000];
    while(gets(st)!=NULL)
    {
        stack<int>qi;
        vector<int>a[55];
        int ma=-1;
        for(int i=0;i<strlen(st);i++)
        {
            if(st[i]=='(')//判断是否是边的其中一端
            {
                int s=0;
                i++;
                while(st[i]>='0'&&st[i]<='9')
                {
                    s=s*10+st[i]-'0';
                    i++;
                }
                ma=max(ma,s);//确定长度
                if(!qi.empty())//判断是否已经有一个端点
                {
                    int l=qi.top();
                    a[l].push_back(s);
                    a[s].push_back(l);
                }
                qi.push(s);端点入栈
                i--;
            }
            if(st[i]==')')//该端点的所有边结束出栈
                qi.pop();
        }
        int b[55];
        int t=0;
        for(int i=1;i<ma;i++)
        {
            for(int j=1;j<=ma;j++)
            {
                if(a[j].size()==1)
                {
                    int sh=a[j][0];
                    b[t]=a[j][0];
                    t++;
                    for(int k=0;k<a[sh].size();k++)
                        if(a[sh][k]==j)//取端点,去边
                        {
                            int l=a[sh][k];
                           a[sh][k]=a[sh][a[sh].size()-1];
                           a[sh][a[sh].size()-1]=l;
                           a[sh].pop_back();
                        }
                    a[j].pop_back();
                    break;
                }
            }
        }
        printf("%d",b[0]);
        for(int i=1;i<t;i++)
        {
            printf(" %d",b[i]);
        }
        printf("\n");
    }
 
}

Problem Statement Given is a weighted undirected connected graph G with N vertices and M edges, which may contain self-loops and multi-edges. The vertices are labeled as Vertex 1, Vertex 2, …, Vertex N. The edges are labeled as Edge 1, Edge 2, …, Edge M. Edge i connects Vertex a i ​ and Vertex b i ​ and has a weight of c i ​ . Here, for every pair of integers (i,j) such that 1≤i<j≤M, c i ​  =c j ​ holds. Process the Q queries explained below. The i-th query gives a triple of integers (u i ​ ,v i ​ ,w i ​ ). Here, for every integer j such that 1≤j≤M, w i ​  =c j ​ holds. Let e i ​ be an undirected edge that connects Vertex u i ​ and Vertex v i ​ and has a weight of w i ​ . Consider the graph G i ​ obtained by adding e i ​ to G. It can be proved that the minimum spanning tree T i ​ of G i ​ is uniquely determined. Does T i ​ contain e i ​ ? Print the answer as Yes or No. Note that the queries do not change T. In other words, even though Query i considers the graph obtained by adding e i ​ to G, the G in other queries does not have e i ​ . What is minimum spanning tree? The spanning tree of G is a tree with all of the vertices in G and some of the edges in G. The minimum spanning tree of G is the tree with the minimum total weight of edges among the spanning trees of G. Constraints 2≤N≤2×10 5 N−1≤M≤2×10 5 1≤a i ​ ≤N (1≤i≤M) 1≤b i ​ ≤N (1≤i≤M) 1≤c i ​ ≤10 9 (1≤i≤M) c i ​  =c j ​ (1≤i<j≤M) The graph G is connected. 1≤Q≤2×10 5 1≤u i ​ ≤N (1≤i≤Q) 1≤v i ​ ≤N (1≤i≤Q) 1≤w i ​ ≤10 9 (1≤i≤Q) w i ​  =c j ​ (1≤i≤Q,1≤j≤M) All values in input are integers.c++code
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值