A USACO Themed Problem by Eric Yang
Estimated Difficulty: USACO Silver / Codeforces 1600
Bessie the cow is lost in Farmer John's farm, which can be represented as a series of $N$ barns $(1\leq N\leq4\cdot10^{5})$ numbered $1...N$ connected by $N-1$ bidirectional pathways. Bessie starts at room $K$ $(1\leq K\leq N)$ and wants to get to Farmer John's home, located at barn $1$. Each minute, Bessie can either do nothing or move to an adjacent room.
Scattered around the farm are $M$ bulls $(0\leq M\leq N)$, located in different barns. Each minute, these bulls always move to an adjacent barn that leads them closer to the Farmer John's house. One minute after reaching Farmer John's house, the bulls walk behind the barn to sleep.
However, Bessie has a fear of bulls, meaning that if Bessie and one or multiple bulls are present in a barn, including Farmer John's house, at the same minute, then Bessie will faint of shock. Note that bulls functionally disappear one minute after reaching Farmer John's house. If Bessie chooses to do nothing during a turn, she can hide in the corner, causing bulls to have no effect on her.
Determine the minimum number of minutes (the first move starts at minute $1$) for Bessie to reach Farmer John's house without fainting.
The first line contains $T$, the number of testcases.
In each testcase:
The first line contains integers $N$, $M$, and $K$.
The next $N-1$ lines contain barns $u$ and $v$, indicating that there is a path between barns $u$ and $v$.
The next $M$ lines contain barn $x$, a room with a bull.
Print the answer for all $T$ testcases. For each testcase, print "TESTCASE" (without the quotes) on the first line. On the next line, print the minimum minutes elapsed until Bessie reaches Farmer John's house (the first move can occur at minute $1$).
27 4 41 22 32 44 54 64 723676 2 31 21 33 43 54 6
26
TESTCASE4TESTCASE2
For the first test case, Bessie can hide in the corner at times $1$ and $2$. Starting at time $3$, she can finally move all the way to Farmer John's house without interruptions, making her final move at $4$ minutes.