#P16115. [USTCPC 2026] Climbing Tree
[USTCPC 2026] Climbing Tree
Background
Today, Kruskal-chan is again staring at the whiteboard in the club room, thinking hard.
“Hmm... walking around on a tree, and in the end returning to the starting point... does such a tree really exist?”
A junior curiously leans over. “Senpai is studying another weird problem again?”
“It’s not weird at all!” Kruskal-chan puffs out her cheeks. “This is the key problem that decides whether I can return to where I started!”
She stares at the action sequence in her hand, lightly tapping the desk with her fingers.
“If we can find such a tree, it must be very interesting~”
Problem Description
In a rooted tree , the nodes are labeled with distinct positive integers. Kruskal-chan initially stays at some node of . She can perform the following four actions:
- Move to the parent node, denoted by
p. - Move to any child node, denoted by
c. - Move to any sibling node with a smaller label, denoted by
l. - Move to any sibling node with a larger label, denoted by
r.
Given an action sequence, determine whether there exists a rooted tree such that, by appropriately choosing the initial node and the target node of each action, Kruskal-chan can return to the initial node exactly after performing all actions.
Note: You should ensure that your construction is valid at every step. For example, if the current node is the root, then you cannot perform action p.
Input Format
This problem contains multiple test cases.
The first line contains an integer (), representing the number of test cases.
Then lines follow. Each line contains a non-empty string consisting only of the characters pclr, representing the action sequence.
It is guaranteed that the total length of all action sequences does not exceed .
Output Format
Output lines, each representing the result for one test case. If there exists a rooted tree that satisfies the requirement, output Yes, otherwise output No.
4
l
lr
ppc
cppc
No
Yes
No
Yes
Hint
For the first and the third test cases, it can be proven that it is impossible to return to the initial node in the end.
The figure below shows a tree that satisfies the second test case. The root node is labeled , and the initial node is labeled . The action process can be or .

The figure below shows a tree that satisfies the fourth test case. The root node is labeled , and the initial node is labeled . The action process is .

Translated by ChatGPT 5