Ant Hill
Statement
Alice the Ant likes to make Ant Hills, long winding tunnels throughout the ground, so that her and all of her family have places to rest. Alice's family has a terrible sense of direction, so all of the Ant Hills she designs contain no loops (In other words, the only way to get back to a particular point in the hill is to retrace your steps).
Because of her digging ability, we'd like Alice to build a particular Ant Hill for us, but since she is an Ant, we need to specify the building directions in a very simple manner.
Alice only follows two commands:
- Tunnel: Alice will dig a new tunnel from her current position for centimeters
- Back: Alice will move backwards centimeters from her current position (always towards the start of the ant hill)
So following the following commands:
TUNNEL 10
BACK 1
TUNNEL 2
BACK 4
TUNNEL 3
BACK 6
TUNNEL 2
She'll have carved out the following tunnel:
We don't mind the direction of the tunnels, which is why we are just specifying forwards/backwards. Can you provide the instructions to Alice so she can dig the ant hill to our liking?
Input
Input will begin with a single integer , the number of nodes in the ant hill. Nodes are just the end points of lines.
lines will follow, each containing 3 space separated integers, , and . This means that there is a line connecting nodes and of length .
In the example above, input might have been:
8
1 2 4
2 3 2
2 4 3
4 5 3
4 6 2
6 7 2
6 8 1
Where the bottom most node is node 1, and so the second line of input is connecting that to node 2 with distance 4. The next two lines of input then connect node 2 to two other nodes with distance 2 and 3 respectively, in pink and purple in the diagram above.
Output
Output should start with an integer , the number of instructions. lines should follow, each containing either TUNNEL x
or BACK x
for some integer x
. If these instructions carve out the ant hill specified, then your program will receive AC
.
Note
Note that your instructions must be assumed to start at Node 1 of input. But that still allows for multiple instruction sets to work. For example, in the previous drawing, the following instructions would also be appropriate:
9
TUNNEL 2
TUNNEL 4
BACK 2
TUNNEL 6
BACK 1
TUNNEL 2
BACK 4
TUNNEL 3
BACK 6
Constraints
- Your output instructions should have
- Input will not contain a node which is touched by exactly 2 edges (A redundant stopping point in the hill)
Comments