- 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 find out the number of accepted invitations in Python

Suppose there are m boys and n girls, and m = n. There is a party incoming, and each boy has to go with a girl to that party. So, the boys invite all the girls and a girl can accept one invitation only. We have to find out the total number of invitations from the boys that the girls can accept. The input is given in a m x n matrix, where each cell position i, j denotes if the boy i has sent a letter to girl j. If a cell is 1 it means that an invitation has been sent, if it is 0 it means no invitation is sent.

So, if the input is like

1 | 0 | 0 |

1 | 0 | 1 |

1 | 1 | 0 |

then the output will be 3.

The output will be 3 if −

Girl 1 accepts boy 1's invitation.

Girl 2 accepts boy 3's invitation.

Girl 3 accepts boy 2's invitation.

(here index starts at 1)

To solve this, we will follow these steps −

- Define a function dfs() . This will take node, seen
- for nei in range 0 to N, do
- if grid[node][nei] is non-zero and seen[nei] is False, then
- seen[nei] := True
- if matching[nei] is same as -1 or dfs(matching[nei], seen) is True, then
- matching[nei] := node
- return True

- if grid[node][nei] is non-zero and seen[nei] is False, then
- return False

- for nei in range 0 to N, do
- M:= row count of grid
- N := column count of grid
- matching := a list of size N containing value -1
- res := 0
- for i in range 0 to M, do
- seen := a list of size N containing value False
- if dfs(i, seen) is True, then
- return res

- return res

## Example

Let us see the following implementation to get better understanding −

def solve(grid): M, N = len(grid), len(grid[0]) matching = [-1] * N def dfs(node, seen): for nei in range(N): if grid[node][nei] and not seen[nei]: seen[nei] = True if matching[nei] == -1 or dfs(matching[nei], seen): matching[nei] = node return True return False res = 0 for i in range(M): seen = [False] * N if dfs(i, seen): res += 1 return res print(solve([[1, 0, 0], [1, 0, 1], [1, 1, 0]]))

## Input

[[1, 0, 0], [1, 0, 1], [1, 1, 0]]

## Output

3

- Related Articles
- Program to find out the number of pairs of equal substrings in Python
- Program to Find Out the Number of Squares in a Grid in Python
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Program to find out the sum of the number of divisor of the divisors in Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Program to find out the number of special numbers in a given range in Python
- Program to find out the number of boxes to be put into the godown in Python
- Program to find out number of blocks that can be covered in Python
- Program to find out number of distinct substrings in a given string in python
- Program to find out the minimum number of intercountry travels in a road trip in Python
- Python Program to find out the number of sets greater than a given value
- Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python
- Python Program to find out the price of a product after a number of days
- Python Program to find out the number of rooms in which a prize can be hidden
- Program to Find Out the Minimal Submatrices in Python