Polish version    English version  
  About Olympic -> Problems


 News
 About Olympic
About contest
Problems
I OI 1993/1994
II OI 1994/1995
III OI 1995/1996
IV OI 1996/1997
V OI 1997/1998
VI OI 1998/1999
VII OI 1999/2000
VIII OI 2000/2001
IX OI 2001/2002
X OI 2002/2003
XI OI 2003/2004
XII OI 2004/2005
XIII OI 2005/2006
XIV OI 2006/2007
XV OI 2007/2008
Problems archive
 History of OI
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
IX Olimpiada Informatyczna 2001/2002

Task: kom
Author: Tomasz Waleń
Byteasar the Travelling Salesman

I stage contest  

A salesman Byteasar works hard travelling over Byteotia. In past times travelling salesmen could choose on their own towns they wanted to visit and order to do so, but those times are now gone forever. From the time the Central Controlling Office for Travelling Salesmen was established every salesman gets from the Office a list of towns to visit and the order of his tour. As it usually happens with central offices, the imposed order of visiting towns has not much in common with an optimal order. Before leaving for his tour Byteasar would like to know at least how much time it will take him to visit all the towns. It is your task to compute this.

The towns of Byteotia are numbered from 1 to n. The capital of Byteotia has the number 1, and this is the place Byteasar starts on his journey from. The towns are connected by a network of two-way roads. A trip between two towns directly connected by a road always takes 1 unit of time. From the capital one can reach any other Byteotian town. However, the road network had been designed very thriftily, so the roads never form cycles.

Task

Write a program which:
  • reads from the text file kom.in the description of the road network in Byteotia and the list of towns that Byteasar has to visit,
  • computes the total time of Byteasar's trip,
  • writes the result to the output text file kom.out.

Input

In the first line of the text file kom.in there is one integer n equal to the number of towns in Byteotia, 1<=n<=30 000. In the following n-1 lines the network of roads is described. In each of these lines there are two integers a and b (1<=a, b<=n; a<>b), meaning that the towns a and b are connected by a road. In the line n+1 there is one integer m equal to the number of towns Byteasar should visit, 1<=m<=5 000. I the following m lines there are numbers of successive towns on Byteasar's route - one number per line.

Output

In the first and only line of the text file kom.out there should be one integer equal to the total time of Byteasar's trip.

Example

For the following input file kom.in:
5
1 2
1 5
3 5
4 5
4
1
3
2
5
the correct answer is in the following output file kom.out:
7



Print friendly version