根据给定条件查找是否可能只访问给定图中的每个节点一次
给定一个具有N+1个节点、 2*N-1 条边和一个布尔数组arr[ ]的 Graph,任务是找出是否可以准确访问每个节点一次并打印任何可能的路径。还知道如果arr[i]为0 ,则N-1 条边去i-th 到 (i+1)-th节点,其余边去i-th 到 (n+1)-th节点,否则(n+1) -th 到 i-th 。
例子:
Input: N = 3, arr = {0, 1, 0}
Output: [1, 2, 3, 4]
Explanation:
from the given data, the edges will:
1 -> 2
2 -> 3
1 -> 4
4 -> 2
3 -> 4
Therefore, one can follow the path: 1 -> 2 -> 3 -> 4
Input: N = 4, arr = {0, 1, 1, 1}
Output: [1, 4, 2, 3]
方法:这个问题可以通过一些观察使用贪心方法来解决。观察结果如下:
- 如果arr[1] 为 1 ,则遵循路径[N+1 -> 1 -> 2……..-> N] 。
- 如果arr[N] 为 0 ,则遵循路径[1 -> 2 ->………….-> N -> N+1] 。
- 如果arr[1] 为 0 且 arr[N] 为 1 ,则必须存在整数i (1 ≤ i< N)其中arr[i] =0 且 arr[i+1] = 1 ,则路径[1 -> 2 ->⋯ -> i -> N+1 -> i+1 -> i+2 ->⋯N]是有效的。
- 上一步证明了该图中始终存在一条哈密顿路径。
以下是上述方法的实现:
C++
// C++ program for above approach.
#include
using namespace std;
// Function to find print path
void findpath(int N, int a[])
{
// If a[0] is 1
if (a[0]) {
// Printing path
cout <<" "<< N + 1;
for (int i = 1; i <= N; i++)
cout <<" " << i;
return;
}
// Seeking for a[i] = 0 and a[i+1] = 1
for (int i = 0; i < N - 1; i++) {
if (!a[i] && a[i + 1]) {
// Printing path
for (int j = 1; j <= i; j++)
cout <<" "<< j;
cout <<" "<< N + 1;
for (int j = i + 1; j <= N; j++)
cout <<" "<< j;
return;
}
}
// If a[N-1] = 0
for (int i = 1; i <= N; i++)
cout <<" "<< i;
cout <<" "<< N + 1;
}
// Driver Code
int main()
{
// Given Input
int N = 3, arr[] = { 0, 1, 0 };
// Function Call
findpath(N, arr);
}
// This code is contributed by shivanisinghss2110
C
// C++ program for above approach.
#include
using namespace std;
// Function to find print path
void findpath(int N, int a[])
{
// If a[0] is 1
if (a[0]) {
// Printing path
printf("%d ", N + 1);
for (int i = 1; i <= N; i++)
printf("%d ", i);
return;
}
// Seeking for a[i] = 0 and a[i+1] = 1
for (int i = 0; i < N - 1; i++) {
if (!a[i] && a[i + 1]) {
// Printing path
for (int j = 1; j <= i; j++)
printf("%d ", j);
printf("%d ", N + 1);
for (int j = i + 1; j <= N; j++)
printf("%d ", j);
return;
}
}
// If a[N-1] = 0
for (int i = 1; i <= N; i++)
printf("%d ", i);
printf("%d ", N + 1);
}
// Driver Code
int main()
{
// Given Input
int N = 3, arr[] = { 0, 1, 0 };
// Function Call
findpath(N, arr);
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find print path
static void findpath(int N, int a[])
{
// If a[0] is 1
if (a[0] == 1) {
// Printing path
System.out.print((N + 1) + " ");
for (int i = 1; i <= N; i++)
System.out.print((i) + " ");
return;
}
// Seeking for a[i] = 0 and a[i+1] = 1
for (int i = 0; i < N - 1; i++) {
if (a[i] == 0 && a[i + 1] == 1) {
// Printing path
for (int j = 1; j <= i; j++)
System.out.print((j) + " ");
System.out.print((N + 1) + " ");
for (int j = i + 1; j <= N; j++)
System.out.print((j) + " ");
return;
}
}
// If a[N-1] = 0
for (int i = 1; i <= N; i++)
System.out.print((i) + " ");
System.out.print((N + 1) + " ");
}
// Driver Code
public static void main(String[] args)
{
int N = 3, arr[] = { 0, 1, 0 };
// Function Call
findpath(N, arr);
}
}
// This code is contributed by dwivediyash
Python3
# Python 3 program for above approach.
# Function to find print path
def findpath(N, a):
# If a[0] is 1
if (a[0]):
# Printing path
print(N + 1)
for i in range(1,N+1,1):
print(i,end = " ")
return
# Seeking for a[i] = 0 and a[i+1] = 1
for i in range(N - 1):
if (a[i]==0 and a[i + 1]):
# Printing path
for j in range(1,i+1,1):
print(j,end = " ")
print(N + 1,end = " ");
for j in range(i + 1,N+1,1):
print(j,end = " ")
return
# If a[N-1] = 0
for i in range(1,N+1,1):
print(i,end = " ")
print(N + 1,end = " ")
# Driver Code
if __name__ == '__main__':
# Given Input
N = 3
arr = [0, 1, 0]
# Function Call
findpath(N, arr)
# This code is contributed by ipg2016107.
C#
// C# program for above approach.
using System;
class GFG{
// Function to find print path
static void findpath(int N, int []a)
{
// If a[0] is 1
if (a[0]!=0) {
// Printing path
Console.Write(N + 1);
for (int i = 1; i <= N; i++)
Console.Write(i+ " ");
return;
}
// Seeking for a[i] = 0 and a[i+1] = 1
for (int i = 0; i < N - 1; i++) {
if (a[i]==0 && a[i + 1] !=0) {
// Printing path
for (int j = 1; j <= i; j++)
Console.Write(j+" ");
Console.Write(N + 1 + " ");
for (int j = i + 1; j <= N; j++)
Console.Write(j + " ");
return;
}
}
// If a[N-1] = 0
for (int i = 1; i <= N; i++)
Console.Write(i+" ");
Console.Write(N + 1 + " ");
}
// Driver Code
public static void Main()
{
// Given Input
int N = 3;
int []arr = { 0, 1, 0 };
// Function Call
findpath(N, arr);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
4 1 2 3
时间复杂度: O(N)
辅助空间: O(1)