ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

Please, give some hints how to solve this problem
Posted by Stratovarius 13 Jul 2006 11:19
Can someone tell me how to solve this problem:

There are N(N<=50000) cities. And there are N-1 roads between them. It is always possible to get from one city to another (so it is a tree). Then N-1 roads are described with three integers 1<=A[i],B[i]<=N and 1<=time[i]<=1000 meaning that a road between cities A[i] and B[i] can be travelled in time[i] hours. Then there are M (1<=M<=50000) queries to answer. Each query contains a pair of numbers 1<=X,Y<=N. It is necessary to find the time needed to pass from city X to city Y. Output should contain M lines - answers to the queries.

Time Limit: 4 sec.
Memory Limit: 32 MB.

PS. Sorry for my poor English.
Re: Please, give some hints how to solve this problem
Posted by N.M.Hieu ( DHSP ) 13 Jul 2006 13:21
Use LCA algorithm. ( Least Common Ancestor )
O( NlogN + M )
You can search this algo in the internet.
Re: Please, give some hints how to solve this problem
Posted by Stratovarius 14 Jul 2006 19:20
Thanks!
 I'll try to find this algo in internet.
 Can you give a link, where this algo is explained in russian?
Re: Please, give some hints how to solve this problem
Posted by N.M.Hieu ( DHSP ) 18 Jul 2006 21:47
Sorry , I can't help you.
I only know this in English.
Re: Please, give some hints how to solve this problem
Posted by nistaman 27 Jan 2007 22:08
wrong