#arc172f. F - Walking
F - Walking
问题陈述
ATCoder的王国有 个交叉点,用从 到 的数字标记。
此外,沙特王国有三种单向道路:
-**类型A:**对于每个 ,从十字路口 到十字路口 有一条路。-**类型B:**对于每个 ,都有一条从交叉口 到交叉口 的道路。
-**类型C:**对于每个 ,有一条道路连接方向为 的交叉口 和交叉口 。
这里, 是 X
或 Y
,其中方向 X
表示道路从交叉口 到交叉口 ,而方向 Y
表示它从交叉点 到交叉点 。Takahashi想要走几次,这样对于每个 ,连接交叉口 和 的C类道路方向为 。>**什么是散步?
**
从任意交叉点开始,重复以下操作零次或多次:
- 如果可以从当前路口,沿着Type-C道路走到下一个路口。>- 否则,如果可以从当前交叉路口进入A类或B类道路,则沿该道路走到下一个交叉路口。
然后,反转所经过的所有C类道路的方向。
计算实现目标所需的最小步行次数,并提供一种在最小步行次数内实现目标的方法。
在这个问题的约束条件下,可以证明在有限的行走次数内可以达到目标。
以上为机器翻译结果,仅供参考。
Problem Statement
The Kingdom of AtCoder has intersections labeled with numbers from to . Additionally, there are three types of one-way roads in the kingdom:
- Type A: For each , there is a road from intersection to intersection .
- Type B: For each , there is a road from intersection to intersection .
- Type C: For each , there is a road connecting intersection and intersection with direction .
Here, is X
or Y
, where direction X
means the road goes from intersection to intersection , and direction Y
means it goes from intersection to intersection .
Takahashi wants to walk several times so that for each , the direction of Type-C road connecting intersections and will be .
What is a walk?
Start at any intersection and repeat the following action zero or more times:
- If it is possible to proceed to a Type-C road from the current intersection, walk along the Type-C road to the next intersection.
- Otherwise, if it is possible to proceed to a Type-A or B road from the current intersection, walk along that road to the next intersection.
Then, reverse the directions of all Type-C roads that were passed.
Calculate the minimum number of walks needed to achieve the goal and provide one method to achieve the goal in the minimum number of walks. Under the constraints of this problem, it can be proved that the goal can be achieved in a finite number of walks.
Constraints
- is an integer such that .
- Each of is
X
orY
. - Each of is
X
orY
.
Input
The input is given from Standard Input in the following format:
Output
Print your solution in the following format, where is the number of walks, and the -th walk starts at intersection and finishes at intersection .
Sample Input 1
5
XYXYX
XXYXX
Sample Output 1
1
2 7
In this sample input, Takahashi will walk as follows:
- Start at intersection .
- Since there is no Type-C road to proceed from intersection , walk along the Type-B road to intersection .
- Since there is a Type-C road to proceed from intersection , walk along the Type-C road to intersection .
- Since there is no Type-C road to proceed from intersection , walk along the Type-A road to intersection .
- Since there is a Type-C road to proceed from intersection , walk along the Type-C road to intersection .
- Since there is no Type-C road to proceed from intersection , walk along the Type-B road to intersection .
- Since there is a Type-C road to proceed from intersection , walk along the Type-C road to intersection .
- Finish at intersection .
This walk passes through the following three Type-C roads:
- The road connecting intersection and intersection .
- The road connecting intersection and intersection .
- The road connecting intersection and intersection .
The directions of these roads are reversed, so the directions of the roads connecting intersections and for are now X
, X
, Y
, X
, X
, respectively, achieving the goal.
Sample Input 2
5
XXYYX
XXYYX
Sample Output 2
0
Note that the goal is already achieved at the beginning, so there is no need to walk even once.
Sample Input 3
5
XXXXX
YYYYY
Sample Output 3
5
1 2
3 4
5 6
7 8
9 10
Sample Input 4
20
XXXYXYYXXXYXXXXYYXXY
XXYXYYXXYXXYXYXYXYXY
Sample Output 4
5
14 18
29 38
14 26
5 10
27 35
Sample Input 5
20
YXYXYXYYYXYYXYYXYXXX
XXXXXYXYYYXYYXXYYYXY
Sample Output 5
5
29 36
10 38
2 3
4 7
28 40