atcoder#ABC225D. [ABC225D] Play Train
[ABC225D] Play Train
Score : points
Problem Statement
Takahashi is playing with toy trains, connecting and disconnecting them. There are toy train cars, with car numbers: Car , Car , , Car . Initially, all cars are separated.
You will be given queries. Process them in the order they are given. There are three kinds of queries, as follows.
1 x y
: Connect the front of Car to the rear of Car . It is guaranteed that:- just before this query, no train is connected to the rear of Car ;
- just before this query, no train is connected to the front of Car ;
- just before this query, Car and Car belong to different connected components.
2 x y
: Disconnect the front of Car from the rear of Car . It is guaranteed that:- ;
- just before this query, the front of Car is directly connected to the rear of Car .
3 x
: Print the car numbers of the cars belonging to the connected component containing Car , from front to back.
Constraints
- All values in input are integers.
- All queries satisfy the conditions in the Problem Statement.
- The queries of the format
3 x
ask to print at most car numbers in total.
Input
Input is given from Standard Input in the following format:
The -th query begins with an integer (, , or ) representing the kind of the query, followed by and if or , and followed by if .
In short, each query is in one of the following three formats:
Output
If a query with asks to print the values , output the following line:
Your output should consist of lines, where is the number of queries with . The -th line should contain the response to the -th such query.
7 14
1 6 3
1 4 1
1 5 2
1 2 7
1 3 5
3 2
3 4
3 6
2 3 5
2 4 1
1 1 5
3 2
3 4
3 6
5 6 3 5 2 7
2 4 1
5 6 3 5 2 7
4 1 5 2 7
1 4
2 6 3
The figure below shows the cars when the first queries are processed. For example, Car belongs to the same connected component as Cars , which is different from the connected component containing Cars .
The figure below shows the cars when the first queries are processed.