给定两个整数V和E ,它们表示平面图的顶点和边的数量。任务是找到该平面图的区域数。
平面图( Planar Graph):平面图是指没有边相交的图或可以在平面上绘制而没有边相交的图,称为平面图。
区域:绘制平面图时,没有交叉边,图的边和顶点将平面划分为区域。
例子:
Input: V = 4, E = 5
Output: R = 3
Input: V = 3, E = 3
Output: R = 2
方法:欧拉找出平面图中的区域数作为图中顶点数和边数的函数,即
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the number
// of regions in a Planar Graph
int Regions(int Vertices, int Edges)
{
int R = Edges + 2 - Vertices;
return R;
}
// Driver code
int main()
{
int V = 5, E = 7;
cout << Regions(V, E);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG {
// Function to return the number
// of regions in a Planar Graph
static int Regions(int Vertices, int Edges)
{
int R = Edges + 2 - Vertices;
return R;
}
// Driver code
public static void main(String[] args)
{
int V = 5, E = 7;
System.out.println(Regions(V, E));
}
}
// This code is contributed by akt_mit
Python3
# Python3 implementation of the approach
# Function to return the number
# of regions in a Planar Graph
def Regions(Vertices, Edges) :
R = Edges + 2 - Vertices;
return R;
# Driver code
if __name__ == "__main__" :
V = 5; E = 7;
print(Regions(V, E));
# This code is contributed
# by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the number
// of regions in a Planar Graph
static int Regions(int Vertices, int Edges)
{
int R = Edges + 2 - Vertices;
return R;
}
// Driver code
static public void Main()
{
int V = 5, E = 7;
Console.WriteLine(Regions(V, E));
}
}
// This code is contributed by ajit
PHP
Javascript
输出:
4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。