Problem Description
Ms. Zhang is a journalist from Hong Kong, and she runs very very fast, so every one calls her “The fastest runner”. There are n cities (numbered from 1 to n) and n roads in the country, the roads are undirected. It costs 1 unit of time to go through each road. There exists at least one path between any pair of cities. Now Ms. Zhang can start her interview in city S, visit all of the n cities and finish her interview in city T. Since Ms. Zhang’s interview is simple and naive, the interview does not cost any time. Can you help Ms. Zhang to find the optimal S and T that minimise the total traveling time of Ms. Zhang? If there are multiple optimal solutions, output the solution with the smallest S. If there are still multiple solutions, output the solution with the smallest T.
Input
The first line contains only one integer T (
T≤40), which indicates the number of test cases.
Each test case contains n + 1 lines.
The first line contains one integer
1≤n≤105.
In the next n lines, each line contains two integers x and y indicating there is a road between city x and city y.
It is guarenteed that there does not exist a road connecting one city to itself or two roads connecting the same pair of cities.
Output
For each test case, output one line “Case #x: t S T ”, where x is the case number(starting from 1),t is the minimal total traveling time, S and T are the index of starting and ending city.
Sample Input
1 10 7 1 5 3 4 5 8 9 6 4 8 6 2 7 10 3 4 1 9 7
Sample Output
题意:给定一个环套树的图,让找一个起点和终点,使得从起点开始每个点访问一次到终点的最小路程最少,问最小路程是多少。
分析:找到环后分两种情况,起点终点在一棵子树中,那么答案为2*n-circle-dis(s,t),或者起点终点在不同子树中,那么答案为2*n-dis(U,V)-2-dis(U,u)-dis(V,v),前者树形DP时就能统计出,后者需要对环单独扫两遍求解。
#include
#include
#include
#include
#include
#include
#include