TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

After an adventure, Farnsworth has decided to mass-manufacture and market his mind/body-switching device. Given any pairing of bodies, the device can switch the minds inhabiting them once -- the device will not operate on the same pairing of bodies a second time.

In light of this design flaw, Tate and Dixon (two Globetrotters) sense a very lucrative consulting opportunity. Once a group of users can no longer stand the confusion and wish for their proper mind/body relationships to be restored, Tate and Dixon offer their services (at a very competitive and not at all exploitative market rate, mind you) by using their own bodies to help, Tate and Dixon then switch everybody's mind back into their proper body (Note that each group of customers uses a different copy of the mind-switching device, so that Tate and Dixon's bodies can switch minds once per group of customers).

After a few months, Tate and Dixon hire you to help with the sorting problems---it's hard for them to keep their own minds on it nowadays. Your task is as follows: given a sequence of mind/body swaps, provide another subsequent sequence of swaps such that all swaps (new and old, combined) are distinct and each body ends up with its rightful mind.

Warn: 本題有 specialjudge

Input Format

Each input case begins with two space-separated numbers N and M on a single line, where 1<= N,M<= 100000. The number N denotes the number of customers, labelled 0,1,...,N-1; Tate and Dixon are numbered N and N+1, respectively. Also, M denotes the number of swaps already performed by the device. The next line consists of 2M space-separated numbers:

a1, b1, a2, b2, ..., aM, bM.

For each 1<= i<= M, this means that bodies ai and bi have switched minds on the i-th swap. You may assume that neither Tate nor Dixon is involved in these swaps, and the given swaps are all distinct.

Input is followed by a single line with N=0, which should not be processed.

Output Format

For each test case, output on a single line any sequence of space-separated numbers

a1, b1, a2, b2, ..., ak, bk

that describes a valid sequence of swaps restoring bodies to their rightful minds. You are allowed at most 5000000 swaps per test case. To indicate no swaps, simply output a blank line on its own.

Sample Input 1

4 3
0 1 2 3 0 2
4 6
0 1 2 3 0 2 1 3 0 3 1 2
100000 0

100000 1
0 1
100000 1
0 1
0

Sample Output 1

1 3 0 3 1 2

0 1 2 3 0 2 1 3 0 3 1 2
2 3 0 2 1 3 0 3 1 2
100001 3 0 100001 1 3 0 3 1 100001

Hints

Problem Source

Migrated from old NTUJ.

2010 Stanford Local Round 2

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 3000 262144 40000