B train

Time limit: 10000 MS    Space limitations: 565536 KB
Benchmark description: 1.5s, 512m
Problem Description

A state-owned n cities, there are a number of connected two-way road between the city and there is a unique path between the city twenty-two. Now there is a train in the city a, m need to go through cities.

Trains run according to the following rules: Each time travel has not been to the city in the m cities most forward. A small now want to know the number of road trains after this city m elapsed.

Input Format

The first row of three integers n, m, a, the number of cities represents the number of cities, the need to go through, the location of the train begins.

Next, n-1 lines of two integers x and y, expressed a bidirectional road between x and y.

The next line of integers m, expressed the need to go through the city.

Output Format

Line an integer representing the number of road trains passing.

Sample input 1

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

Sample output 1

9

Sample input 2

100 30 19
1 2
2 3
2 4
2 5
3 6
6 7
6 8
7 9
8 10
8 11
11 12
12 13
12 14
14 15
15 16
16 17
16 18
17 19
17 20
19 21
19 22
22 23
23 24
24 25
25 26
24 27
26 28
27 29
27 30
29 31
29 32
31 33
33 34
34 35
35 36
34 37
37 38
36 39
39 40
38 41
39 42
42 43
41 44
44 45
44 46
46 47
45 48
47 49
47 50
48 51
51 52
50 53
53 54
54 55
53 56
54 57
57 58
57 59
58 60
59 61
60 62
62 63
63 64
62 65
64 66
66 67
66 68
66 69
69 70
68 71
71 72
70 73
72 74
74 75
73 76
76 77
75 78
77 79
78 80
78 81
81 82
80 83
83 84
84 85
85 86
86 87
87 88
86 89
89 90
90 91
90 92
91 93
92 94
93 95
94 96
94 97
95 98
97 99
99 100
80 86 36 64 67 45 7 54 87 26 73 68 42 41 57 96 77 8 74 58 52 99 60 22 51 6 93 72 30 15

Sample output 2

401

prompt

N<=500000 ,M<=400000

Sample Description 1:
1. City 4: 2 from 4 arrive, the number of road 2, 3, 4 through the cities
2. Urban 3: 3 has been elapsed, 3 skipped
3. City 1: 1 from 4 reaches , number 3 road, through the cities of 4,3,2,1
4. city 5: 5 from 1 to reach, road number 4, after the city has 1,2,3,4,5

Guess you like

Origin www.cnblogs.com/Limbo-To-Heaven/p/11372887.html