原文:

给定一个,任务是找出普鲁弗序列所构成的树的所有节点的度。 例:

input: arr[] = {4, 1, 3, 4} 
output: 2 1 2 3 1 1
the tree is:
2----4----3----1----5
     |
     6 
input: arr[] = {1, 2, 2} 
output: 2 3 1 1 1

一种简单的方法是使用普鲁夫序列创建树,然后找到所有节点的度。 高效方法:创建一个大小为 2 的度【】数组,大于 prufer 序列的长度,因为如果 n 是节点数,那么 prufer 序列的长度就是n–2。首先,用 1 填充度数数组。迭代普鲁弗序列,增加每个元素在度表中的出现频率。这种方法之所以有效,是因为普鲁弗序列中一个节点的频率比树中的度数少一个。 以下是上述方法的实施:

c

// c   implementation of the approach
#include 
using namespace std;
// function to print the degrees of every
// node in the tree made by
// the given prufer sequence
void printdegree(int prufer[], int n)
{
    int node = n   2;
    // hash-table to mark the
    // degree of every node
    int degree[n   2   1];
    // initially let all the degrees be 1
    for (int i = 1; i <= node; i  )
        degree[i] = 1;
    // increase the count of the degree
    for (int i = 0; i < n; i  )
        degree[prufer[i]]  ;
    // print the degree of every node
    for (int i = 1; i <= node; i  ) {
        cout << degree[i] << " ";
    }
}
// driver code
int main()
{
    int a[] = { 4, 1, 3, 4 };
    int n = sizeof(a) / sizeof(a[0]);
    printdegree(a, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the approach
import java.util.*;
class gfg
{
    // function to print the degrees of every
    // node in the tree made by
    // the given prufer sequence
    static void printdegree(int prufer[], int n)
    {
        int node = n   2;
        // hash-table to mark the
        // degree of every node
        int[] degree = new int[n   2   1];
        // initially let all the degrees be 1
        for (int i = 1; i <= node; i  )
        {
            degree[i] = 1;
        }
        // increase the count of the degree
        for (int i = 0; i < n; i  )
        {
            degree[prufer[i]]  ;
        }
        // print the degree of every node
        for (int i = 1; i <= node; i  )
        {
            system.out.print(degree[i]   " ");
        }
    }
    // driver code
    public static void main(string[] args)
    {
        int a[] = {4, 1, 3, 4};
        int n = a.length;
        printdegree(a, n);
    }
}
/* this code contributed by princiraj1992 */

python 3

# python3 implementation of the approach
# function to print the degrees of
# every node in the tree made by
# the given prufer sequence
def printdegree(prufer, n):
    node = n   2
    # hash-table to mark the
    # degree of every node
    degree = [1] * (n   2   1)
    # increase the count of the degree
    for i in range(0, n):
        degree[prufer[i]]  = 1
    # print the degree of every node
    for i in range(1, node 1): 
        print(degree[i], end = " ")
# driver code
if __name__ == "__main__":
    a = [4, 1, 3, 4]
    n = len(a)
    printdegree(a, n)
# this code is contributed by rituraj jain

c

// c# implementation of the approach
using system;
class gfg
{
// function to print the degrees of every
// node in the tree made by
// the given prufer sequence
static void printdegree(int []prufer, int n)
{
    int node = n   2;
    // hash-table to mark the
    // degree of every node
    int[] degree = new int[n   2   1];
    // initially let all the degrees be 1
    for (int i = 1; i <= node; i  )
    {
        degree[i] = 1;
    }
    // increase the count of the degree
    for (int i = 0; i < n; i  )
    {
        degree[prufer[i]]  ;
    }
    // print the degree of every node
    for (int i = 1; i <= node; i  )
    {
        console.write(degree[i]   " ");
    }
}
// driver code
public static void main(string[] args)
{
    int []a = {4, 1, 3, 4};
    int n = a.length;
    printdegree(a, n);
}
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

2 1 2 3 1 1