#P9966. [THUPC 2024 初赛] 机器人

[THUPC 2024 初赛] 机器人

Problem Description

There are nn robots standing in a circle, numbered 0n10 \sim n-1 in counterclockwise order.

Each robot has two hands. Initially, Robot ii has its “left hand” pointing to Robot lil_i, and its “right hand” pointing to Robot rir_i.

Inside every robot, there are mm lines of “commands”. The “commands” have the following forms.

Commands

“Commands” are divided into “basic commands” and “advanced commands”. “Advanced commands” are more complex, but they are essentially not very different. Below we describe the format of these “commands” and what happens when they are “executed”. In the statement, the word “itself” always refers to the robot that owns this “command”.

Basic Commands

  • SLACKOFF: “slack off”, i.e. do nothing.
  • MOVE h z: Move the hh-th hand counterclockwise by zz robots. When h=0h=0 it means the “left hand”, and when h=1h=1 it means the “right hand”, same below.
  • SWAP h x y: Swap the xx-th line of “command” of the robot pointed to by the hh-th hand with its own yy-th line of “command”.
  • MIRROR h x: “Mirror” (flip) the xx-th line of “command” of the robot pointed to by the hh-th hand, i.e. flip hh in that “command” (00 becomes 11, 11 becomes 00). In particular, it has no effect on the SLACKOFF command; and for the TRIGGER command, it directly modifies hh inside the “command” that will be “executed” when “triggered”.
  • REPLACE h x <COMMAND>: Replace the xx-th line of “command” of the robot pointed to by the hh-th hand with <COMMAND>, where <COMMAND> is a complete “command”.

Advanced Commands

  • ACTIVATE h: “activate” the robot pointed to by the hh-th hand, i.e. “execute” all of that robot’s “commands” in order. A later line is executed only after the previous line finishes executing. Note that when executing earlier “commands”, later “commands” may be changed; in that case, the changed “commands” should be executed. Only after all of that robot’s “commands” have been executed is this “command” considered finished.

  • TRIGGER <COMMANDNAME>: <COMMAND>: Here <COMMANDNAME> is the name of a “command”, i.e. the first all-caps word in a “command”; <COMMAND> is a complete “basic command”. A TRIGGER command will not be “executed”, i.e. it is skipped during sequential “execution”. However, when some other robot finishes “executing” a “command”, and its “right hand” is pointing to itself, then its earliest TRIGGER command (if any) that satisfies the following conditions will be “triggered”, and the corresponding <COMMAND> is “executed” once:

    • If <COMMANDNAME> is not TRIGGER, then the “command” that has just finished “executing” is a <COMMANDNAME> command;
    • If <COMMANDNAME> is TRIGGER, then the “command” that has just finished “executing” is the “command” executed when a TRIGGER command is “triggered”.

    After it finishes “executing”, it returns to the original “execution” order.

You need to start from Robot 00 and “activate” these robots in increasing order, round and round, and output information about the first kk executed commands.

Input Format

The first line contains three positive integers n,m,kn, m, k.

Then, the information of the nn robots is given in increasing order of their indices.

For each robot, the first line contains two non-negative integers li,ril_i, r_i, indicating the robot index pointed to by its “left hand” and by its “right hand”.

The next mm lines give the robot’s “commands” in order. The format of “commands” is as described in the statement.

Output Format

Output kk lines, describing the relevant information of the first kk commands that begin “executing”, in order. Output before each command starts “executing”, one per line, in the following format:

  • When “slacking off”, output Robot <id> slacks off.. Here <id> is an integer, the index of the robot “executing” the current “command”, same below.
  • When “moving”, output Robot <id> moves its <side> hand towards Robot <id2>.. Here <side> is left or right, indicating which hand is moved (left for “left hand”, right for “right hand”); <id2> is an integer, the index of the robot that this hand points to after moving.
  • When “swapping”, output Robot <id> swaps a line of command with Robot <id2>.. Here <id2> is an integer, the index of the robot whose “command” is swapped.
  • When “mirroring” (flipping), output Robot <id> modifies a line of command of Robot <id2>.. Here <id2> is an integer, the index of the robot whose “command” is mirrored.
  • When “replacing”, output Robot <id> replaces a line of command of Robot <id2>.. Here <id2> is an integer, the index of the robot whose “command” is replaced.
  • When “executing” an ACTIVATE command to “activate” (different from your round-by-round “activation”), output Robot <id> activates Robot <id2>.. Here <id2> is an integer, the index of the robot being “activated”.
  • TRIGGER commands are not “executed”, so no output is needed for them; however, when they are “triggered”, you still need to output the information of the corresponding “basic command” being “executed” in the formats above.
