QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#18394. Special Vertex

Estadísticas

There is an undirected tree consisting of $N$ vertices, labeled from $1$ to $N$.

The tree contains special vertices. We want to add edges to the tree to create a path that visits every special vertex exactly once. In this path, no vertex may be visited more than once. Non-special vertices (regular vertices) may be included in the path, but it is not necessary to visit all regular vertices.

Find the minimum number of edges that must be added to create a path that visits every special vertex exactly once.

Input

The first line contains the number of vertices $N$. $(1 \leq N \leq 200\,000)$

The next $N-1$ lines each contain two integers $u$ and $v$, representing an edge between vertex $u$ and vertex $v$. $(1 \leq u, v \leq N)$

The $(N+1)$-th line contains $N$ integers separated by spaces. If the $i$-th integer is $0$, vertex $i$ is a regular vertex; if it is $1$, vertex $i$ is a special vertex.

The graph provided in the input is a tree.

Output

Output the minimum number of edges required to create the path.

Examples

Input 1

21
1 2
1 3
2 4
2 5
3 6
3 7
3 8
3 9
4 10
5 11
5 12
6 13
6 14
10 15
10 16
13 17
16 18
16 19
17 20
17 21
0 0 0 0 0 1 1 1 1 0 1 0 0 1 1 0 0 1 1 1 0

Output 1

4

Input 2

4
1 2
2 3
3 4
0 0 0 0

Output 2

0

Input 3

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

Output 3

1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.