Instruction | 프로그래밍의 벗 PivotOJ
PivotOJ

Instruction

시간 제한: 2000ms메모리 제한: 256MB출처: NEERC Northern Subregional 2014BOJ 10380
이 문제는 본문 이미지 일부가 표시되지 않습니다. 텍스트만으로 풀이가 어려울 수 있습니다.

문제

Ingrid is a head of a big railway station and, among other duties, is responsible for routing trains to the right platforms. The station has one entrance, and there are many switches that direct trains to other switches and platforms.

Each switch has one inbound track and two outbound tracks, platforms have one inbound track, and station entrance has one outbound track. Each outbound track is connected to one inbound track and vice versa. Every switch and platform is reachable from station entrance.

Platforms have a rail dead ends and you may assume that trains disappear from the platform immediately after arriving to it.

[이미지 1]

Each morning Ingrid looks at the timetable and writes switch toggling instruction: when and which switch to toggle. She would like to automate this process to save a lot of time.

입력

The first line of the input file contains a single integer n — the total number of switches and platforms on the station (3 ≤ n ≤ 51).

The i-th of the following n lines describes a switch or a platform with an index i. Description starts with a character ‘p’ for a platform or ‘s’ for a switch. Next number qi indicates the number of the switch the inbound track is connected to or 0 if it is connected to station entrance (0 ≤ qi < i). Description of the platform also contains a unique lowercase English letter — the platform identifier.

Trains spend exactly one minute to move between two connected switches or a switch and a platform. In the morning, each switch is toggled in a way that a train would pass to the one of the two outbound tracks connected to the switch/platform with the lower number.

Next line of the input file contains a single integer m (1 ≤ m ≤ 1000) — the number of trains in timetable.

Each of the following m lines contains integer ai (0 ≤ ai ≤ 10 000; ai > ai−1) — the time in minutes when a train arrives to the station entrance, and the letter pi — identifier of the destination platform for this train.

출력

In the first line output integer c — the number of commands in the switch toggling instruction. For each command, output two integers si and ti (1 ≤ si ≤ n; 0 ≤ ti ≤ 109) — the number of the switch and the time to toggle it. Assume that the switch is toggled between minutes ti − 1 and ti.

Output commands in order of non-decreasing time. The number of commands should not exceed 100 000.

힌트

Below is the time trace for the first example.

[이미지 2]

예제

예제 1

입력
7
s 0
s 1
s 1
p 2 a
p 2 b
p 3 c
p 3 d
5
0 a
1 c
3 b
4 a
5 d
출력
6
1 2
1 4
2 4
2 6
1 6
3 7

예제 2

입력
5
s 0
p 1 y
s 1
p 3 z
p 3 x
3
7 y
8 y
15 y
출력
0

예제 3

입력
3
s 0
p 1 y
p 1 z
3
7 y
8 y
10 y
출력
5
1 1
1 2
1 2
1 3
1 200
이 문제는 채점 준비 중입니다. 테스트 데이터가 확보되면 제출이 가능합니다.