给定一个数组arr[]由初始未连通图的N个顶点的值和整数M 组成,任务是将图的某些顶点与恰好M 条边相连,仅形成一个连通分量,这样就不能形成环和连接顶点的按位与是最大可能的。
例子:
Input: arr[] = {1, 2, 3, 4}, M = 2
Output: 0
Explanation:
Following arrangement of M edges between the given vertices are:
1->2->3 (1 & 2 & 3 = 0).
2->3->4 (2 & 3 & 4 = 0).
3->4->1 (3 & 4 & 1 = 0).
1->2->4 (1 & 2 & 4 = 0).
Therefore, the maximum Bitwise AND value among all the cases is 0.
Input: arr[] = {4, 5, 6}, M = 2
Output: 4
Explanation:
Only possible way to add M edges is 4 -> 5 -> 6 (4 & 5 & 6 = 4).
Therefore, the maximum Bitwise AND value possible is 4.
方法:解决给定问题的想法是使用M 条边生成连接顶点的所有可能组合,并打印所有可能组合中的最大按位与。
连接N个顶点的方式总数为2 N ,并且应该有(M + 1)个顶点才能形成一个连接组件。按照步骤解决给定的问题:
- 初始化两个变量,比如AND和ans为0以分别存储最大的Bitwise AND和任何可能连接组件的节点的Bitwise AND 。
- 使用变量i迭代范围[1, 2 N ] ,并执行以下步骤:
- 如果我有(M + 1) 个设置位,则找到设置位位置的按位与并将其存储在变量中,例如ans 。
- 如果AND的值超过ans ,则将AND的值更新为ans 。
- 完成上述步骤后,打印AND的值作为结果的最大Bitwise AND 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum Bitwise
// AND of connected components possible
// by connecting a graph using M edges
int maximumAND(int arr[], int n, int m)
{
// Stores total number of
// ways to connect the graph
int tot = 1 << n;
// Stores the maximum Bitwise AND
int mx = 0;
// Iterate over the range [0, 2^n]
for (int bm = 0; bm < tot; bm++) {
// Store the Bitwise AND of
// the connected vertices
int andans = 0;
// Store the count of the
// connected vertices
int count = 0;
// Check for all the bits
for (int i = 0; i < n; ++i) {
// If i-th bit is set
if ((bm >> i) & 1) {
// If the first vertex is added
if (count == 0) {
// Set andans equal to arr[i]
andans = arr[i];
}
else {
// Calculate Bitwise AND
// of arr[i] with andans
andans = andans & arr[i];
}
// Increase the count of
// connected vertices
count++;
}
}
// If number of connected vertices
// is (m + 1), no cycle is formed
if (count == (m + 1)) {
// Find the maximum Bitwise
// AND value possible
mx = max(mx, andans);
}
}
// Return the maximum
// Bitwise AND possible
return mx;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
int M = 2;
cout << maximumAND(arr, N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the maximum Bitwise
// AND of connected components possible
// by connecting a graph using M edges
static int maximumAND(int arr[], int n, int m)
{
// Stores total number of
// ways to connect the graph
int tot = 1 << n;
// Stores the maximum Bitwise AND
int mx = 0;
// Iterate over the range [0, 2^n]
for(int bm = 0; bm < tot; bm++)
{
// Store the Bitwise AND of
// the connected vertices
int andans = 0;
// Store the count of the
// connected vertices
int count = 0;
// Check for all the bits
for(int i = 0; i < n; ++i)
{
// If i-th bit is set
if (((bm >> i) & 1) != 0)
{
// If the first vertex is added
if (count == 0)
{
// Set andans equal to arr[i]
andans = arr[i];
}
else
{
// Calculate Bitwise AND
// of arr[i] with andans
andans = andans & arr[i];
}
// Increase the count of
// connected vertices
count++;
}
}
// If number of connected vertices
// is (m + 1), no cycle is formed
if (count == (m + 1))
{
// Find the maximum Bitwise
// AND value possible
mx = Math.max(mx, andans);
}
}
// Return the maximum
// Bitwise AND possible
return mx;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 1, 2, 3, 4 };
int N = arr.length;
int M = 2;
System.out.println(maximumAND(arr, N, M));
}
}
// This code is contributed by souravghosh0416
Python3
# Python3 program for the above approach
# Function to find the maximum Bitwise
# AND of connected components possible
# by connecting a graph using M edges
def maximumAND(arr, n, m):
# Stores total number of
# ways to connect the graph
tot = 1 << n
# Stores the maximum Bitwise AND
mx = 0
# Iterate over the range [0, 2^n]
for bm in range(tot):
# Store the Bitwise AND of
# the connected vertices
andans = 0
# Store the count of the
# connected vertices
count = 0
# Check for all the bits
for i in range(n):
# If i-th bit is set
if ((bm >> i) & 1):
# If the first vertex is added
if (count == 0):
# Set andans equal to arr[i]
andans = arr[i]
else:
# Calculate Bitwise AND
# of arr[i] with andans
andans = andans & arr[i]
# Increase the count of
# connected vertices
count += 1
# If number of connected vertices
# is (m + 1), no cycle is formed
if (count == (m + 1)):
# Find the maximum Bitwise
# AND value possible
mx = max(mx, andans)
# Return the maximum
# Bitwise AND possible
return mx
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4]
N = len(arr)
M = 2
print (maximumAND(arr, N, M))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum Bitwise
// AND of connected components possible
// by connecting a graph using M edges
static int maximumAND(int[] arr, int n, int m)
{
// Stores total number of
// ways to connect the graph
int tot = 1 << n;
// Stores the maximum Bitwise AND
int mx = 0;
// Iterate over the range [0, 2^n]
for(int bm = 0; bm < tot; bm++)
{
// Store the Bitwise AND of
// the connected vertices
int andans = 0;
// Store the count of the
// connected vertices
int count = 0;
// Check for all the bits
for(int i = 0; i < n; ++i)
{
// If i-th bit is set
if (((bm >> i) & 1) != 0 )
{
// If the first vertex is added
if (count == 0)
{
// Set andans equal to arr[i]
andans = arr[i];
}
else
{
// Calculate Bitwise AND
// of arr[i] with andans
andans = andans & arr[i];
}
// Increase the count of
// connected vertices
count++;
}
}
// If number of connected vertices
// is (m + 1), no cycle is formed
if (count == (m + 1))
{
// Find the maximum Bitwise
// AND value possible
mx = Math.Max(mx, andans);
}
}
// Return the maximum
// Bitwise AND possible
return mx;
}
// Driver Code
static public void Main ()
{
int[] arr = { 1, 2, 3, 4 };
int N = arr.Length;
int M = 2;
Console.WriteLine(maximumAND(arr, N, M));
}
}
// This code is contributed by avanitrachhadiya2155
Javascript
0
时间复杂度: O((2 N )*N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live