在 K 个三元组 (A, B, C) 之后创建一棵由数字 [1, N] 组成的树,这样从 A 到 C 的任何路径都不能包含 B
给定一个整数N表示数字[1, N]和K以三元组{A, B, C} 的形式限制,这样从A到C的任何简单路径都不能包含B。任务是创建一个数字树 [ 1, N] 遵循这些 K 限制并打印该树的边缘。假设对于给定的一组限制,树总是存在的。
例子:
Input: N = 8, restrictions[] = {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}, {6, 7, 4}}
Output:
3 4
3 5
3 1
1 2
2 8
8 6
8 7
Explanation: The tree formed following the given restrictions is:
3
/ | \
4 5 1
|
2
|
8
/ \
6 7
Input: N = 8, restrictions[] = {{3, 2, 4}, {3, 5, 4}, {5, 8, 7}, {6, 1, 8}}
Output:
3 1
3 2
3 4
3 5
3 6
3 7
3 8
方法:这个问题的基本问题是,对于一棵树,每个节点只能有一个父节点。因此,总有一个节点不受限制,它可以位于任意两个节点之间。因此,将该节点连接到所有节点以获得答案。现在要解决此问题,请按照以下步骤操作:
- 创建一个布尔向量访问,它将所有节点1标记为限制,并用0对其进行初始化。
- 为所有限制迭代一个循环,并在已访问的数组中有限制的所有节点都标记为1 。
- 将访问数组中值为0的节点的值存储为root 。
- 使用root连接所有节点。
- 根据以上观察打印答案。
下面是上述方法的实现:
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)