# Find two disjoint good sets of vertices in a given graph

Given an undirected unweighted graph with **N** vertices and **M** edges. The task is to find two disjoint good sets of vertices. A set X is called good if for every edge UV in the graph at least one of the endpoint belongs to X(i.e, U or V or both U and V belongs to X).

If it is not possible to make such sets then print -1.

**Examples:**

Input :

Output :{1 3 4 5} ,{2 6}

One disjoint good set contains vertices {1, 3, 4, 5} and other contains {2, 6}.

Input :

Output :-1

**Approach:**

One of the observation is that there is no edge UV that U and V are in the same set.The two good sets form a bipartition of the graph, so the graph has to be bipartite. And being bipartite is also sufficient. Read about bipartition here.

Below is the implementation of the above approach :

`// C++ program to find two disjoint ` `// good sets of vertices in a given graph ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define N 100005 ` ` ` `// For the graph ` `vector<` `int` `> gr[N], dis[2]; ` `bool` `vis[N]; ` `int` `colour[N]; ` `bool` `bip; ` ` ` `// Function to add edge ` `void` `Add_edge(` `int` `x, ` `int` `y) ` `{ ` ` ` `gr[x].push_back(y); ` ` ` `gr[y].push_back(x); ` `} ` ` ` `// Bipartie function ` `void` `dfs(` `int` `x, ` `int` `col) ` `{ ` ` ` `vis[x] = ` `true` `; ` ` ` `colour[x] = col; ` ` ` ` ` `// Check for child vertices ` ` ` `for` `(` `auto` `i : gr[x]) { ` ` ` ` ` `// If it is not visited ` ` ` `if` `(!vis[i]) ` ` ` `dfs(i, col ^ 1); ` ` ` ` ` `// If it is already visited ` ` ` `else` `if` `(colour[i] == col) ` ` ` `bip = ` `false` `; ` ` ` `} ` `} ` ` ` `// Function to find two disjoint ` `// good sets of vertices in a given graph ` `void` `goodsets(` `int` `n) ` `{ ` ` ` `// Initially assume that graph is bipartie ` ` ` `bip = ` `true` `; ` ` ` ` ` `// For every unvisited vertex call dfs ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `if` `(!vis[i]) ` ` ` `dfs(i, 0); ` ` ` ` ` `// If graph is not bipartie ` ` ` `if` `(!bip) ` ` ` `cout << -1; ` ` ` `else` `{ ` ` ` ` ` `// Differentiate two sets ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `dis[colour[i]].push_back(i); ` ` ` ` ` `// Print vertices belongs to both sets ` ` ` ` ` `for` `(` `int` `i = 0; i < 2; i++) { ` ` ` ` ` `for` `(` `int` `j = 0; j < dis[i].size(); j++) ` ` ` `cout << dis[i][j] << ` `" "` `; ` ` ` `cout << endl; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6, m = 4; ` ` ` ` ` `// Add edges ` ` ` `Add_edge(1, 2); ` ` ` `Add_edge(2, 3); ` ` ` `Add_edge(2, 4); ` ` ` `Add_edge(5, 6); ` ` ` ` ` `// Function call ` ` ` `goodsets(n); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 3 4 5 2 6

## Recommended Posts:

- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Find maximum number of edge disjoint paths between two vertices
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Find if there is a path between two vertices in a directed graph
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Finding in and out degrees of all vertices in a graph
- Articulation Points (or Cut Vertices) in a Graph
- Construct a graph from given degrees of all vertices
- Minimum number of edges between two vertices of a Graph
- Maximum and minimum isolated vertices in a graph
- Number of Simple Graph with N Vertices and M Edges
- Minimum number of edges between two vertices of a graph using DFS
- Check whether given degrees of vertices represent a Graph or Tree
- Largest subset of Graph vertices with edges of 2 or more colors

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.