📌  相关文章
📜  在图中找到与剩余顶点中的至少一个相连的 K 个顶点

📅  最后修改于: 2021-10-25 03:28:09             🧑  作者: Mango

给定一个有N个顶点的连通图。任务是从图中选择 k(k 必须小于或等于 n/2,不一定是最小值)个顶点,以便所有这些选定的顶点都连接到至少一个未选定的顶点。如果有多个答案,请打印其中任何一个。

例子:

有效的方法:一种有效的方法是使用简单的 dfs 或 bfs函数找到偶数级和奇数级的顶点。然后,如果奇数级别的顶点小于偶数级别的顶点,则打印奇数级别的顶点。否则,打印偶数级顶点。

下面是上述方法的实现:

C++
// C++ program to find K vertices in
// the graph which are connected to at
// least one of remaining vertices
#include 
using namespace std;
#define N 200005
 
// To store graph
int n, m, vis[N];
vector gr[N];
vector v[2];
 
// Function to add edge
void add_edges(int x, int y)
{
    gr[x].push_back(y);
    gr[y].push_back(x);
}
 
// Function to find level of each node
void dfs(int x, int state)
{
    // Push the vertex in respected level
    v[state].push_back(x);
 
    // Make vertex visited
    vis[x] = 1;
 
    // Traverse for all it's child nodes
    for (auto i : gr[x])
        if (vis[i] == 0)
            dfs(i, state ^ 1);
}
 
// Function to print vertices
void Print_vertices()
{
    // If odd level vertices are less
    if (v[0].size() < v[1].size()) {
        for (auto i : v[0])
            cout << i << " ";
    }
    // If even level vertices are less
    else {
        for (auto i : v[1])
            cout << i << " ";
    }
}
 
// Driver code
int main()
{
    int n = 4, m = 3;
 
    // Add edges
    add_edges(1, 2);
    add_edges(2, 3);
    add_edges(3, 4);
 
    // Function call
    dfs(1, 0);
 
    Print_vertices();
 
    return 0;
}


Java
// Java program to find K vertices in
// the graph which are connected to at
// least one of remaining vertices
import java.util.*;
 
class GFG
{
 
    static final int N = 200005;
 
    // To store graph
    static int n, m;
    static int[] vis = new int[N];
    static Vector[] gr = new Vector[N];
    static Vector[] v = new Vector[2];
 
    // Function to add edge
    static void add_edges(int x, int y)
    {
        gr[x].add(y);
        gr[y].add(x);
    }
 
    // Function to find level of each node
    static void dfs(int x, int state)
    {
        // Push the vertex in respected level
        v[state].add(x);
 
        // Make vertex visited
        vis[x] = 1;
 
        // Traverse for all it's child nodes
        for (int i : gr[x])
        {
            if (vis[i] == 0)
            {
                dfs(i, state ^ 1);
            }
        }
    }
 
    // Function to print vertices
    static void Print_vertices()
    {
        // If odd level vertices are less
        if (v[0].size() < v[1].size())
        {
            for (int i : v[0])
            {
                System.out.print(i + " ");
            }
        }
         
        // If even level vertices are less
        else
        {
            for (int i : v[1])
            {
                System.out.print(i + " ");
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        n = 4;
        m = 3;
        for (int i = 0; i < N; i++)
        {
            gr[i] = new Vector();
        }
        for (int i = 0; i < 2; i++)
        {
            v[i] = new Vector();
        }
         
        // Add edges
        add_edges(1, 2);
        add_edges(2, 3);
        add_edges(3, 4);
 
        // Function call
        dfs(1, 0);
 
        Print_vertices();
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find K vertices in
# the graph which are connected to at
# least one of remaining vertices
 
N = 200005
 
# To store graph
n, m, =0,0
vis=[0 for i in range(N)]
gr=[[] for i in range(N)]
v=[[] for i in range(2)]
 
# Function to add edge
def add_edges(x, y):
    gr[x].append(y)
    gr[y].append(x)
 
# Function to find level of each node
def dfs(x, state):
 
    # Push the vertex in respected level
    v[state].append(x)
 
    # Make vertex visited
    vis[x] = 1
 
    # Traverse for all it's child nodes
    for i in gr[x]:
        if (vis[i] == 0):
            dfs(i, state ^ 1)
 
 
# Function to prvertices
def Print_vertices():
 
    # If odd level vertices are less
    if (len(v[0]) < len(v[1])):
        for i in v[0]:
            print(i,end=" ")
    # If even level vertices are less
    else:
        for i in v[1]:
            print(i,end=" ")
 
# Driver code
 
n = 4
m = 3
 
# Add edges
add_edges(1, 2)
add_edges(2, 3)
add_edges(3, 4)
 
# Function call
dfs(1, 0)
 
Print_vertices()
 
# This code is contributed by mohit kumar 29


C#
// C# program to find K vertices in
// the graph which are connected to at
// least one of remaining vertices
using System;
using System.Collections.Generic;
 
class GFG
{
    static readonly int N = 200005;
 
    // To store graph
    static int n, m;
    static int[] vis = new int[N];
    static List[] gr = new List[N];
    static List[] v = new List[2];
 
    // Function to add edge
    static void add_edges(int x, int y)
    {
        gr[x].Add(y);
        gr[y].Add(x);
    }
 
    // Function to find level of each node
    static void dfs(int x, int state)
    {
        // Push the vertex in respected level
        v[state].Add(x);
 
        // Make vertex visited
        vis[x] = 1;
 
        // Traverse for all it's child nodes
        foreach (int i in gr[x])
        {
            if (vis[i] == 0)
            {
                dfs(i, state ^ 1);
            }
        }
    }
 
    // Function to print vertices
    static void Print_vertices()
    {
        // If odd level vertices are less
        if (v[0].Count < v[1].Count)
        {
            foreach (int i in v[0])
            {
                Console.Write(i + " ");
            }
        }
         
        // If even level vertices are less
        else
        {
            foreach (int i in v[1])
            {
                Console.Write(i + " ");
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        n = 4;
        m = 3;
        for (int i = 0; i < N; i++)
        {
            gr[i] = new List();
        }
        for (int i = 0; i < 2; i++)
        {
            v[i] = new List();
        }
         
        // Add edges
        add_edges(1, 2);
        add_edges(2, 3);
        add_edges(3, 4);
 
        // Function call
        dfs(1, 0);
 
        Print_vertices();
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2 4

时间复杂度: O(V+E)
其中 V 是顶点数,E 是图中的边数。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程