Submit | All submissions | Best solutions | Back to list |
FIRM - Goods |
There are n dealers in the market. Each of them has some unique goods (nobody else has the same goods). Besides, each of them wants to obtain some other goods, which exist in the market. This is rather strange, but for each kind of goods on the market there exists exactly one dealer who wants to obtain it.
To prevent fraud, only exchanges in pairs are allowed in this market. Moreover, each dealer is allowed to make at most one exchange a day. But the total number of transactions isn’t limited. A transaction means that all the goods of one dealer are exchanged for all the goods of the other participating dealer (partial transactions are not allowed).
You are to write a program which outputs the minimum number of days needed for each dealer to get the goods that he wants. Also output one of the possible variants of exchanges leading to this goal.
Input
The first line contains an integer n [n <= 5000]. In the second line exactly n numbers of goods are given, which the dealers require. If integer j appears as the i-th at input, then this means that goods required by dealer i are initially owned by dealer j.
Output
You must output the minimum number of days m which are needed to complete the transactions. In the next m lines you must output the way these transactions should be managed by the dealers. One line corresponds to one day. At the beginning of each line you must output the number of transactions on this day. After that output the pairs of dealers who exchange their goods on this day. Dealers in pairs are separated by '-' symbol. If there are many ways to perform the exchanges then output any of them.
Example
Input: 7 2 1 3 5 6 7 4 Output: 2 3 1-2 4-5 7-6 1 5-7
Author: Filimonenkov D.O.
Added by: | Roman Sol |
Date: | 2006-05-05 |
Time limit: | 1s-2.142s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ERL JS-RHINO NODEJS PERL6 VB.NET |
Resource: | ZCon 2007 |
hide comments
2012-10-15 05:21:18 CHA
Is that any tricky case? |
|
2012-09-07 21:57:37 akb
Awesome...:D |
|
2011-09-15 13:14:40 Vineet Kumar
i am getting wrong ans....can any1 please tell me any tricky case.. |