#P3629. [APIO2010] 巡逻

    ID: 1807 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2010APIO深度优先搜索,DFS树的直径

[APIO2010] 巡逻

Description

In a region, there are nn villages, numbered 1,2,,n1, 2, \dots, n. There are n1n-1 roads connecting these villages. Each road connects exactly two villages, and from any village you can reach any other village via these roads. Each road has length 11 unit. To ensure safety, a patrol car must patrol all roads every day. The police station is located in village 11. Every day the patrol car always starts from the police station and finally returns to the police station. The figure below shows a region with 88 villages, where villages are represented by circles (village 11 is the black circle) and roads are the line segments connecting the circles. To traverse all the roads, the patrol car needs to travel a distance of 1414 units, and each road must be traversed twice.

To reduce the total patrol distance, the region plans to build KK new roads between the villages, and each new road can connect any two villages. Two new roads may meet at or end at the same village, as in figure (c) below. A new road can even be a loop, i.e., both ends connect to the same village. Due to limited budget, KK can only be 11 or 22. Meanwhile, to avoid wasting funds, every day the patrol car must traverse each newly built road exactly once. The following are some examples:

In (a), one new road is built, and the total distance is 1111. In (b), two new roads are built, and the total patrol distance is 1010. In (c), two new roads are built, but since the patrol car must traverse each new road exactly once, the total distance becomes 1515. Write a program that reads the information about the roads between villages and the number of roads to be built, computes the best plan for adding new roads to minimize the total patrol distance, and outputs this minimum patrol distance.

Input Format

The first line contains two integers n,K(1K2)n, K(1 ≤ K ≤ 2). Then follow n1n-1 lines, each with two integers a,ba,b, indicating that there is a road between villages aa and bb (1a,bn)(1 ≤ a, b ≤ n).

Output Format

Output a single integer, the minimum total patrol distance after adding KK new roads.

8 1 
1 2 
3 1 
3 4 
5 3 
7 5 
8 5 
5 6 
11
8 2 
1 2 
3 1 
3 4 
5 3 
7 5 
8 5 
5 6 
10
5 2 
1 2 
2 3 
3 4 
4 5 
6

Hint

  • In 10%10\% of the testdata, 1n1000,K=11≤n≤1000,K=1.
  • In 30%30\% of the testdata, K=1K=1.
  • In 80%80\% of the testdata, the number of adjacent villages for each village does not exceed 2525.
  • In 90%90\% of the testdata, the number of adjacent villages for each village does not exceed 150150.
  • In 100%100\% of the testdata, 3n105,1K23≤n≤10^5,1≤K≤2.

Translated by ChatGPT 5