📌  相关文章
📜  在 K 个三元组 (A, B, C) 之后创建一棵由数字 [1, N] 组成的树,这样从 A 到 C 的任何路径都不能包含 B

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

在 K 个三元组 (A, B, C) 之后创建一棵由数字 [1, N] 组成的树,这样从 A 到 C 的任何路径都不能包含 B

给定一个整数N表示数字[1, N]K以三元组{A, B, C} 的形式限制,这样从AC的任何简单路径都不能包含B。任务是创建一个数字树 [ 1, N] 遵循这些 K 限制并打印该树的边缘。假设对于给定的一组限制,树总是存在的。

例子:

方法:这个问题的基本问题是,对于一棵树,每个节点只能有一个父节点。因此,总有一个节点不受限制,它可以位于任意两个节点之间。因此,将该节点连接到所有节点以获得答案。现在要解决此问题,请按照以下步骤操作:

  1. 创建一个布尔向量访问,它将所有节点1标记为限制,并用0对其进行初始化。
  2. 为所有限制迭代一个循环,并在已访问的数组中有限制的所有节点都标记为1
  3. 访问数组中值为0的节点的值存储为root
  4. 使用root连接所有节点。
  5. 根据以上观察打印答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the tree following
// the given restrictions
void RestrictedTree(
    vector >& restrictions,
    int N)
{
    // Vector to track the restricted nodes
    vector visited(N + 1, false);
 
    // Loop to mark the restricted node visited
    for (int index = 0;
         index < restrictions.size();
         index++) {
 
        int a = restrictions[index][1];
        visited[a] = true;
    }
 
    // Variable to consider root node
    int root = -1;
    for (int index = 1; index <= N; index++) {
        if (visited[index] == false) {
            root = index;
            break;
        }
    }
 
    // Connecting all node to the root node
    for (int index = 1; index <= N; index++) {
 
        if (index != root) {
            cout << root << " "
                 << index << "\n";
        }
    }
    return;
}
 
// Driver code
int main()
{
    int N = 8;
    vector > restrictions = {
        { 3, 2, 4 },
        { 3, 5, 4 },
        { 5, 8, 7 },
        { 6, 1, 8 }
    };
 
    RestrictedTree(restrictions, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to print the tree following
// the given restrictions
static void RestrictedTree(
    int[][]restrictions,
    int N)
{
   
    // Vector to track the restricted nodes
    boolean visited[] = new boolean[N + 1];
 
    // Loop to mark the restricted node visited
    for (int index = 0;
         index < restrictions.length;
         index++) {
 
        int a = restrictions[index][1];
        visited[a] = true;
    }
 
    // Variable to consider root node
    int root = -1;
    for (int index = 1; index <= N; index++) {
        if (visited[index] == false) {
            root = index;
            break;
        }
    }
 
    // Connecting all node to the root node
    for (int index = 1; index <= N; index++) {
 
        if (index != root) {
            System.out.print(root+ " "
                 + index+ "\n");
        }
    }
    return;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 8;
    int [][]restrictions = {
        { 3, 2, 4 },
        { 3, 5, 4 },
        { 5, 8, 7 },
        { 6, 1, 8 }
    };
 
    RestrictedTree(restrictions, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python Program to implement
# the above approach
 
# Function to print the tree following
# the given restrictions
def RestrictedTree(restrictions, N):
 
    # Vector to track the restricted nodes
    visited = [False] * (N + 1)
 
    # Loop to mark the restricted node visited
    for index in range(len(restrictions)):
        a = restrictions[index][1]
        visited[a] = True
 
    # Variable to consider root node
    root = -1
    for index in range(1, N + 1):
        if (visited[index] == False):
            root = index
            break
 
    # Connecting all node to the root node
    for index in range(1, N + 1):
 
        if (index != root):
            print(f'{root} {index}')
     
    return
 
# Driver code
N = 8
restrictions = [
    [3, 2, 4],
    [3, 5, 4],
    [5, 8, 7],
    [6, 1, 8]
]
 
RestrictedTree(restrictions, N)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to print the tree following
// the given restrictions
static void RestrictedTree(
    int[,]restrictions,
    int N)
{
   
    // Vector to track the restricted nodes
    bool []visited = new bool[N + 1];
 
    // Loop to mark the restricted node visited
    for (int index = 0;
         index < restrictions.GetLength(0);
         index++) {
 
        int a = restrictions[index,1];
        visited[a] = true;
    }
 
    // Variable to consider root node
    int root = -1;
    for (int index = 1; index <= N; index++) {
        if (visited[index] == false) {
            root = index;
            break;
        }
    }
 
    // Connecting all node to the root node
    for (int index = 1; index <= N; index++) {
 
        if (index != root) {
            Console.Write(root+ " "
                 + index+ "\n");
        }
    }
    return;
}
 
// Driver code
public static void Main(string[] args)
{
    int N = 8;
    int [,]restrictions = {
        { 3, 2, 4 },
        { 3, 5, 4 },
        { 5, 8, 7 },
        { 6, 1, 8 }
    };
 
    RestrictedTree(restrictions, N);
}
}
 
// This code is contributed by ukasp.


Javascript



输出
3 1
3 2
3 4
3 5
3 6
3 7
3 8

时间复杂度: O(N)
辅助空间: O(N)