PivotOJ

Branch Manager

시간 제한: 4000ms메모리 제한: 1024MB출처: ICPC Rocky Mountain Regional 2022-2023BOJ 27568

문제

You are managing a transportation network of one-way roads between cities. People travel through the transportation network one by one in order all starting from the same city, and each person waits for the person before them to stop moving before starting. The people follow a simple algorithm until they reach their destination: they will look at all the outgoing roads from the current city, and choose the one that leads to the city with the smallest label. A person will stop when they either reach their destination, or reach a city with no outgoing roads. If at any point someone fails to reach their destination, the rest of the people still waiting in line will leave.

Before each person enters the transportation network, you can permanently close down any subset of roads to guarantee they reach their destination. The roads that you choose to close down will not be available for future people.

There are nn cities, labeled from 11 to nn. There are n1n-1 directed roads, and each road will always be from a lower labeled city to a higher labeled one. The network will form a rooted tree with city 11 as the root. There are mm people that want to travel through the network. Each person starts from city 11, and has a specific destination city dd in mind. These people will line up in the given order. What is the maximum number of people you can route correctly to their destination if you close roads optimally?

입력

The first line of input contains two integers nn and mm (2n,m2×1052 \le n,m \le 2 \times 10^5), where nn is the number of cities and mm is the number of people.

Each of the next n1n-1 lines contains two integers aa and bb (1a<bn1 \le a < b \le n), denoting a directed road from city aa to bb. These roads will describe a rooted tree with city 11 as the root.

Each of the next mm lines contains a single integer dd (2dn2 \le d \le n), denoting the destination city of the next person in line.

출력

Output a single integer, which is the maximum number of people you can route to the correct destination.

예제

예제 1

입력
8 5
1 2
4 8
4 6
1 4
2 5
4 7
2 3
5
2
6
4
8
출력
5

예제 2

입력
4 4
1 2
1 3
1 4
3
2
3
4
출력
1
코드를 제출하려면 로그인하세요.