Description
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lif Estyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare", followed by a line containing a single I Nteger K, followed by K "Distance Queries". Each distance query was a line of input containing and integers, giving the numbers of the of the farms between which FJ is Intere Sted in computing distance (measured in the length of the roads along the path between the farms). Please answer FJ' as As
Input
1.. 1 as " Navigation Nightmare " 2 1 Ten,3+m. 2
Output
1
Sample Input
7 61 6 -E6 3 9E3 5 7S4 1 3N2 4 -W4 7 2S31 61 42 6
Sample Output
- 3 $
Hint
2 6 +3+ +=
Source
Usaco 2004 February
Test instructions: to a tree with weights, a total of k queries, each query tree in the distance of 2 nodes. Node n Max is 40000,k max 10000
Analysis: First we convert the tree without root to a root tree, and we can get the distance from each node to the root node in O (n) time. Since the path from one node to another in the tree is unique, the path from a to B must pass through the LCA (A, a, a, a, a, a, a) =c. It is not difficult at this time to find D (A, b) =d (a,root) +d (b,root) -2*d (c,root).
Here is a method to find the LCA, time 1000+ms, feeling a little slow
1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 200000 at #defineINF 1e12 -vector<pair<int,int> >Edge[n]; -vector<pair<int,int> >Que[n]; - intn,m,q; - intAns[n]; - intDis[n]; in intFa[n]; - intVis[n]; to intsum; + intFindintx) { - returnfa[x]==x?x:fa[x]=find (Fa[x]); the } * voidLCA (intUintp) { $fa[u]=u;Panax Notoginseng for(intI=0; I<edge[u].size (); i++){ - intv=Edge[u][i].first; the if(v==p)Continue; +dis[v]=dis[u]+Edge[u][i].second; A LCA (v,u); thefa[v]=u; + } -vis[u]=1; $ if(SUM==Q)return; $ for(intI=0; I<que[u].size (); i++){ - intv=Que[u][i].first; - if(Vis[v]) { theans[que[u][i].second]=dis[u]+dis[v]-2*Dis[find (v)]; - }Wuyi } the } - intMain () Wu { - while(SCANF ("%d%d", &n,&m) = =2){ About $ for(intI=0; i<n;i++){ - edge[i].clear (); - que[i].clear (); - } Asum=0; +memset (Vis,0,sizeof(Vis)); the - Chars[3]; $ for(intI=0; i<m;i++){ the inta,b,c; thescanf"%d%d%d%s",&a,&b,&c,s); the Edge[a].push_back (Make_pair (b,c)); the Edge[b].push_back (Make_pair (a,c)); - } in thescanf"%d",&q); the for(intI=0; i<q;i++){ About intx, y; thescanf"%d%d",&x,&y); the Que[x].push_back (Make_pair (y,i)); the Que[y].push_back (Make_pair (x,i)); +ans[i]=0; - } thedis[1]=0;BayiLCA (1,0); the the for(intI=0; i<q;i++){ -printf"%d\n", Ans[i]); - } the } the return 0; the}
View Code
POJ 1986 Distance Queries (LCA)