National Instruments Interview Experience | Set 2

Interplanetary trade:
Hober Mallow wants to setup an inter-galactic trade which would allow trade between different planets. Each planet has its own set of currencies in which they trade. As there are too many currencies, Hober decided to declare a set of M currencies (numbered from 1 to M) as the official currencies that can be used to trade between planets. Two planets can trade if and only if they have a common official currency or if there is another planet with which each of these planets share a common official currency. For example, let’s say planet A has currencies – 1, 3, 4, planet B has currencies 5, 6 and planet C has 6, 4. It is still possible for planet A and B to trade via planet C (planet A -> C via currency 4 and then planet C ->B via currency 6 and vice versa). This type of trade can be done with more than one planets acting as intermediary as well (i.e. planet X and Y can trade via A, B, C if XA share a common currency, AB share a common currency, BC share a common currency and finally CY share some common currency). Hober observes that even with these rules there are planets which have no official currencies or planet groups which do not share any currency with other planet groups. To enable trade among all the planets he has to implement system of new currency for each of such planets which have no official or common official currencies. Implementing a new currency system costs him 1 platinum unit per planet. You have to help Hober minimize the cost for enabling trade between all the planets.

You will be given N (2 <= N <= 50) which indicates number of planets that want to be part of the inter-galactic trade. You will be given M (2 <= M <= 50) which indicates the number of official currency numbered from 1 to M. This will be followed by N lines, each consisiting of a numberCi, the number of official currencies supported by planet i, followed by C currencies which are in trade for the ith planet. You have to output the number of platinum units required to enable trade across all planets.

Example Inputs

5 5
1 2
2 2 3
2 3 4
2 4 5
1 5

5 5
0
3 4 1 5
2 3 4
0
1 2
Expected Outputs

0
Planets #1 and #2 can trade through currency 2. Planets #2 and #3 can trade through currency 3. Planets #3 and #4 can trade using 4. Planets #4 and #5 can trade using currency 5. This makes it possible to trade between any two planets via zero or more intermediary planets. Thus there is no need to setup a new currency and hence the cost is 0.
3
Planet #2 and #3 can trade through currency 4, but planets #1, #4 and #5 do not share any currency with one another or with #2 and #3 and hence can not trade with anyone else. One of currencies from planets #2 or #3 can be set up in planets #1, #4 and #5 which would make trade possible among all the planets. As currency will have to be setup on 3 difference planets, the cost is 3.

Example Inputs and Outputs
Testcase Input Output Download Link Run Link(Also saves the code)
1
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
0
Download Input File Run against code
2
5 5
0
3 4 1 5
2 3 4
0
1 2
3
Download Input File Run against code
Time limit per test case:

2 second(s)

If you have a machine with GCC/G++ and want to work offline, download the template here and submit

Code templates:

C Template: Trade.c

C++ Template: Trade.cpp

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

Write your Interview Experience or mail it to contribute@geeksforgeeks.org



My Personal Notes arrow_drop_up