📌  相关文章
📜  最大化图中与所有其他节点断开连接的节点数

📅  最后修改于: 2021-05-06 09:45:24             🧑  作者: Mango

给定两个整数NE ,该整数表示无向图的节点数和边数,任务是在不使用任何自环的情况下,使未连接到图中任何其他节点的节点数最大化。
例子:

方法:该方法基于以下想法:为了最大化断开连接的节点数,直到每两个不同的节点都被连接后,新节点才会添加到图中。以下是解决此问题的步骤:

  1. 初始化两个变量currrem来分别存储连接的节点和未分配的边。
  2. 如果rem变为0,则所需答案将为N – curr
  3. 否则,将curr的值增加1。
  4. 因此,当前步骤中保持两个不同节点保持连接所需的最大边为min(rem,curr) 。从rem减去它并增加curr
  5. 重复此过程,直到rem降为零。
  6. 最后,打印N-curr

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function which returns
// the maximum number of
// isolated nodes
int maxDisconnected(int N, int E)
{
    // Used nodes
    int curr = 1;
 
    // Remaining edges
    int rem = E;
 
    // Count nodes used
    while (rem > 0) {
        rem = rem
              - min(
                    curr, rem);
        curr++;
    }
 
    // If given edges are non-zero
    if (curr > 1) {
        return N - curr;
    }
    else {
        return N;
    }
}
 
// Driver Code
int main()
{
    // Given N and E
    int N = 5, E = 1;
 
    // Function Call
    cout << maxDisconnected(N, E);
 
    return 0;
}


Java
// Java implementation of
// the above approach
import java.util.*;
 
class GFG{
 
// Function which returns
// the maximum number of
// isolated nodes
static int maxDisconnected(int N, int E)
{
     
    // Used nodes
    int curr = 1;
 
    // Remaining edges
    int rem = E;
 
    // Count nodes used
    while (rem > 0)
    {
        rem = rem - Math.min(
                    curr, rem);
        curr++;
    }
 
    // If given edges are non-zero
    if (curr > 1)
    {
        return N - curr;
    }
    else
    {
        return N;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N and E
    int N = 5, E = 1;
 
    // Function call
    System.out.print(maxDisconnected(N, E));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of
# the above approach
 
# Function which returns
# the maximum number of
# isolated nodes
def maxDisconnected(N, E):
 
    # Used nodes
    curr = 1
 
    # Remaining edges
    rem = E
 
    # Count nodes used
    while (rem > 0):
        rem = rem - min(curr, rem)
        curr += 1
 
    # If given edges are non-zero
    if (curr > 1):
        return N - curr
    else:
        return N
 
# Driver Code
if __name__ == '__main__':
 
    # Given N and E
    N = 5
    E = 1
 
    # Function call
    print(maxDisconnected(N, E))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of
// the above approach
using System;
class GFG{
 
// Function which returns
// the maximum number of
// isolated nodes
static int maxDisconnected(int N,
                           int E)
{   
  // Used nodes
  int curr = 1;
 
  // Remaining edges
  int rem = E;
 
  // Count nodes used
  while (rem > 0)
  {
    rem = rem - Math.Min(curr, rem);
    curr++;
  }
 
  // If given edges are non-zero
  if (curr > 1)
  {
    return N - curr;
  }
  else
  {
    return N;
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given N and E
  int N = 5, E = 1;
 
  // Function call
  Console.Write(maxDisconnected(N, E));
}
}
  
// This code is contributed by 29AjayKumar


输出:
3


时间复杂度: O(E)
辅助空间: O(1)