No.994 Scattered Coins
タグ : / 解いたユーザー数 354
作問者 :
ganmodokix
/ テスター :
monkukui2
Problem Statement
Consider an undirected tree with $N$ vertices. The vertices of this tree are numbered $1,2,\cdots,N$.
For the $N-1$ edges, the endpoints of the $i$-th edge are vertex $a_i$ and vertex $b_i$.
Initially, $K$ coins are placed on vertex $1$.
From this state, you may perform the following operation any number of times: "choose one vertex with at least 1 coin on it, choose any number (at least 1) of the coins on that vertex, and move them to an adjacent vertex." The goal is to reach a state in which every vertex has at most 1 coin on it.
If it is possible to reach this state, find the minimum number of operations required.
Input
$N\ K$
$a_1$ $b_1$
$a_2$ $b_2$
$\vdots$
$a_{N-1}$ $b_{N-1}$
Constraints
- All inputs are integers.
- $2\le N\le 10^5$
- $1\le K\le 10^5$
- $1\le a_i,b_i \le N$
- If $i\ne j$, then $(a_i,b_i)\ne(a_j,b_j)$
- The graph given by the input is a tree.
Output
If the required state can be reached, output the minimum number of operations; otherwise, output '-1'. Print a newline at the end.
Sample
Sample 1
Input
4 2 1 2 2 3 3 4
Output
1
Of the $2$ coins placed on vertex $1$, moving $1$ coin to vertex $2$ achieves the required state in the minimum of $1$ operation.
Sample 2
Input
5 6 1 2 1 3 1 4 2 5
Output
-1
No matter how the operations are performed, some vertex will end up with $2$ or more coins on it.
Sample 3
Input
5 3 1 2 2 3 2 4 1 5
Output
2
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。