Cactus Transformation
Time limit3sMemory limit2048 MB
Decide whether one cactus graph can be turned into another by repeatedly removing an edge and adding a new one, at most 15000 times, and print the moves.
- Level
Hard9 of 10
- Topics
- Graph, Tree, DFS, Implementation
- Solved
- No attempts yet
Problem
In the university, Caroline started to learn about cactus graphs. Her teacher wanted to check whether the students really understood the definition of a cactus, so he gave them the following home assignment.
You are given two cactuses with the same number of vertices and edges. Determine whether the first cactus can be transformed into the second one using the following two-step operation at most times:
- Pick an arbitrary edge from the first cactus and remove it. After this step, the graph does not have to be a cactus.
- Add an arbitrary non-existing edge to the first graph so that the graph becomes a cactus.
Each operation consists of both steps, so you must perform both steps every time.
It is guaranteed that if the transformation is possible, it can be done using at most operations.
A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. A cactus has no multiple edges and no loops.
Two cactuses are called the same if for every pair of vertices and (), either both cactuses have the edge or neither does.
Input
The first line contains two integers and (, ), the number of vertices and edges in each cactus. Each of the next lines contains two integers and (), an edge. The first lines describe the first cactus, and the next lines describe the second cactus.
Output
If the first cactus cannot be transformed into the second one, print a single line with "NO".
Otherwise, print "YES" on the first line. On the second line, print the number of operations (). Each of the following lines contains four integers , , , (). The vertices and are the endpoints of the removed edge, and and are the endpoints of the added edge.