#P7215. [JOISC 2020] 首都
[JOISC 2020] 首都
Description
JOI Country has towns, numbered from to . These towns are connected by bidirectional roads.
JOI Country also has cities, numbered from to . Town belongs to city .
Now the Prime Minister of JOI Country, Mr. JOI the th, wants to choose one city as the capital. The city chosen as the capital must satisfy the following condition: from any town in the capital to any other town in the capital, it is possible to travel using only towns in the capital. However, there may be no city that satisfies this condition.
Therefore, Mr. JOI the th will perform city merges. Merging city and city will move all towns in city into city .
Find the minimum number of merges needed so that a capital can be chosen.
Input Format
The first line contains two integers , representing the number of towns and the number of cities.
The next lines each contain two integers , representing a bidirectional edge.
The next lines contain one integer each; the -th of these lines contains , meaning that town belongs to city .
Output Format
Output one integer in a single line, representing the minimum number of merges.
6 3
2 1
3 5
6 2
3 4
2 3
1
3
1
2
3
2
1
8 4
4 1
1 3
3 6
6 7
7 2
2 5
5 8
2
4
3
1
1
2
3
4
1
12 4
7 9
1 3
4 6
2 4
10 12
1 2
2 10
11 1
2 8
5 3
6 7
3
1
1
2
4
3
3
2
2
3
4
4
2
Hint
Explanation for Sample 1
You can merge cities and , and then choose city as the capital.
Subtasks
| Subtask | Special Property | Score |
|---|---|---|
| Each town is connected to at most two towns | ||
| None |
Constraints
For of the testdata, . It is guaranteed that starting from any town you can reach all other towns. Also, .
Notes
Translated from The 19th Japanese Olympiad in Informatics Spring Training Camp Day4 A Capital.
Translated by ChatGPT 5
京公网安备 11011102002149号