Tourism
Time limit2sMemory limit512 MB
Given a connected graph with city weights and a start city, find the maximum total weight of cities a walk from the start can visit without immediately reusing the road just taken.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Tree, Dynamic programming
- Solved
- No attempts yet
Problem
Da Vinci has a wide range of interests, including botany. He once planned a trip around Florence to study plant domestication and ecosystems. Florence has cities, and city has plant species. Each of the bidirectional roads connects a pair of cities, and it is possible to travel between every pair of cities using these roads.
Da Vinci starts at city . He can move along one road at a time to an adjacent city. However, if he travels to city via the road , he cannot immediately return to city on that same road. (Going back and forth on the same road is boring.)
Da Vinci wants to see as many plant species as possible, that is, maximize . He may visit a city more than once, but each city's species are counted only once. He does not care which city the trip ends in. What is the maximum number of plant species he can see during the tour?
Input
The first line contains the number of data sets (), followed by the data sets.
The first line of each data set contains two integers and (), separated by a space. The second line contains integers (). Each of the next lines contains two integers and (), describing the -th road that connects cities and . The next line contains a single integer (), the city where Da Vinci starts.
Any pair of cities can be reached from each other using these roads. No two roads connect the same pair of cities, and no road connects a city to itself.
Output
For each data set, print a line "Data Set x:" where is the data set number. On the next line, print the maximum number of plant species Da Vinci can see. Print a blank line after each data set.