A warehouse contains rooms connected by corridors that form a tree. Each corridor has a length. Room contains one bar of soap described by : hand soap (H) or face soap (F).
You start in room . Collect every bar of soap exactly once and finish in room . You may visit rooms and traverse corridors any number of times; visiting a room does not force you to collect its soap.
The required types of collected soap alternate as hand, face, hand, face, and so on. On a hand turn, you may collect either type of soap. On a face turn, you may collect only face soap.
Find the minimum possible total length of all traversed corridors. If it is impossible to collect all bars under these rules, output .
H and F.Collect the soap in room on a hand turn and the soap in room on a face turn, then return. The corridor is traversed twice, for a total distance of .
Collect the soaps in rooms , in that order. Pass through room on the way to room without collecting its soap yet. The total distance is .
Collect the soaps in rooms , in that order, then return to room . The total distance is .
Collect the soaps in rooms , in that order, then return to room . The face soap in room fills a hand turn. The total distance is .