给定图的“n”个顶点和“m”条边。找出图中可能的孤立顶点的最小数量和最大数量。
例子:
Input : 4 2
Output : Minimum 0
Maximum 1
1--2 3--4 <---Minimum - No isolated vertex
1--2 <--- Maximum - 1 Isolated vertex i.e. 4
|
3
Input : 5 2
Output : Minimum 1
Maximum 2
1--2 3--4 5 <-- Minimum - 1 isolated vertex i.e. 5
1--2 4 5 <-- Maximum - 2 isolated vertex i.e. 4 and 5
|
3
- 对于最少数量的孤立顶点,我们仅通过一条边连接两个顶点。每个顶点应该只连接到另一个顶点,并且每个顶点都应该有一个度数
因此,如果边的数量是 ‘m’,并且如果 ‘n’ 顶点 <=2 * ‘m’ 边,则没有孤立顶点,如果此条件为假,则有 n-2*m 个孤立顶点。 - 为了获得最大数量的孤立顶点,我们创建了一个多边形,这样每个顶点都连接到其他顶点,并且每个顶点与每个其他顶点都有一条对角线。因此,n个边多边形的一个顶点到另一个顶点的对角线数为n*(n-3)/2,连接相邻顶点的边数为n。因此,边的总数为 n*(n-1)/2。
下面是上述方法的实现。
C++
// CPP program to find maximum/minimum number
// of isolated vertices.
#include
using namespace std;
// Function to find out the minimum and
// maximum number of isolated vertices
void find(int n, int m)
{
// Condition to find out minimum number
// of isolated vertices
if (n <= 2 * m)
cout << "Minimum " << 0 << endl;
else
cout << "Minimum " << n - 2 * m << endl;
// To find out maximum number of isolated
// vertices
// Loop to find out value of number of
// vertices that are connected
int i;
for (i = 1; i <= n; i++) {
if (i * (i - 1) / 2 >= m)
break;
}
cout << "Maximum " << n - i;
}
// Driver Function
int main()
{
// Number of vertices
int n = 4;
// Number of edges
int m = 2;
// Calling the function to maximum and
// minimum number of isolated vertices
find(n, m);
return 0;
}
Java
// Java program to find maximum/minimum number
// of isolated vertices.
import java.io.*;
class GFG {
// Function to find out the minimum and
// maximum number of isolated vertices
static void find(int n, int m)
{
// Condition to find out minimum number
// of isolated vertices
if (n <= 2 * m)
System.out.println( "Minimum " + 0);
else
System.out.println( "Minimum " + (n - 2 * m));
// To find out maximum number of isolated
// vertices
// Loop to find out value of number of
// vertices that are connected
int i;
for (i = 1; i <= n; i++) {
if (i * (i - 1) / 2 >= m)
break;
}
System.out.println( "Maximum " + (n - i));
}
// Driver Function
public static void main (String[] args) {
// Number of vertices
int n = 4;
// Number of edges
int m = 2;
// Calling the function to maximum and
// minimum number of isolated vertices
find(n, m);
}
}
//This code is contributed by inder_verma.
Python3
# Python3 program to find maximum/minimum
# number of isolated vertices.
# Function to find out the minimum and
# maximum number of isolated vertices
def find(n, m) :
# Condition to find out minimum
# number of isolated vertices
if (n <= 2 * m):
print("Minimum ", 0)
else:
print("Minimum ", n - 2 * m )
# To find out maximum number of
# isolated vertices
# Loop to find out value of number
# of vertices that are connected
for i in range(1, n + 1):
if (i * (i - 1) // 2 >= m):
break
print("Maximum ", n - i)
# Driver Code
if __name__ == '__main__':
# Number of vertices
n = 4
# Number of edges
m = 2
# Calling the function to maximum and
# minimum number of isolated vertices
find(n, m)
# This code is contributed by
# SHUBHAMSINGH10
C#
// C# program to find maximum/
// minimum number of isolated vertices.
using System;
class GFG
{
// Function to find out the
// minimum and maximum number
// of isolated vertices
static void find(int n, int m)
{
// Condition to find out minimum
// number of isolated vertices
if (n <= 2 * m)
Console.WriteLine("Minimum " + 0);
else
Console.WriteLine("Minimum " +
(n - 2 * m));
// To find out maximum number
// of isolated vertices
// Loop to find out value of
// number of vertices that
// are connected
int i;
for (i = 1; i <= n; i++)
{
if (i * (i - 1) / 2 >= m)
break;
}
Console.WriteLine("Maximum " + (n - i));
}
// Driver Code
public static void Main ()
{
// Number of vertices
int n = 4;
// Number of edges
int m = 2;
// Calling the function to
// maximum and minimum number
// of isolated vertices
find(n, m);
}
}
// This code is contributed
// by inder_verma.
PHP
= $m)
break;
}
echo "Maximum " , ($n - $i);
}
// Driver Code
// Number of vertices
$n = 4;
// Number of edges
$m = 2;
// Calling the function to
// maximum and minimum number
// of isolated vertices
find($n, $m);
// This code is contributed
// by inder_verma
?>
Javascript
输出:
Minimum 0
Maximum 1
时间复杂度– O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。