1336A.md (2836B)
1 Source: https://codeforces.com/problemset/problem/1336/A 2 3 A. Linova and Kingdom 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it. 22 23  24 25 There are $n$ cities and $n-1$ two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from $1$ to $n$, and the city $1$ is the capital of the kingdom. So, the kingdom has a tree structure. 26 27 As the queen, Linova plans to choose exactly $k$ cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city. 28 29 A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique). 30 31 Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path. 32 33 In order to be a queen loved by people, Linova wants to choose $k$ cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her? 34 35 Input 36 37 The first line contains two integers $n$ and $k$ ($2\le n\le 2 \cdot 10^5$, $1\le k \lt n$) — the number of cities and industry cities respectively. 38 39 Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1\le u,v\le n$), denoting there is a road connecting city $u$ and city $v$. 40 41 It is guaranteed that from any city, you can reach any other city by the roads. 42 43 Output 44 45 Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys. 46 47 Examples 48 49 Input 50 51 ``` 52 7 4 53 1 2 54 1 3 55 1 4 56 3 5 57 3 6 58 4 7 59 ``` 60 61 Output 62 63 ``` 64 7 65 ``` 66 67 Input 68 69 ``` 70 4 1 71 1 2 72 1 3 73 2 4 74 ``` 75 76 Output 77 78 ``` 79 2 80 ``` 81 82 Input 83 84 ``` 85 8 5 86 7 5 87 1 7 88 6 1 89 3 7 90 8 3 91 2 1 92 4 5 93 ``` 94 95 Output 96 97 ``` 98 9 99 ``` 100 101 Note 102 103  104 105 In the first example, Linova can choose cities $2$, $5$, $6$, $7$ to develop industry, then the happiness of the envoy from city $2$ is $1$, the happiness of envoys from cities $5$, $6$, $7$ is $2$. The sum of happinesses is $7$, and it can be proved to be the maximum one. 106 107  108 109 In the second example, choosing cities $3$, $4$ developing industry can reach a sum of $3$, but remember that Linova plans to choose exactly $k$ cities developing industry, then the maximum sum is $2$.