Company mergers

2.5

25 votes
Basic Programming, Basics of Implementation, Easy, Implementation
Problem

Several car companies exist in such a way that each company can control a large share of sales in different markets. You are required to perform a simulation that modifies the results after any mergers that happen between these companies.

Your task is to determine the minimum number of mergers that must be performed between the car companies to ensure that a market is controlled by no more than two separate companies.

Note: A merger between two companies results in a single company that controls the same markets as the two previous companies.

Input format

  • First line: An integer n denoting the number of car companies
  • Second line: An integer m denoting the number of markets controlled by each company
  • Each ith line of the n subsequent lines (where 0i<n): m space-separated integers denoting the market ID of the market controlled by company i

Output format
Print an integer denoting the minimum number of mergers.

Constraints
1n51m31market_id105

Sample Input
2
2
1 2
2 3
Sample Output
0
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Each of the markets is controlled by less than or equal to 2 companies. So no mergers are required.

Editor Image

?