Hide

Problem J
Money Orbits

/problems/moneyorbits/file/statement/en/img-0001.jpg
“Coins before Euro - European Coins In Circulation” by Istvan Takacs on Wikimedia, CC-BY-SA 3.0 Unported

The great market in Consumeron Prime is known around the galaxy for being a great place to shop. Every time there is an intergalactic eclipse festival, merchants from up to one hundred thousand different planets descend on the market to sell their goods, often at a steep discount.

Of course, the lack of a galactic currency can make buying goods tricky: most merchants only accept the currency of their own planet as payment. Fortunately, many money-changing booths exist around the market to help you convert your own currency into the currency you need to make your purchase.

Each money-changing booth accepts multiple currencies, but will produce only one specific currency in exchange. For example, the Zebulon Dollars booth will take Breel Coins, Mulvarian Duckets, and Praxobucks, converting each of them into Zebulon Dollars, but it will not convert the other way. If you want to convert your Zebulon Dollars into Praxobucks, you will have to go to the Praxobucks booth.

This process is made more complicated by the fact that not every money-changing booth accepts every currency. In some cases, getting the currency you need may require multiple exchanges — for example, changing Orpon Dollars into Breel Coins, then Breel coins into Zebulon Dollars. Even worse, some types of currency may be impossible to convert to others. Many customer complaints have arisen from beings who have converted their money into one currency, only to find they cannot change it back.

In order to simplify the whole shopping process, the rulers of Consumeron Prime have decided to limit the number of currencies allowed at the great market. In particular, they would like to allow as many currencies as possible while still ensuring that any of the allowed currencies may be exchanged to any of the other allowed currencies—even a sequence of multiple exchanges is required to do so.

You are to write a program that looks at the currencies currently in use at the market and what exchanges are possible, then reports the largest subset of currencies that would allow any currency in the subset to be exchanged into any other currency in the subset.

Input

The first line of input contains a single integer $1 \leq c \leq 10^5$, the number of currencies. Each of the next $c$ lines describes a single money-changing booth. The $i$th line begins with an integer $0 \leq k_ i < c$, the number of currencies accepted by the money-changing booth that produces currency $i$. The rest of the $i$th line consists of $k_ i$ distinct integers indicating the currencies accepted by booth $i$ (the currencies are numbered $1 \dots c$). Note that a booth will never accept the same currency it produces. It is also guaranteed that the sum of all $k_ i$ will be at most $2 \cdot 10^5$.

Output

Output a single integer, the size of the largest set of currencies such that every currency in the set may be exchanged into any other.

Sample Input 1 Sample Output 1
3
1 2
1 3
1 2
2
Sample Input 2 Sample Output 2
8
1 2
1 3
2 1 4
2 3 5
1 4
2 4 8
1 6
1 7
5

Please log in to submit a solution to this problem

Log in