Efficient Inspection
InterviewTime limit1sMemory limit1024 MB
On a tree, find the shortest closed walk from node 0 that visits every required node, and print its length and the route taken.
Problem
The Joseon court is about to launch a large-scale inspection for corruption. The problem is that the officials must travel across the whole country in person, which is exhausting and takes a lot of time. The king wants to run the inspection efficiently with help from you, who come from the future.
Each official was given a map and a table listing the places that official must visit. Every place on the map has a unique number from to , and the palace is number . Each place is connected to others by roads of the same length that can be traveled in both directions. All officials start at the palace (0), visit every designated place, and return to the palace to report to the king. When moving from one place to another, they must follow the given roads and cannot change direction partway along a road.
The maps given to the officials are designed as follows.
- All places are connected to each other by exactly roads. Each road connects only its two end places.
- There is exactly one path between any two places.
- Traveling along one road takes the same time, one unit of time .
Given the map and table for one official, find one optimal route that starts at the palace (0), visits all designated places, and returns to the palace in the minimum possible time.
Input
The first line contains the number of places and the number of places that the official must visit, separated by a space. (; )
The second line contains distinct place numbers that the official must visit, separated by spaces. ()
The next lines give, on line , the two end places and of the -th road, separated by a space. (; )
Output
On the first line, print the minimum time (in units of time) needed for the trip.
On the second line, print the place numbers visited along one optimal route, in order, separated by spaces.