📜  为树的所有边缘着色所需的最短时间

📅  最后修改于: 2021-10-26 06:39:20             🧑  作者: Mango

给定一组对Edges[][] ,代表连接由N 个节点组成的树中的顶点的边,任务是基于对边着色需要1的假设,找到为树的所有边着色所需的最短时间时间单位。

注意:多条边可以在特定时刻着色,但一个节点只能是在特定日期着色的一条边的一部分。

例子

方法:这个问题可以使用DFS(深度优先搜索)来解决。请按照以下步骤解决问题:

  • 初始化的全局变量,说ANS0,存储的最短时间颜色的树的所有边缘必需的。
  • 将变量current_time初始化为0,以存储为当前边着色所需的时间。
  • 迭代当前节点的子节点并执行以下步骤:
    • 如果当前边未被访问,即当前节点不等于父节点:
      • current_time增加1
      • 检查父边缘是否同时着色。如果发现为真,则将current_time增加1,因为节点不能是同时着色的多个边的一部分。
      • ans更新为 anscurrent_time 的最大值
      • 调用递归 当前节点的子节点的函数minTimeToColor
  • 在这个函数结束后,打印ans

下面是上述方法的代码。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Stores the required answer
int ans = 0;
 
// Stores the graph
vector edges[100000];
 
// Function to add edges
void Add_edge(int u, int v)
{
    edges[u].push_back(v);
    edges[v].push_back(u);
}
 
// Function to calculate the minimum time
// required to color all the edges of a tre
void minTimeToColor(int node, int parent,
                    int arrival_time)
{
    // Starting from time = 0,
    // for all the child edges
    int current_time = 0;
 
    for (auto x : edges[node]) {
 
        // If the edge is not visited yet.
        if (x != parent) {
 
            // Time of coloring of
            // the current edge
            ++current_time;
 
            // If the parent edge has
            // been colored at the same time
            if (current_time == arrival_time)
                ++current_time;
 
            // Update the maximum time
            ans = max(ans, current_time);
 
            // Recurisvely call the
            // function to its child node
            minTimeToColor(x, node, current_time);
        }
    }
}
 
// Driver Code
int main()
{
 
    pair A[] = { { 1, 2 },
                           { 2, 3 },
                           { 3, 4 } };
 
    for (auto i : A) {
 
        Add_edge(i.first, i.second);
    }
 
    // Function call
    minTimeToColor(1, -1, 0);
 
    // Finally, print the answer
    cout << ans << "\n";
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Stores the required answer
static int ans = 0;
 
// Stores the graph
@SuppressWarnings("unchecked")
static Vector edges[] = new Vector[100000];
 
// Function to add edges
static void Add_edge(int u, int v)
{
    edges[u].add(v);
    edges[v].add(u);
}
 
// Function to calculate the minimum time
// required to color all the edges of a tre
static void minTimeToColor(int node, int parent,
                           int arrival_time)
{
     
    // Starting from time = 0,
    // for all the child edges
    int current_time = 0;
 
    for(int x = 0; x < edges[node].size(); x++)
    {
         
        // If the edge is not visited yet.
        if (edges[node].get(x) != parent)
        {
             
            // Time of coloring of
            // the current edge
            ++current_time;
 
            // If the parent edge has
            // been colored at the same time
            if (current_time == arrival_time)
                ++current_time;
 
            // Update the maximum time
            ans = Math.max(ans, current_time);
 
            // Recurisvely call the
            // function to its child node
            minTimeToColor(edges[node].get(x), node,
                           current_time);
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    for(int i = 0; i < edges.length; i++)
        edges[i] = new Vector();
         
    int A[][] = { { 1, 2 },
                  { 2, 3 },
                  { 3, 4 } };
 
    for(int i = 0; i < 3; i++)
    {
        Add_edge(A[i][0], A[i][1]);
    }
     
    // Function call
    minTimeToColor(1, -1, 0);
 
    // Finally, print the answer
    System.out.print(ans + "\n");
}
}
 
// This code is contributed by umadevi9616


Python3
# Python3 program for the above approach
 
# Stores the required answer
ans = 0
 
# Stores the graph
edges = [[] for i in range(100000)]
 
# Function to add edges
def Add_edge(u, v):
     
    global edges
    edges[u].append(v)
    edges[v].append(u)
 
# Function to calculate the minimum time
# required to color all the edges of a tre
def minTimeToColor(node, parent, arrival_time):
     
    global ans
     
    # Starting from time = 0,
    # for all the child edges
    current_time = 0
 
    for x in edges[node]:
 
        # If the edge is not visited yet.
        if (x != parent):
 
            # Time of coloring of
            # the current edge
            current_time += 1
 
            # If the parent edge has
            # been colored at the same time
            if (current_time == arrival_time):
                current_time += 1
 
            # Update the maximum time
            ans = max(ans, current_time)
 
            # Recurisvely call the
            # function to its child node
            minTimeToColor(x, node, current_time)
 
# Driver Code
if __name__ == '__main__':
     
    A = [ [ 1, 2 ],
          [ 2, 3 ],
          [ 3, 4 ] ]
 
    for i in A:
        Add_edge(i[0], i[1])
 
    # Function call
    minTimeToColor(1, -1, 0)
 
    # Finally, print the answer
    print(ans)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Stores the required answer
    static int ans = 0;
      
    // Stores the graph
    static List> edges = new List>();
      
    // Function to add edges
    static void Add_edge(int u, int v)
    {
        edges[u].Add(v);
        edges[v].Add(u);
    }
     
    // Function to calculate the minimum time
    // required to color all the edges of a tre
    static void minTimeToColor(int node, int parent,
                        int arrival_time)
    {
        // Starting from time = 0,
        // for all the child edges
        int current_time = 0;
      
        for(int x = 0; x < edges[node].Count; x++) {
      
            // If the edge is not visited yet.
            if (edges[node][x] != parent) {
      
                // Time of coloring of
                // the current edge
                ++current_time;
      
                // If the parent edge has
                // been colored at the same time
                if (current_time == arrival_time)
                    ++current_time;
      
                // Update the maximum time
                ans = Math.Max(ans, current_time);
      
                // Recurisvely call the
                // function to its child node
                minTimeToColor(edges[node][x], node, current_time);
            }
        }
    }
 
  // Driver code
  static void Main() {
     
    for(int i = 0; i < 100000; i++)
    {
        edges.Add(new List());
    }
     
    int[,] A = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
  
    for(int i = 0; i < 3; i++)
    {
        Add_edge(A[i,0], A[i,1]);
    }
  
    // Function call
    minTimeToColor(1, -1, 0);
  
    // Finally, print the answer
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
2

时间复杂度: O(N)
辅助史派西: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程