Time limit
2s
Memory limit
128 MB
An apple tree is a rooted tree with exactly one apple on each vertex. Every internal vertex, meaning every vertex with children, has at least two children.
A worm explores the tree from the root in DFS order. If a vertex has several children, the worm visits them from left to right in the drawing. Record 0 when a vertex is first visited, and record 1 when the worm returns from that vertex after visiting all of its children. This produces a binary string of length 2N.
Each position in the string corresponds to one vertex. A 0 corresponds to the vertex first visited at that moment, and a 1 corresponds to the vertex the worm is about to return from. One traversal record can look like this.
| 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
|---|---|---|---|---|---|---|---|---|---|
| a | b | c | d | e | |||||
| c | d | b | e | a |
If a vertex is cut away, that vertex and all of its descendants are removed. Given two positions whose corresponding vertices contain rotten apples, choose exactly one vertex to cut so that both rotten apples are removed. Among all such choices, lose as few healthy apples as possible.
Find the visit and return positions of the vertex that should be cut.
The first line contains the number of vertices N (1 ≤ N ≤ 2,000).
The second line contains the binary string of length 2N made by the worm.
The third line contains two integers X and Y. The apples on the vertices corresponding to the X-th and Y-th positions of the binary string are rotten. The two positions may refer to the same vertex.
Let Z be the vertex that should be cut. Let i be the position of the 0 recorded when Z is first visited, and let j be the position of the 1 recorded when the worm returns from Z. Print i j on the first line.