📜  检查给定的Prufer序列是否有效

📅  最后修改于: 2021-05-04 17:10:06             🧑  作者: Mango

给定N个整数的Prufer序列,任务是检查给定序列是否为有效的Prufer序列。

例子:

Input: arr[] = {4, 1, 3, 4} 
Output: Valid 
The tree is:
2----4----3----1----5
     |
     6                 

Input: arr[] = {4, 1, 7, 4} 
Output: Invalid 

方法:由于我们知道Prufer序列的长度为N – 2 ,其中N是顶点的数量。因此,我们需要检查Prufer序列是否由[1,N]范围内的元素组成。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if
// given Prufer sequence is valid
bool isValidSeq(int a[], int n)
{
    int nodes = n + 2;
  
    // Iterate in the Prufer sequence
    for (int i = 0; i < n; i++) {
  
        // If out of range
        if (a[i] < 1 || a[i] > nodes)
            return false;
    }
  
    return true;
}
  
// Driver code
int main()
{
    int a[] = { 4, 1, 3, 4 };
    int n = sizeof(a) / sizeof(a[0]);
    if (isValidSeq(a, n))
        cout << "Valid";
    else
        cout << "Invalid";
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
  
// Function that returns true if
// given Prufer sequence is valid
static boolean isValidSeq(int []a, int n)
{
    int nodes = n + 2;
  
    // Iterate in the Prufer sequence
    for (int i = 0; i < n; i++) 
    {
  
        // If out of range
        if (a[i] < 1 || a[i] > nodes)
            return false;
    }
  
    return true;
}
  
// Driver code
public static void main (String[] args) 
{
    int a[] = { 4, 1, 3, 4 };
    int n = a.length;
    if (isValidSeq(a, n))
        System.out.println( "Valid");
    else
        System.out.print( "Invalid");
}
}
  
// This code is contributed by anuj_67..


Python3
# Python3 implementation of the approach 
  
# Function that returns true if 
# given Prufer sequence is valid 
def isValidSeq(a, n) : 
  
    nodes = n + 2; 
  
    # Iterate in the Prufer sequence 
    for i in range(n) :
  
        # If out of range 
        if (a[i] < 1 or a[i] > nodes) :
            return False; 
      
    return True; 
  
# Driver code 
if __name__ == "__main__" : 
  
    a = [ 4, 1, 3, 4 ]; 
      
    n = len(a); 
      
    if (isValidSeq(a, n)) :
        print("Valid"); 
    else :
        print("Invalid"); 
          
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG 
{
  
  
// Function that returns true if
// given Prufer sequence is valid
static Boolean isValidSeq(int []a, int n)
{
    int nodes = n + 2;
  
    // Iterate in the Prufer sequence
    for (int i = 0; i < n; i++) 
    {
  
        // If out of range
        if (a[i] < 1 || a[i] > nodes)
            return false;
    }
  
    return true;
}
  
// Driver code
public static void Main (String[] args) 
{
    int []a = { 4, 1, 3, 4 };
    int n = a.Length;
    if (isValidSeq(a, n))
        Console.WriteLine( "Valid");
    else
    Console.WriteLine( "Invalid");
}
}
  
// This code has been contributed by 29AjayKumar


输出:
Valid