2 2 5
0 0
MOVE 1 1
MOVE 0 1
0 1
TRIGGER MOVE: MOVE 0 1
SLACKOFF

Robot 0 moves its right hand towards Robot 1.
Robot 1 moves its left hand towards Robot 1.
Robot 0 moves its left hand towards Robot 1.
Robot 1 moves its left hand towards Robot 0.
Robot 1 slacks off.

2 2 4
0 1
ACTIVATE 1
SLACKOFF
0 1
SWAP 0 2 2
MIRROR 0 1

Robot 0 activates Robot 1.
Robot 1 swaps a line of command with Robot 0.
Robot 1 slacks off.
Robot 0 modifies a line of command of Robot 0.

3 2 6
1 2
ACTIVATE 0
ACTIVATE 0
2 1
SWAP 0 2 2
TRIGGER ACTIVATE: REPLACE 0 2 SLACKOFF
0 1
TRIGGER MIRROR: SLACKOFF
SLACKOFF

Robot 0 activates Robot 1.
Robot 1 swaps a line of command with Robot 2.
Robot 1 slacks off.
Robot 2 replaces a line of command of Robot 0.
Robot 0 slacks off.
Robot 1 swaps a line of command with Robot 2.

3 2 8
0 1
SLACKOFF
TRIGGER MOVE: SLACKOFF
1 2
TRIGGER TRIGGER: SLACKOFF
TRIGGER SLACKOFF: MOVE 0 1
2 0
TRIGGER SLACKOFF: MOVE 1 2
TRIGGER TRIGGER: MOVE 1 1

Robot 0 slacks off.
Robot 1 moves its left hand towards Robot 2.
Robot 2 moves its right hand towards Robot 1.
Robot 1 slacks off.
Robot 2 moves its right hand towards Robot 0.
Robot 0 slacks off.
Robot 1 slacks off.
Robot 2 moves its right hand towards Robot 2.

见附加文件的 5.in。
见附加文件的 5.ans。

Hint

Explanation of Sample #1

The “trigger” timing of TRIGGER commands is after the “execution” finishes. Note that it cannot “trigger” its own TRIGGER commands.

Explanation of Sample #2

Note that when executing earlier “commands”, later “commands” may be changed; in that case, the changed “commands” should be executed.

Explanation of Sample #3

When an ACTIVATE command “activates” another robot, only after all of that robot’s “commands” have been “executed” is this “command” considered finished.

Explanation of Sample #4

Only its earliest TRIGGER command that satisfies the conditions will be “triggered”.

Explanation of Sample #5

Selfless gifts? Powerful help?

Subtasks

It is guaranteed that the format of all commands is correct.

It is guaranteed that the input file length does not exceed 5MB5\mathtt{MB}.

It is guaranteed that at least kk “commands” can be “executed”.

Constraints: 2n1002 \le n \le 100, 1m101 \le m \le 10, 1k3×1051 \le k \le 3 \times 10^5.

It is guaranteed that 0li,ri<n0 \le l_i, r_i < n.

It is guaranteed that 0h10 \le h \le 1, 1x,ym1 \le x, y \le m, 1z<n1 \le z < n. All input numbers are integers.

Problem Usage Agreement

From the THUPC2024 (Tsinghua University Programming Contest and Collegiate Invitational Contest 2024) Preliminary Round.

The following “this repository” refers to the official THUPC2024 Preliminary Round repository (https://github.com/ckw20/thupc2024_pre_public).

  1. Any organization or individual may use or repost the problems in this repository for free.

  2. Any organization or individual, when using the problems in this repository, should do so free of charge and publicly. It is strictly forbidden to profit from these problems or to add special access restrictions to them.

  3. If possible, when using the problems in this repository, please also provide ways to obtain resources such as testdata, reference solutions, and editorials; otherwise, please include the GitHub address of this repository.

Translated by ChatGPT 5