report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Oggy and the cockroaches - MarisaOJ: Marisa Online Judge

Oggy and the cockroaches

Time limit: 1000 ms
Memory limit: 256 MB
The residential area of Oggy's neighborhood is represented as a tree graph consisting of $n$ houses and $n-1$ roads connecting them. Over $q$ days, Oggy has to chase cockroaches to maintain order in his neighborhood. On the $i$-th day among the $q$ days, Oggy will start at house $u_i$, and the cockroaches are located at house $v_i$. In each unit of time, both Oggy and the cockroaches can move to an adjacent house or stay at their current house (crossing one road takes one unit of time). For each day, determine the maximum number of time units it will take for Oggy to catch the cockroaches, knowing that they both move optimally. ### Input - The first line contains two positive integers $n$ and $q$. - The next $n-1$ lines, each containing two positive integers $u_i, v_i$, describe a road. - The following $q$ lines, each containing two positive integers $u_i, v_i$, indicate the starting location of Oggy and the cockroaches. ### Output Print $q$ lines, each containing the answer to the corresponding query. ### Constraints - $1 \le n,q \le 10^5$ ### Example Input ``` 6 4 1 2 2 3 2 4 1 5 5 6 3 5 1 3 2 6 5 2 ``` Output ``` 4 2 3 3 ```
Lowest common ancestor (LCA)
LCA
Distance query
Robot on tree
Heaviest edge
Equal path
Triplet
Path update
Second best minimum spanning tree
Oggy and the cockroaches
Topic
Tree
Greedy
Rating 1800
Solution (0) Solution