You have two rooted undirected trees, each contains
n vertices. Let's number the vertices of each tree with integers from
1 to
n. The root of each tree is at vertex
1. The edges of the first tree are painted blue, the edges of the second one are painted red. For simplicity, let's say that the first tree is blue and the second tree is red.
Edge
{x,y} is called bad for edge
{p,q} if two conditions are fulfilled:
-
The color of edge {x,y} is different from the color of edge {p,q}.
-
Let's consider the tree of the same color that edge {p,q} is. Exactly one of vertices x, y lies both in the subtree of vertex p and in the subtree of vertex q.
In this problem, your task is to simulate the process described below. The process consists of several stages:
-
On each stage edges of exactly one color are deleted.
-
On the first stage, exactly one blue edge is deleted.
-
Let's assume that at the stage i we've deleted edges {u1,v1}, {u2,v2}, ..., {uk,vk}. At the stage i+1 we will delete all undeleted bad edges for edge {u1,v1}, then we will delete all undeleted bad edges for edge {u2,v2} and so on until we reach edge {uk,vk}.
For each stage of deleting edges determine what edges will be removed on the stage. Note that the definition of a bad edge always considers the initial tree before it had any edges removed.
Note
For simplicity let's assume that all edges of the root tree received some direction, so that all vertices are reachable from vertex
1. Then a
subtree of vertex
v is a set of vertices reachable from vertex
v in the resulting directed graph (vertex
v is also included in the set).