Codeforces Gym 100531I Instruction 构造

时间:2021-12-12 11:33:50

Problem I. Instruction

题目连接:

http://codeforces.com/gym/100531/attachments

Description

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.

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.

Input

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.

Output

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

Sample Input

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

Sample Output

6

1 2

1 4

2 4

2 6

1 6

3 7

Hint

题意

给你一个像二叉树的火车站,一开始,所有点都指向最小的节点,你按动开关就会使得一个点指向另外一个儿子

然后现在有很多个火车会从起点走到他想去的叶子节点,然后请你输出一个可行解

题解:

我们一个火车一个火车分析就好了

当火车去序号小的,就不动开关,如果去序号大的,就动开关,当他过了之后,就再动一下开关,使得又指向小的就好了

代码

#include<bits/stdc++.h>
using namespace std; int n;
map<char,int> H;
vector<int> E[3000];
vector<int>ans1[30000];
vector<int>temp1,temp2;
void solve(int x,int time,int fin)
{
//cout<<x<<" "<<time<<" "<<fin<<endl;
if(x==fin)
{
for(int i=0;i<temp1.size();i++)
ans1[temp2[i]].push_back(temp1[i]);
return;
}
for(int i=0;i<E[x].size();i++)
{
if(i==1)
{
temp1.push_back(x);
temp1.push_back(x);
temp2.push_back(time);
temp2.push_back(time+1);
}
solve(E[x][i],time+1,fin);
if(i==1)
{
temp1.pop_back();
temp1.pop_back();
temp2.pop_back();
temp2.pop_back();
}
}
}
int main()
{
freopen("instruction.in","r",stdin);
freopen("instruction.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
if(s=="s")
{
int x;
scanf("%d",&x);
E[x].push_back(i);
}
if(s=="p")
{
int x;
scanf("%d",&x);
char c;cin>>c;
H[c]=i;
E[x].push_back(i);
}
}
int q;scanf("%d",&q);
for(int i=0;i<q;i++)
{
int t;scanf("%d",&t);
char c;cin>>c;
solve(0,t,H[c]);
}
int sum = 0;
for(int i=0;i<30000;i++)
sum+=ans1[i].size();
cout<<sum<<endl;
for(int i=0;i<30000;i++)
for(int j=0;j<ans1[i].size();j++)
cout<<ans1[i][j]<<" "<<i<<endl;
}