P9966 [THUPC 2024 Preliminary] Robots
Description
There are $n$ robots standing in a circle, numbered $0 \sim n-1$ in counterclockwise order.
Each robot has two hands. Initially, Robot $i$ has its “left hand” pointing to Robot $l_i$, and its “right hand” pointing to Robot $r_i$.
Inside every robot, there are $m$ 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 $h$-th hand counterclockwise by $z$ robots. When $h=0$ it means the “left hand”, and when $h=1$ it means the “right hand”, same below.
- `SWAP h x y`: Swap the $x$-th line of “command” of the robot pointed to by the $h$-th hand with its own $y$-th line of “command”.
- `MIRROR h x`: “Mirror” (flip) the $x$-th line of “command” of the robot pointed to by the $h$-th hand, i.e. flip $h$ in that “command” ($0$ becomes $1$, $1$ becomes $0$). In particular, it has no effect on the `SLACKOFF` command; and for the `TRIGGER` command, it directly modifies $h$ inside the “command” that will be “executed” when “triggered”.
- `REPLACE h x `: Replace the $x$-th line of “command” of the robot pointed to by the $h$-th hand with ``, where `` is a complete “command”.
### Advanced Commands
- `ACTIVATE h`: **“activate”** the robot pointed to by the $h$-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 : `: Here `` is the name of a “command”, i.e. the first all-caps word in a “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 `` is “executed” once:
- If `` is not `TRIGGER`, then the “command” that has just finished “executing” is a `` command;
- If `` 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 $0$ and “activate” these robots in increasing order, round and round, and output information about the first $k$ executed commands.
Input Format
The first line contains three positive integers $n, m, k$.
Then, the information of the $n$ robots is given in increasing order of their indices.
For each robot, the first line contains two non-negative integers $l_i, r_i$, indicating the robot index pointed to by its “left hand” and by its “right hand”.
The next $m$ lines give the robot’s “commands” in order. The format of “commands” is as described in the statement.
Output Format
Output $k$ lines, describing the relevant information of the first $k$ commands that begin “executing”, in order. Output before each command starts “executing”, one per line, in the following format:
- When “slacking off”, output `Robot slacks off.`. Here `` is an integer, the index of the robot “executing” the current “command”, same below.
- When “moving”, output `Robot moves its hand towards Robot .`. Here `` is `left` or `right`, indicating which hand is moved (`left` for “left hand”, `right` for “right hand”); `` is an integer, the index of the robot that this hand points to after moving.
- When “swapping”, output `Robot swaps a line of command with Robot .`. Here `` is an integer, the index of the robot whose “command” is swapped.
- When “mirroring” (flipping), output `Robot modifies a line of command of Robot .`. Here `` is an integer, the index of the robot whose “command” is mirrored.
- When “replacing”, output `Robot replaces a line of command of Robot .`. Here `` 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 activates Robot .`. Here `` 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.
Explanation/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 $5\mathtt{MB}$.
It is guaranteed that at least $k$ “commands” can be “executed”.
Constraints: $2 \le n \le 100$, $1 \le m \le 10$, $1 \le k \le 3 \times 10^5$.
It is guaranteed that $0 \le l_i, r_i < n$.
It is guaranteed that $0 \le h \le 1$, $1 \le x, y \le m$, $1 \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](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