给定一个整数N ,表示无向图中存在的节点数,每个节点的值从1到N,以及一个二维数组Edges[][] ,表示由边连接的顶点对,任务是找到一组至多N/2 个节点,使得该集合中不存在的节点与该集合中存在的任何一个节点相邻连接。
例子 :
Input: N = 4, Edges[][2] = {{2, 3}, {1, 3}, {4, 2}, {1, 2}}
Output: 3 2
Explanation: Connections specified in the above graph are as follows:
1
/ \
2 – 3
|
4
Selecting the set {2, 3} satisfies the required conditions.
Input: N = 5, Edges[][2] = {{2, 1}, {3, 1}, {3, 2}, {4, 1}, {4, 2}, {4, 3}, {5, 1}, {5, 2}, {5, 3}, {5, 4}}
Output: 1
方法:根据以下观察可以解决给定的问题:
- 假设一个节点是源节点,那么每个顶点到源节点的距离要么是奇数要么是偶数。
- 根据奇偶性将节点分成两个不同的集合,其中至少一个集合的大小不会超过N/2 。由于某个奇偶校验的每个节点至少连接到一个相反奇偶校验的节点,因此满足最多选择 N/2 个节点的标准。
请按照以下步骤解决问题:
- 假设任何顶点都是源节点。
- 初始化两个集合,例如evenParity和oddParity,以分别存储与源节点具有偶数和奇数距离的节点。
- 对给定图执行 BFS 遍历,并根据顶点与源的距离奇偶性将顶点拆分为两个不同的集合:
- 如果每个连接的节点到源节点的距离是奇数,则将当前节点插入到集合oddParity 中。
- 如果每个连接节点到源节点的距离是偶数,则将当前节点插入到集合evenParity 中。
- 完成上述步骤后,以最小尺寸打印集合的元素。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to add an edge
// to the adjacency list
void addEdge(vector >& adj,
int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
// Function to perform BFS
// traversal on a given graph
vector > BFS(
int N, int source,
vector > adjlist)
{
// Stores the distance of each
// node from the source node
int dist[N + 1];
vector > vertex_set;
// Update the distance of all
// vertices from source as -1
memset(dist, -1, sizeof dist);
// Assign two seperate vectors
// for parity odd and even parities
vertex_set.assign(2, vector(0));
// Perform BFS Traversal
queue Q;
// Push the source node
Q.push(source);
dist = 0;
// Iterate until queue becomes empty
while (!Q.empty()) {
// Get the front node
// present in the queue
int u = Q.front();
Q.pop();
// Push the node into vertex_set
vertex_set[dist[u] % 2].push_back(u);
// Check if the adjacent
// vertices are visited
for (int i = 0;
i < (int)adjlist[u].size(); i++) {
// Adjacent node
int v = adjlist[u][i];
// If the node v is unvisited
if (dist[v] == -1) {
// Update the distance
dist[v] = dist[u] + 1;
// Enqueue the node v
Q.push(v);
}
}
}
// Return the possible set of nodes
return vertex_set;
}
// Function to find a set of vertices
// of at most N/2 nodes such that each
// unchosen node is connected adjacently
// to one of the nodes in the set
void findSet(int N,
vector > adjlist)
{
// Source vertex
int source = 1;
// Store the vertex set
vector > vertex_set
= BFS(N, source, adjlist);
// Stores the index
// with minimum size
int in = 0;
if (vertex_set[1].size()
< vertex_set[0].size())
in = 1;
// Print the nodes present in the set
for (int node : vertex_set[in]) {
cout << node << " ";
}
}
// Driver Code
int main()
{
int N = 5;
int M = 8;
vector > adjlist;
adjlist.assign(N + 1, vector(0));
// Graph Formation
addEdge(adjlist, 2, 5);
addEdge(adjlist, 2, 1);
addEdge(adjlist, 5, 1);
addEdge(adjlist, 4, 5);
addEdge(adjlist, 1, 4);
addEdge(adjlist, 2, 4);
addEdge(adjlist, 3, 4);
addEdge(adjlist, 3, 5);
// Function Call to print the
// set of at most N / 2 nodes
findSet(N, adjlist);
return 0;
}
Python3
# Python3 program for the above approach
from collections import deque
# Function to add an edge
# to the adjacency list
def addEdge(adj, u, v):
adj[u].append(v)
adj[v].append(u)
return adj
# Function to perform BFS
# traversal on a given graph
def BFS(N, source, adjlist):
# Stores the distance of each
# node from the source node
dist = [-1]*(N + 1)
vertex_set = [[] for i in range(2)]
# Perform BFS Traversal
Q = deque()
# Push the source node
Q.append(source)
dist = 0
# Iterate until queue becomes empty
while len(Q) > 0:
# Get the front node
# present in the queue
u = Q.popleft()
# Push the node into vertex_set
vertex_set[dist[u] % 2].append(u)
# Check if the adjacent
# vertices are visited
for i in range(len(adjlist[u])):
# Adjacent node
v = adjlist[u][i]
# If the node v is unvisited
if (dist[v] == -1):
# Update the distance
dist[v] = dist[u] + 1
# Enqueue the node v
Q.append(v)
# Return the possible set of nodes
return vertex_set
# Function to find a set of vertices
# of at most N/2 nodes such that each
# unchosen node is connected adjacently
# to one of the nodes in the set
def findSet(N, adjlist):
# Source vertex
source = 1
# Store the vertex set
vertex_set = BFS(N, source, adjlist)
# Stores the index
# with minimum size
inn = 0
if (len(vertex_set[1]) < len(vertex_set[0])):
inn = 1
# Print the nodes present in the set
for node in vertex_set[inn]:
print(node, end=" ")
# Driver Code
if __name__ == '__main__':
N = 5
M = 8
adjlist = [[] for i in range(N+1)]
# Graph Formation
adjlist = addEdge(adjlist, 2, 5)
adjlist = addEdge(adjlist, 2, 1)
adjlist = addEdge(adjlist, 5, 1)
adjlist = addEdge(adjlist, 4, 5)
adjlist = addEdge(adjlist, 1, 4)
adjlist = addEdge(adjlist, 2, 4)
adjlist = addEdge(adjlist, 3, 4)
adjlist = addEdge(adjlist, 3, 5)
# Function Call to print the
# set of at most N / 2 nodes
findSet(N, adjlist)
# This code is contributed by mohit kumar 29.
输出:
1 3
时间复杂度: O(N + M)
辅助空间: O(N + M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live