Turning Off the Tree Lights
Time limit2sMemory limit1024 MB
Bob walks on a tree, flipping the bulb at each vertex he leaves, and must turn all bulbs off in at most 4N moves starting from vertex a.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Implementation, Greedy
- Solved
- No attempts yet
Problem
vertices form a tree. A tree is a connected graph whose edges have no direction, and the shortest path between any two vertices is unique. Each vertex has a bulb and a switch. A bulb is either on or off. Pressing the switch of a vertex flips the state of that vertex's bulb. A bulb that is on turns off, and a bulb that is off turns on.
Bob is a sloth living on this tree. Bob starts at vertex . He wants to make all bulbs off by repeating the following procedure zero or more times.
- Suppose Bob is at vertex . He chooses a vertex adjacent to , presses the switch at , and moves to .
Help Bob find a way to make all bulbs off using the procedure at most times. The problem conditions guarantee that such a way always exists.
Input
The first line contains the number of vertices and the vertex where Bob starts, separated by a space. (; )
The second line contains a string of length that describes the bulbs. If the bulb at vertex is on, the -th character is \tt{'1'}. Otherwise, the -th character is \tt{'0'}.
From the third line onward, each of the lines contains two vertices and that are connected by an edge, separated by a space. ()
The given graph is guaranteed to be a valid tree.
Output
On the first line, print the number of procedures to carry out, where .
On the second line, print integers , separated by spaces, that give the vertex chosen in each procedure. Vertex must be adjacent to , and for every with , vertex must be adjacent to .
If there are several ways to make all bulbs off, print any one of them. The number of procedures does not need to be minimal.
Hint
The steps that Bob follows in the example, and the resulting bulb states, are as follows.
- Bob moves to , and the bulb at turns off.
- Bob moves to , and the bulb at turns off.
- Bob moves to , and the bulb at turns on.
- Bob moves to , and the bulb at turns off.
- Bob moves to , and the bulb at turns off.
- Bob moves to , and the bulb at turns on.
- Bob moves to , and the bulb at turns on.
- Bob moves to , and the bulb at turns off.
- Bob moves to , and the bulb at turns off.
- Bob moves to , and the bulb at turns on.
- Bob moves to , and the bulb at turns off.
- Bob moves to , and the bulb at turns off.
A total of 12 procedures are carried out, and all bulbs are off when the procedure ends.