[BZOJ1602] [Usaco2008 OCT] Ranch walking (LCA)

Source: Internet
Author: User

Description

N Cattle (2<=n<=1000) Others are labeled 1 to N, grazing on n blocks of land that are also labeled 1 to N, and the first cattle graze on the block I ranch. This n block of land is connected by a n-1 strip. The cow can walk on the edge, the section I is connected to the Ai,bi ranch, and the length of the first edge is Li (1<=li<=10000). These sides are arranged so that any two cows can reach through these sides, so this is a tree. These cows are very sociable and often visit each other, and they want you to help them calculate the distance between the 1<=q<=1000 and the cows.

Input

* First line: Two integers separated by a space: N and Q

* Line two to Nth: line i+1 has two integers separated by spaces: Ai,bi,li

* Line n+1 to n+q: Each line has two spaces separated by an integer: P1,p2, which indicates the number of two cows.

Output

* Line 1th to line Q: Output one number per line, indicating the distance between the two cows.

Sample Input4 2
2 1 2
4 3 2
1 4 3
1 2
3 2
Sample Output2
7
HINTSource

Qualifying Tournament

Solution

Naked LCA, people who can't read the code are    

1#include <bits/stdc++.h>2 using namespacestd;3 structEdge4 {5     intV, W, NXT;6}e[2005];7 structQuery8 {9     intu, V, NXT;Ten}q[2005]; One intefst[1005], qfst[1005], fa[1005], lca[1005], dis[1005]; A BOOLvis[1005]; -   - voidAddedge (intIintUintVintW) the { -E[i] = (edge) {V, W, Efst[u]}, efst[u] =i; - } -   + voidAddQuery (intIintUintv) - { +Q[i] = (query) {u, V, Qfst[u]}, qfst[u] =i; A } at   - intGet_dis (inti) - { -     returnDis[q[i <<1].U] + dis[q[i <<1].V]-2*Dis[lca[i]]; - } -   in intGETFA (intx) - { to     returnFA[X] = x = = Fa[x]?X:GETFA (fa[x]); + } -   the voidTarjan (intu) * { $Fa[u] = u, vis[u] =true;Panax Notoginseng      for(inti = Efst[u]; I i =e[i].nxt) -         if(!VIS[E[I].V]) the         { +DIS[E[I].V] = Dis[u] +E[I].W; A Tarjan (E[I].V); theFA[E[I].V] =u; +         } -      for(inti = Qfst[u]; I i =q[i].nxt) $     { $         intv = q[i].u = = u?q[i].v:q[i].u; -         if(Vis[v]) lca[i >>1] =GETFA (Fa[v]); -     } the } -  Wuyi intMain () the { -     intN, q, U, V, W; WuCIN >> N >>Q; -      for(inti =1; I < n; i++) About     { $cin >> u >> v >>W; -Addedge (i <<1, U, V, W); -Addedge (i <<1|1, V, u, W); -     } A      for(inti =1; I <= Q; i++) +     { theCIN >> U >>v; -AddQuery (i <<1, u, v); $AddQuery (i <<1|1, V, u); the     } theTarjan (1); the      for(inti =1; I <= Q; i++) thecout << Get_dis (i) <<Endl; -     return 0; in}
View Code

[BZOJ1602] [Usaco2008 OCT] Ranch walking (LCA)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.