P26144 [AtCoder] [ARC030B] Tree Graph
普及+
题目描述

Problem Description

There is a tree graph consisting of $ n $ vertices. This graph has exactly $ n-1 $ edges and is guaranteed to be connected. A graph with such properties does not contain any cycles. Each vertex is represented by a distinct integer from $ 1 $ to $ n $, and the cost of each edge is $ 1 $.

Some of the vertices in this graph may contain at most one gem. You can only collect a gem when you are at a vertex that contains a gem. You want to start at a certain vertex $ x $ and find the shortest path that allows you to collect all the gems and return to vertex $ x $. Determine the length of such a path. The length of a path is defined as the sum of the costs of the edges included in the path.

Input Format

The input is given from the standard input in the following format:

$ n $ $ x $ $ h_1 $ $ h_2 $ … $ h_n $ $ a_1 $ $ b_1 $ $ a_2 $ $ b_2 $ : $ a_{n-1} $ $ b_{n-1} $

  • The first line contains an integer $n\ (1\ ≦\ n\ ≦\ 100)$ representing the number of vertices in the tree graph and an integer $x\ (1≦\ x\ ≦n)$ representing the starting vertex number.
  • The second line contains $ n $ integers $h_i\ (0≦h_i≦1)$ separated by spaces, where $ h_i $ represents the number of gems at vertex $ i $.
  • From the third line to the $ n-1 $ line, the vertex numbers $ a_i $ and $b_i\ (1≦a_i,b_i≦n)$ connected by the $ i $ th edge of the tree graph are given.
  • The given graph does not have self-loops or multiple edges and is guaranteed to be connected.

Output Format

Output the length of the shortest path in one line. Do not forget to add a newline.

Sample Input and Output

Sample Input #1

5 1
1 0 1 0 1
1 2
2 3
2 4
1 5

Sample Output #1

6

Sample Input #2

3 2
0 1 0
1 2
2 3

Sample Output #2

0

Notes/Hints

Sample Explanation 1

The input graph and one example of the shortest path are shown in the figure below. Since $ x=1 $, the journey starts at vertex $ 1 $. ![](/img/arc/030/Bsample1.png)

Sample Explanation 2

Since the only gem is at the starting point, the shortest path is to stay put.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 1 1 0 1 0 1 1 2 2 3 2 4 1 5
样例输出
6