📜  欧拉游览树中遍历的总节点

📅  最后修改于: 2021-04-17 08:55:24             🧑  作者: Mango

欧拉树巡回讨论已经讨论过,它将树的层次结构展平为精确包含2 * N-1值的数组。在这篇文章中,任务是证明Euler Tour Tree的度是节点数的2倍减1。这里的度数表示在Euler Tour中遍历的节点总数。

例子:

解释:

使用示例1

在哪里

C++
// C++ program to check the number of nodes
// in Euler Tour tree.
#include 
using namespace std;
  
#define MAX 1001
  
// Adjacency list representation of tree
vector adj[MAX];
  
// Function to add edges to tree
void add_edge(int u, int v)
{
    adj[u].push_back(v);
}
  
// Program to check if calculated Value is 
// equal to 2*size-1
void checkTotalNumberofNodes(int actualAnswer,
                              int size)
{
    int calculatedAnswer = size;
  
    // Add out-degree of each node 
    for (int i = 1; i <= size; i++)
        calculatedAnswer += adj[i].size();
  
    if (actualAnswer == calculatedAnswer)
        cout << "Calculated Answer is " << calculatedAnswer 
                     << " and is Equal to Actual Answer\n";
    else
        cout << "Calculated Answer is Incorrect\n";
}
int main()
{ // Constructing 1st tree from example
    int N = 8;
    add_edge(1, 2);
    add_edge(1, 3);
    add_edge(2, 4);
    add_edge(2, 5);
    add_edge(3, 6);
    add_edge(3, 7);
    add_edge(6, 8);
  
    // Out_deg[node[i]] is equal to adj[i].size()
    checkTotalNumberofNodes(2 * N - 1, N);
  
    // clear previous stored tree
    for (int i = 1; i <= N; i++) 
        adj[i].clear();
  
    // Constructing 2nd tree from example
    N = 9;
    add_edge(1, 2);
    add_edge(1, 3);
    add_edge(2, 4);
    add_edge(2, 5);
    add_edge(2, 6);
    add_edge(3, 9);
    add_edge(5, 7);
    add_edge(5, 8);
  
    // Out_deg[node[i]] is equal to adj[i].size()
    checkTotalNumberofNodes(2 * N - 1, N);
  
    return 0;
}


Java
// Java program to check the number of nodes
// in Euler Tour tree.
import java.util.*;
  
class GFG 
{
    static final int MAX = 1001;
  
    // Adjacency list representation of tree
    static Vector[] adj = new Vector[MAX];
  
    // Function to add edges to tree
    static void add_edge(int u, int v) 
    {
        adj[u].add(v);
    }
  
    // Program to check if calculated Value is
    // equal to 2*size-1
    static void checkTotalNumberofNodes(int actualAnswer, 
                                        int size) 
    {
        int calculatedAnswer = size;
  
        // Add out-degree of each node
        for (int i = 1; i <= size; i++)
            calculatedAnswer += adj[i].size();
  
        if (actualAnswer == calculatedAnswer)
            System.out.print("Calculated Answer is " + 
                                    calculatedAnswer + 
                  " and is Equal to Actual Answer\n");
        else
            System.out.print("Calculated Answer is Incorrect\n");
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        for (int i = 0; i < MAX; i++)
            adj[i] = new Vector();
              
        // Constructing 1st tree from example
        int N = 8;
        add_edge(1, 2);
        add_edge(1, 3);
        add_edge(2, 4);
        add_edge(2, 5);
        add_edge(3, 6);
        add_edge(3, 7);
        add_edge(6, 8);
  
        // Out_deg[node[i]] is equal to adj[i].size()
        checkTotalNumberofNodes(2 * N - 1, N);
  
        // clear previous stored tree
        for (int i = 1; i <= N; i++)
            adj[i].clear();
  
        // Constructing 2nd tree from example
        N = 9;
        add_edge(1, 2);
        add_edge(1, 3);
        add_edge(2, 4);
        add_edge(2, 5);
        add_edge(2, 6);
        add_edge(3, 9);
        add_edge(5, 7);
        add_edge(5, 8);
  
        // Out_deg[node[i]] is equal to adj[i].size()
        checkTotalNumberofNodes(2 * N - 1, N);
    }
}
  
// This code is contributed by Rajput-Ji


C#
// C# program to check the number 
// of nodes in Euler Tour tree.
using System;
using System.Collections.Generic;
  
class GFG 
{
    static readonly int MAX = 1001;
  
    // Adjacency list representation of tree
    static List[] adj = new List[MAX];
  
    // Function to add edges to tree
    static void add_edge(int u, int v) 
    {
        adj[u].Add(v);
    }
  
    // Program to check if calculated Value is
    // equal to 2*size-1
    static void checkTotalNumberofNodes(int actualAnswer, 
                                        int size) 
    {
        int calculatedAnswer = size;
  
        // Add out-degree of each node
        for (int i = 1; i <= size; i++)
            calculatedAnswer += adj[i].Count;
  
        if (actualAnswer == calculatedAnswer)
            Console.Write("Calculated Answer is " + 
                                 calculatedAnswer + 
               " and is Equal to Actual Answer\n");
        else
            Console.Write("Calculated Answer " + 
                              "is Incorrect\n");
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        for (int i = 0; i < MAX; i++)
            adj[i] = new List();
              
        // Constructing 1st tree from example
        int N = 8;
        add_edge(1, 2);
        add_edge(1, 3);
        add_edge(2, 4);
        add_edge(2, 5);
        add_edge(3, 6);
        add_edge(3, 7);
        add_edge(6, 8);
  
        // Out_deg[node[i]] is equal to adj[i].Count
        checkTotalNumberofNodes(2 * N - 1, N);
  
        // clear previous stored tree
        for (int i = 1; i <= N; i++)
            adj[i].Clear();
  
        // Constructing 2nd tree from example
        N = 9;
        add_edge(1, 2);
        add_edge(1, 3);
        add_edge(2, 4);
        add_edge(2, 5);
        add_edge(2, 6);
        add_edge(3, 9);
        add_edge(5, 7);
        add_edge(5, 8);
  
        // Out_deg[node[i]] is equal to adj[i].Count
        checkTotalNumberofNodes(2 * N - 1, N);
    }
}
  
// This code is contributed by PrinciRaj1992


输出:

Calculated Answer is 15 and is Equal to Actual Answer
Calculated Answer is 17 and is Equal to Actual Answer