📌  相关文章
📜  最小节点组数,使得同一组中不存在祖先

📅  最后修改于: 2022-05-13 01:57:19.224000             🧑  作者: Mango

最小节点组数,使得同一组中不存在祖先

给定一棵有N个节点的树。任务是形成最少数量的节点组,使得每个节点都完全属于一个组,并且其祖先都不在同一组中。给定每个节点的父节点(如果节点没有父节点,则为 -1)。
例子:

方法:可以通过将同一级别的节点分组在一起来组成组(一个节点及其任何祖先不能在同一级别)。因此,最小组数将是树的最大深度。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the depth of the tree
int findDepth(int x, vector child[])
{
    int mx = 0;
 
    // Find the maximum depth of all its children
    for (auto ch : child[x])
        mx = max(mx, findDepth(ch, child));
 
    // Add 1 for the depth of the current node
    return mx + 1;
}
 
// Function to return
// the minimum number of groups required
int minimumGroups(int n, int par[])
{
    vector child[n + 1];
 
    // For every node create a list of its children
    for (int i = 1; i <= n; i++)
        if (par[i] != -1)
            child[par[i]].push_back(i);
    int res = 0;
 
    for (int i = 1; i <= n; i++)
 
        // If the node is root
        // perform dfs starting with this node
        if (par[i] == -1)
            res = max(res, findDepth(i, child));
 
    return res;
}
 
// Driver code
main()
{
    int par[] = { 0, -1, 1, 1, 2, 2, 5, 6 };
    int n = sizeof(par) / sizeof(par[0]) - 1;
    cout << minimumGroups(n, par);
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
    // Function to return the depth of the tree
    static int findDepth(int x, Vector child[])
    {
        int mx = 0;
 
        // Find the maximum depth of all its children
        for (Integer ch : child[x])
            mx = Math.max(mx, findDepth(ch, child));
 
        // Add 1 for the depth of the current node
        return mx + 1;
    }
 
    // Function to return
    // the minimum number of groups required
    static int minimumGroups(int n, int par[])
    {
        Vector[] child = new Vector[n + 1];
        for (int i = 0; i <= n; i++)
        {
            child[i] = new Vector();
        }
         
        // For every node create a list of its children
        for (int i = 1; i <= n; i++)
            if (par[i] != -1)
                child[par[i]].add(i);
        int res = 0;
 
        for (int i = 1; i <= n; i++)
 
            // If the node is root
            // perform dfs starting with this node
            if (par[i] == -1)
                res = Math.max(res, findDepth(i, child));
 
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int par[] = { 0, -1, 1, 1, 2, 2, 5, 6 };
        int n = par.length - 1;
        System.out.print(minimumGroups(n, par));
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
 
# Function to return the depth of the tree
def findDepth(x, child):
    mx = 0
     
    # Find the maximum depth
    # of all its children
    for ch in child[x]:
        mx = max(mx, findDepth(ch, child))
         
    # Add 1 for the depth
    # of the current node
    return mx + 1
 
# Function to return the minimum 
# number of groups required
def minimumGroups(n, par):
    child = [[] for i in range(n + 1)]
     
    # For every node create a list
    # of its children
    for i in range(0, n):
        if (par[i] != -1):
            child[par[i]].append(i)
    res = 0
    for i in range(0, n):
         
        # If the node is root
        # perform dfs starting with this node
        if(par[i] == -1):
            res = max(res, findDepth(i, child))
    return res
 
# Driver Code
array = [0, -1, 1, 1, 2, 2, 5, 6]
print(minimumGroups(len(array), array))
 
# This code is contributed
# by SidharthPanicker


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to return the depth of the tree
    static int findDepth(int x, List []child)
    {
        int mx = 0;
 
        // Find the maximum depth of all its children
        foreach (int ch in child[x])
            mx = Math.Max(mx, findDepth(ch, child));
 
        // Add 1 for the depth of the current node
        return mx + 1;
    }
 
    // Function to return
    // the minimum number of groups required
    static int minimumGroups(int n, int []par)
    {
        List[] child = new List[n + 1];
        for (int i = 0; i <= n; i++)
        {
            child[i] = new List();
        }
         
        // For every node create a list of its children
        for (int i = 1; i <= n; i++)
            if (par[i] != -1)
                child[par[i]].Add(i);
        int res = 0;
 
        for (int i = 1; i <= n; i++)
 
            // If the node is root
            // perform dfs starting with this node
            if (par[i] == -1)
                res = Math.Max(res, findDepth(i, child));
 
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []par = { 0, -1, 1, 1, 2, 2, 5, 6 };
        int n = par.Length - 1;
        Console.Write(minimumGroups(n, par));
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
5