Chef in a Mysterious Land (Challenge)

All submissions for this problem are available.
Read problems statements in Hindi, Mandarin chinese and Vietnamese as well.
Chef arrived to a mysterious land. This land consists of $N$ worlds (numbered $1$ through $N$), called *ages*. Chef can travel from an age to another one by the process of *linking*; this process is facilitated by *linking books*. Each linking book allows Chef to travel to a specific age from any age. There may be multiple linking books that link to the same age; all such books are indistinguishable from each other. Each linking book contains a *linking panel*. Whenever Chef touches the linking panel, he and everything he is carrying is instantly teleported to the age this book links to. Note that the linking book you use to teleport is not carried along with him — it remains in the current age. However, Chef can carry at most one other linking book with him when teleporting. During his journey across the ages, Chef encountered a helpful man named Atrus. This man gave Chef a map of the current locations of all linking books. In exchange for this information, Atrus wants Chef to complete a task: for each age, the books on the map which link to this age have a list of *ideal ages* where they should be located; Chef should move some linking books to other ages in such a way that they are all located in ideal ages. Chef starts in age $1$; help him complete Atrus' task by linking as few times as possible. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains a single integer $N$.  $N$ lines follow. For each valid $i$, the $i$th of these lines contains an integer $B$ denoting the number of linking books that link to age $i$, followed by a space and $2B$ spaceseparated integers.  The first $B$ of these integers denote the ages where books linking to age $i$ are currently located. These integers are not necessarily different; if the number of some age appears among them $x$ times, there are $x$ books in that age linking to age $i$.  The next $B$ integers denote the ages which Atrus considers ideal for these linking books. These integers are not necessarily different either; if the number of some age appears among them $x$ times, there should be $x$ books in that age linking to age $i$ at the end. ### Output For each test case, print a line containing one integer $L$ — the number of times Chef has to link to other ages in your solution. Then, print $L$ lines describing Chef's linking strategy. Each of these lines should contain two spaceseparated integers — the age Chef should link to from his current age and the linking age of the book Chef should carry (or $0$ if Chef should not carry any book during this link). ### Constraints  $T = 5$  $30 \le N \le 100$  $1 \le B \le 30$  in the initial configuration, it is possible to reach any age from any other age  in the ideal configuration, each age contains at least two books that do not link to this age  it is always possible to reach the ideal configuration from the initial configuration ### Example Input ``` 1 4 1 4 4 2 1 3 3 1 3 1 2 1 4 1 2 3 4 4 3 1 2 3 ``` ### Example Output ``` 6 3 3 4 3 1 4 4 0 3 4 2 4 ``` ### Explanation Note that while the example input does not satisfy the constraints on $T$ and $N$, this input does not appear in the actual test data. **Example case 1:**  Initially, there are two linking books located in age $1$ that link to age $3$. Chef links using one of these books while carrying the other one.  Then, Chef links to age $4$ from age $3$, still carrying the same book as in the previous step.  In age $4$, there are two books that link to age $4$ itself. Chef links to age $1$, carrying one of them.  Then, Chef returns to age $4$ using the book he just carried, without carrying another book.  Finally, Chef takes the other book in age $4$ that links to age $4$, carries it to age $3$ and then to age $2$. ### Scoring The score for each test case is $L / N$, where $L$ is the number of links in your solution. The score for each test file is the average of scores for the individual test cases. The score of a submission is the sum of scores for all test files. For the only test case in the example output, the score would be $6 / 4 = 1.5$. ### Test Generation Process There are twenty test files. During the contest, the displayed score will account for exactly four test files, i.e. your score reflects your submission's performance on 20% (4/20) of the test files. However, if your program gets a nonAC verdict on any test file, your submission's verdict will be nonAC. In other words, an AC verdict denotes that your program runs successfully on all the test files. After the end of the contest, your score will be changed to include the sum of your program's scores over the other sixteen test files also. In each test file, 5 test cases are generated independently. In each of these test cases, $N$ is chosen uniformly randomly between $30$ and $100$ (inclusive). Then, a real number $P$ is chosen uniformly randomly between $2$ and $5$. Next, all $B$s are generated. Each of them is selected independently from a geometric distribution on the range $[1, 30]$ with mean $P$. As long as the sum of all $B$s is less than $2N$, the generation of $B$s is restarted with the same value of $P$. Next, the initial configuration is generated uniformly randomly among all configurations satisfying the constraints. Finally, the ideal configuration is generated uniformly randomly among all configurations satisfying the constraints.Author:  pieguy 
Tags  pieguy 
Date Added:  17072018 
Time Limit:  5 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 