Guardian of the Forest
Time limit1sMemory limit1024 MB
Given a tree where fire starts at vertex a and Geunseong stands at vertex b, find the maximum number of vertices where he can copy data before the fire reaches him.
- Level
Medium6 of 10
- Topics
- Tree, DFS, Dynamic programming
- Solved
- No attempts yet
Problem
Geunseong is very interested in trees.
Geunseong is a druid and the guardian of the forest who takes care of a tree. The tree he manages has the form of a tree graph with vertices. Geunseong values every vertex, so he assigned each of the vertices a unique number from to .
One day, the villain Yeongdo, who has always envied Geunseong's wonderful tree, set it on fire. To preserve the tree as well as possible, Geunseong must copy as much of the data stored at each vertex as he can before the tree burns away completely.
Initially, Yeongdo set fire to vertex , and Geunseong stands on vertex , which has not burned. On an unburned vertex, Geunseong may choose and perform one of the following actions.
- Copy: copy the data at the current vertex. This takes no time. However, data cannot be copied again at a vertex where it has already been copied.
- Move: move along an edge to one of the neighboring unburned vertices.
- Wait: stay at the current vertex without moving.
If Geunseong moves or waits, fire spreads during that time from each burning vertex to its unburned neighbors.
If Geunseong's next action puts him on a burning vertex, he immediately gives up preserving the tree and escapes. If fire spreads to a vertex at the same moment Geunseong arrives there, he cannot copy data at that vertex.
Your task is to find the maximum number of vertices at which Geunseong can copy data before he escapes, and report it to him.
Input
The first line contains the number of vertices of the tree.
Each of the next lines contains two integers and , separated by a space, meaning there is an edge between vertex and vertex .
The line after those lines contains the numbers of the vertex that is initially on fire and the vertex where Geunseong stands, separated by a space.
Output
Print the maximum number of vertices at which Geunseong can copy data before he escapes, over all possible cases.
Constraints
- All numbers in the input are integers.
- Every tree in the input is a valid tree.
Hint
The figure below shows the tree from Sample 1. In this problem, a tree is a simple connected graph with no cycles. The figure of the sample is also a simple connected graph with no cycles.

In this sample, the maximum number of vertices where Geunseong can copy data is 3. One optimal plan is to move ()→→ without waiting, copying the data at every vertex.