给定一个由 N 个节点和两个整数a和b组成的未加权和无向图。任意两个节点之间的边仅在它们之间的位差为2时才存在,任务是找到节点a和b之间的最短路径的长度。如果节点a和b之间不存在路径,则打印-1 。
例子:
Input: N = 15, a = 15, b = 3
Output: 1
Explanation: a = 15 = (1111)2 and b = 3 = (0011)2. The bit difference between 15 and 3 is 2. Therefore, there is a direct edge between 15 and 3. Hence, length of the shortest path is 1.
Input: N = 15, a = 15, b = 2
Output: -1
Explanation: a = 15 = (1111)2 and b= 2 = (0010)2. The bit difference between 15 and 2 is 3. As the bit difference can only be 2, it is impossible to reach 15
from 2.
朴素的方法:解决这个问题的最简单的方法是首先使用给定的条件构造图,然后通过将a作为图的源节点,使用 bfs 找到使用a和b的节点之间的最短路径。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效方法:可以通过观察任意两个节点之间的位差之和必须是因子2并且它们的最短距离必须是该和的一半来解决该问题。按照下面给出的步骤来理解该方法:
- a和b 的按位异或中的设置位计数给出节点a和b之间的位差计数。
- 如果a和b 的按位异或中的设置位计数是2的倍数,则a和b连接。
- 如果设置位的计数为2 ,则意味着它们彼此相距1 个单位。如果a和b 的异或中的设置位计数为4 ,则意味着节点a和b相距2 个单位。因此,如果位差为x则最短路径为x/2 。
- 如果位差是奇数,则它们没有连接,因此,打印-1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count set bits
// in a number
int countbitdiff(int xo)
{
// Stores count of
// set bits in xo
int count = 0;
// Iterate over each
// bits of xo
while (xo) {
// If current bit of xo
// is 1
if (xo % 2 == 1) {
// Update count
count++;
}
// Update xo
xo = xo / 2;
}
return count;
}
// Function to find length of shortest
// path between the nodes a and b
void shortestPath(int n, int a, int b)
{
// Stores XOR of a and b
int xorVal = a ^ b;
// Stores the count of
// set bits in xorVal
int cnt = countbitdiff(xorVal);
// If cnt is an even number
if (cnt % 2 == 0)
cout << cnt / 2 << endl;
else
cout << "-1" << endl;
}
// Driver Code
int main()
{
// Given N
int n = 15;
// Given a and b
int a = 15, b = 3;
// Function call
shortestPath(n, a, b);
return 0;
}
Java
// Java program for tha above approach
import java.util.*;
class GFG{
// Function to count set bits
// in a number
static int countbitdiff(int xo)
{
// Stores count of
// set bits in xo
int count = 0;
// Iterate over each
// bits of xo
while (xo != 0)
{
// If current bit of xo
// is 1
if (xo % 2 == 1)
{
// Update count
count++;
}
// Update xo
xo = xo / 2;
}
return count;
}
// Function to find length of shortest
// path between the nodes a and b
static void shortestPath(int n, int a, int b)
{
// Stores XOR of a and b
int xorVal = a ^ b;
// Stores the count of
// set bits in xorVal
int cnt = countbitdiff(xorVal);
// If cnt is an even number
if (cnt % 2 == 0)
System.out.print(cnt / 2);
else
System.out.print("-1");
}
// Driver Code
public static void main(String[] args)
{
// Given N
int n = 15;
// Given a and b
int a = 15, b = 3;
// Function call
shortestPath(n, a, b);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program for the above approach
# Function to count set bits
# in a number
def countbitdiff(xo):
# Stores count of
# set bits in xo
count = 0
# Iterate over each
# bits of xo
while (xo):
# If current bit of xo
# is 1
if (xo % 2 == 1):
# Update count
count+=1
# Update xo
xo = xo // 2
return count
# Function to find length of shortest
# path between the nodes a and b
def shortestPath(n, a, b):
# Stores XOR of a and b
xorVal = a ^ b
# Stores the count of
# set bits in xorVal
cnt = countbitdiff(xorVal)
# If cnt is an even number
if (cnt % 2 == 0):
print(cnt // 2)
else:
print("-1")
# Driver Code
if __name__ == '__main__':
# Given N
n = 15
# Given a and b
a,b = 15,3
# Function call
shortestPath(n, a, b)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to count set bits
// in a number
static int countbitdiff(int xo)
{
// Stores count of
// set bits in xo
int count = 0;
// Iterate over each
// bits of xo
while (xo != 0)
{
// If current bit of xo
// is 1
if (xo % 2 == 1)
{
// Update count
count++;
}
// Update xo
xo = xo / 2;
}
return count;
}
// Function to find length of shortest
// path between the nodes a and b
static void shortestPath(int n, int a, int b)
{
// Stores XOR of a and b
int xorVal = a ^ b;
// Stores the count of
// set bits in xorVal
int cnt = countbitdiff(xorVal);
// If cnt is an even number
if (cnt % 2 == 0)
Console.Write(cnt / 2);
else
Console.Write("-1");
}
// Driver code
public static void Main (String[] args)
{
// Given N
int n = 15;
// Given a and b
int a = 15, b = 3;
// Function call
shortestPath(n, a, b);
}
}
// This code is contributed by code_hunt.
Javascript
1
时间复杂度: O(log 2 (N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。