Problem J
Jaðarjuð
Languages
en
is
For the last few years, the prospect of merging some of the universities of Iceland has been considered. Finally, some actual plans are being drawn about how this could be implemented, but in order to make good plans the organising committee needs some data on how the plans are looking thus far as they plan it out. After hearing about this programming contest and nagging the jury incessantly, it is decided that the contestants can take care of this not at all tedious task.
Input
The input begins with a line containing two integers, $n$ which gives the number of universities and $q$ which gives the number of operations. You may assume that $1 \leq n, q \leq 5 \cdot 10^5$. Next there are $q$ lines, each giving one operation. They each begin with one of the letters j, u or p. If the line begins with j it is followed by two integers $1 \leq a, b \leq n$ with $a \neq b$. This means university number $a$ and university number $b$ should be merged. If the line begins with u the last merger should be reversed. If no merger is available to be undone, the operation should do nothing. Finally if the line begins with p a single integer $1 \leq a \leq n$ follows, and all universities currently merged with university number $a$ should be printed on a single line, including $a$ itself. Note that if $a$ is merged with $b$ and $b$ merged with $c$, $a$ will be considered to have been merged with $c$.
Output
For each operation starting with a p a line should be printed, as described above. The integers on the line should be separated by spaces, but they can be printed in any internal order. The lines themselves should be printed in the same order as the operations are given in the input. You may assume you will not have to print more than $10^6$ numbers.
Sample Input 1 | Sample Output 1 |
---|---|
4 11 p 3 j 1 2 j 3 4 p 2 j 1 3 j 1 2 p 2 u u u p 4 |
3 1 2 1 2 3 4 4 |