📜  通过M个边连接图形,以使图形不包含任何循环,并且连接的顶点的按位与最大

📅  最后修改于: 2021-04-17 15:33:53             🧑  作者: Mango

给定一个数组arr [] ,该数组arr []由最初未连接图的N个顶点的值和整数M组成,任务是将图的某些顶点与恰好M个边连接,从而仅形成一个连接的分量,从而无法形成任何循环连接的顶点的按位与是最大可能的。

例子:

方法:解决给定问题的想法是使用M条边生成连接顶点的所有可能组合,并在所有可能的组合中打印最大的按位与。

连接N个顶点的方法总数为2 N,并且应该有(M + 1)个顶点以仅构成一个连接的组件。请按照以下步骤解决给定的问题:

  • 将两个变量ANDans初始化为0,以分别存储任何可能连接的组件的节点的最大Bitwise ANDBitwise 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


输出:
0

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