QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 256 MB 總分: 100

#12017. Colorful Tree

统计

You are given a tree with colored edges.

A path is defined as valid if the colors of the edges on the path appear only on that specific path.

You are given $m$ independent queries. Each query asks for the maximum length of a valid path after removing a specific vertex.

A path is still valid if it was valid in the original tree and the removed vertex is not on the path.

Input

The first line contains two integers $n$ and $m$.

The next $n-1$ lines each contain three integers $x, y, col$, representing an edge between $x$ and $y$ with color $col$.

The next $m$ lines each contain an integer $x$, representing a query.

Output

Output $m$ lines, each containing an integer representing the answer to the corresponding query.

Constraints

$m \le n \le 10^5, 1 \le x, y, col \le n$

Subtask 1 (9 points): $n \le 100$.

Subtask 2 (11 points): $n \le 1000$.

Subtask 3 (17 points): The tree structure is guaranteed to be a chain.

Subtask 4 (23 points): $m = 1$.

Subtask 5 (40 points): No additional constraints.

Examples

Input 1

4 4
1 2 2
1 3 1
1 4 1
1
2
3
4

Output 1

0
2
1
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.