
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to separate persons where no enemies can stay in same group in Python
Suppose we have a number n and a 2D matrix called enemies. Here n indicates there is n people labeled from [0, n - 1]. Now each row in enemies contains [a, b] which means that a and b are enemies. We have to check whether it is possible to partition the n people into two groups such that no two people that are enemies are in the same group.
So, if the input is like n = 4, enemies = [[0, 3],[3, 2]], then the output will be True, as we can have these two groups [0, 1, 2] and [3].
To solve this, we will follow these steps −
graph := an empty adjacency list
for each enemy pair(u, v) in enemies, do
insert v at the end of graph[u]
insert u at the end of graph[v]
color := a new map
Define a function dfs() . This will take u, c := 0 initially
if u is in color, then
return true when color[u] is same as c
color[u] := c
return true when all of dfs(v, c XOR 1) for each v in graph[u] are true
From the main method do the following −
return true when all of (dfs(u) for each u in range 0 to n and if u is not in color) are true
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n, enemies): from collections import defaultdict graph = defaultdict(list) for u, v in enemies: graph[u].append(v) graph[v].append(u) color = {} def dfs(u, c=0): if u in color: return color[u] == c color[u] = c return all(dfs(v, c ^ 1) for v in graph[u]) return all(dfs(u) for u in range(n) if u not in color) ob = Solution() n = 4 enemies = [[0, 3],[3, 2]] print(ob.solve(n, enemies))
Input
4, [[0, 3],[3, 2]]
Output
True
- Related Articles
- Program to form smallest number where no two adjacent digits are same in Python
- Program to check whether we can color a tree where no adjacent nodes have the same color or not in python
- Program to find minimum cost to increase heights of trees where no adjacent tree has same height in Python
- Program to find a sublist where first and last values are same in Python
- Program to find buildings from where sea can be visible in Python
- Python Group tuples in list with same first value
- Program to find index, where we can insert element to keep list sorted in Python
- Number of Ways to Stay in the Same Place After Some Steps in C++
- Program to find maximum number enemies will be killed to place a bomb in C++?
- Python Group elements at same indices in a multi-list
- Program to find out the vertical area between two points where no point lies and is the widest in Python
- Program to find number of starting point from where we can start travelling in Python
- Program to find best team with no conflicts in Python
- Program to find out the sum of numbers where the correct permutation can occur in python
- Check if all enemies are killed with bombs placed in a matrix in Python
