要从给定的无向图中删除的最小边以删除节点 A 和 B 之间的任何现有路径
给定两个整数N和M表示图中的顶点和边的数量,以及大小为M的数组edges[][] ,表示边[i][0]和边[i][1]之间的边,任务是找到与节点B直接连接的最小边,这些边必须被移除,使得顶点A和B之间不存在路径。
例子:
Input: N = 4, A = 3, B = 2, edges[][] = {{3, 1}, {3, 4}, {1, 2}, {4, 2}}
Output: 2
Explanation: The edges at index 2 and 3 i.e., {1, 2} and {4, 2} must be removed as they both are in the path from vertex A to vertex B.
Input: N = 6, A = 1, B = 6, edges[][] = {{1, 2}, {1, 6}, {2, 6}, {1, 4}, {4, 6}, {4, 3}, {2, 4}}
Output: 3
方法:给定的问题可以使用深度优先搜索算法来解决。可以观察到 与结束顶点B相关联并且存在于从起始节点A到节点B结束的任何路径中的所有边都必须被删除。因此,执行dfs 从节点A开始并维护所有访问过的顶点。请按照以下步骤解决问题:
- 创建一个邻接矩阵g[][]来存储两个节点之间的边。
- 初始化一个数组v[] ,标记可以从节点A到达的节点。
- 创建一个变量cnt ,它存储需要删除的节点数。最初, cnt = 0 。
- 遍历所有节点,如果它可以从A到达并且与B直接连接,则增加cnt的值。
- 存储在cnt中的值是所需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function for Depth first Search
void dfs(int s, vector > g,
vector& v)
{
for (auto i : g[s]) {
// If current vertex is
// not visited yet
if (!v[i]) {
v[i] = 1;
// Recursive call for
// dfs function
dfs(i, g, v);
}
}
}
// Function to find the out the minimum
// number of edges that must be removed
int deleteEdges(int n, int m, int a, int b,
vector > edges)
{
// Creating Adjacency Matrix
vector > g(n, vector());
for (int i = 0; i < m; i++) {
g[edges[i][0] - 1].push_back(edges[i][1] - 1);
g[edges[i][1] - 1].push_back(edges[i][0] - 1);
}
// Vector for marking visited
vector v(n, 0);
v[a - 1] = 1;
// Calling dfs function
dfs(a - 1, g, v);
// Stores the final count
int cnt = 0;
for (int i = 0; i < n; i++) {
// If current node can not
// be visited from node A
if (v[i] == 0)
continue;
for (int j = 0; j < g[i].size(); j++) {
// If a node between current
// node and node b exist
if (g[i][j] == b - 1) {
cnt++;
}
}
}
// Return Answer
return cnt;
}
// Driver Code
int main()
{
int N = 6;
int M = 7;
int A = 1;
int B = 6;
vector > edges{
{ 1, 2 }, { 5, 2 }, { 2, 4 },
{ 2, 3 }, { 3, 6 }, { 4, 6 }, { 5, 6 }
};
cout << deleteEdges(N, M, A, B, edges);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function for Depth first Search
static void dfs(int s, Vector [] g,
int[] v)
{
for (int i : g[s]) {
// If current vertex is
// not visited yet
if (v[i] == 0) {
v[i] = 1;
// Recursive call for
// dfs function
dfs(i, g, v);
}
}
}
// Function to find the out the minimum
// number of edges that must be removed
static int deleteEdges(int n, int m, int a, int b,
int[][] edges)
{
// Creating Adjacency Matrix
Vector []g = new Vector[n];
for (int i = 0; i < g.length; i++)
g[i] = new Vector();
for (int i = 0; i < m; i++) {
g[edges[i][0] - 1].add(edges[i][1] - 1);
g[edges[i][1] - 1].add(edges[i][0] - 1);
}
// Vector for marking visited
int []v = new int[n];
v[a - 1] = 1;
// Calling dfs function
dfs(a - 1, g, v);
// Stores the final count
int cnt = 0;
for (int i = 0; i < n; i++) {
// If current node can not
// be visited from node A
if (v[i] == 0)
continue;
for (int j = 0; j < g[i].size(); j++) {
// If a node between current
// node and node b exist
if (g[i].get(j) == b - 1) {
cnt++;
}
}
}
// Return Answer
return cnt;
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
int M = 7;
int A = 1;
int B = 6;
int[][] edges ={
{ 1, 2 }, { 5, 2 }, { 2, 4 },
{ 2, 3 }, { 3, 6 }, { 4, 6 }, { 5, 6 }
};
System.out.print(deleteEdges(N, M, A, B, edges));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function for Depth first Search
def dfs(s, g, v):
for i in g[s]:
# If current vertex is
# not visited yet
if not v[i]:
v[i] = 1
# Recursive call for
# dfs function
dfs(i, g, v)
# Function to find the out the minimum
# number of edges that must be removed
def deleteEdges(n, m, a, b, edges):
# Creating Adjacency Matrix
g = [0] * m
for i in range(len(g)):
g[i] = []
for i in range(m):
g[edges[i][0] - 1].append(edges[i][1] - 1)
g[edges[i][1] - 1].append(edges[i][0] - 1)
# Vector for marking visited
v = [0] * n
v[a - 1] = 1
# Calling dfs function
dfs(a - 1, g, v)
# Stores the final count
cnt = 0
for i in range(n):
# If current node can not
# be visited from node A
if (v[i] == 0):
continue
for j in range(len(g[i])):
# If a node between current
# node and node b exist
if (g[i][j] == b - 1):
cnt += 1
# Return Answer
return cnt
# Driver Code
N = 6
M = 7
A = 1
B = 6
edges = [[1, 2], [5, 2], [2, 4],
[2, 3], [3, 6], [4, 6],
[5, 6]]
print(deleteEdges(N, M, A, B, edges))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function for Depth first Search
static void dfs(int s, List [] g,
int[] v)
{
foreach (int i in g[s]) {
// If current vertex is
// not visited yet
if (v[i] == 0) {
v[i] = 1;
// Recursive call for
// dfs function
dfs(i, g, v);
}
}
}
// Function to find the out the minimum
// number of edges that must be removed
static int deleteEdges(int n, int m, int a, int b,
int[,] edges)
{
// Creating Adjacency Matrix
List []g = new List[n];
for (int i = 0; i < g.Length; i++)
g[i] = new List();
for (int i = 0; i < m; i++) {
g[edges[i,0] - 1].Add(edges[i,1] - 1);
g[edges[i,1] - 1].Add(edges[i,0] - 1);
}
// List for marking visited
int []v = new int[n];
v[a - 1] = 1;
// Calling dfs function
dfs(a - 1, g, v);
// Stores the readonly count
int cnt = 0;
for (int i = 0; i < n; i++) {
// If current node can not
// be visited from node A
if (v[i] == 0)
continue;
for (int j = 0; j < g[i].Count; j++) {
// If a node between current
// node and node b exist
if (g[i][j] == b - 1) {
cnt++;
}
}
}
// Return Answer
return cnt;
}
// Driver Code
public static void Main(String[] args)
{
int N = 6;
int M = 7;
int A = 1;
int B = 6;
int[,] edges ={
{ 1, 2 }, { 5, 2 }, { 2, 4 },
{ 2, 3 }, { 3, 6 }, { 4, 6 }, { 5, 6 }
};
Console.Write(deleteEdges(N, M, A, B, edges));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(N)