Coloring trees
Practice
0 (0 votes)
Graphs
Algorithms
Graph representation
Problem
100% Success 3 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

There are \(N\) cities that are connected by \(N-1\) roads. \(K\) cities have bus terminals and other \(N-K\) cities have bus stops. A bus terminus is a designated place where a bus starts or ends its scheduled route.

A bus should start from a terminal and must end its journey at another terminal visiting any city at most once. A city is crowded if there is a bus service in the city where one or more than one bus visits it along any route. You are required to simulate the routes for the buses so that the maximum number of cities is crowded. You can assume there is a number of buses ready for service from each terminus.

Note:

  • A city with a bus terminal is always crowded.
  • It is guaranteed that it is possible to reach from one city to another city.

Input format

  • First line: Two space-separated integers \(N\) and \(K\)
  • Next \(N-1\) lines: Two integers \(u\) and \(v\) that denote city \(u\) and city \(v\) is connected by a road
  • Next line: \(K\) integers denoting the cities that have a bus terminal

Output format

Print the number of cities that are crowded after all simulations. 

Constraints

\(1 \leq N \leq 200000\)

\(1 \leq K, u, v \leq N\)

It is guaranteed that it is possible to reach from one city to another city.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
48 votes
Tags:
ApprovedBasic ProgrammingEasyImplementationOpen
Points:30
544 votes
Tags:
MathematicsApprovedEasy-Medium
Points:20
16 votes
Tags:
Easy