POJ1986 Distance Queries "Recent public ancestor" "Tarjan-lca algorithm"

Source: Internet
Author: User

Distance QueriesTime limit:2000msMemory limit:30000kTotal submissions:9777accepted:3425Case Time limit:1000ms

Description


Farmer John ' s cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. 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 ' s distance queries as quickly as possible!


Input
* Lines 1..1+m:same format as "Navigation Nightmare"

* Line 2+m:a single integer, K. 1 <= k <= 10,000

* Lines 3+m. 2+m+k:each line corresponds to a distance query and contains the indices of both farms.


Output

* Lines 1..k:for Each distance query, output on a single line an integer giving the appropriate distance.


Sample Input
7 6
1 6 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 W
4 7 2 S
3
1 6
1 4

2 6


Sample Output
13
3

36


Hint

Farms 2 and 6 are 20+3+13=36 apart.


Source

Usaco 2004 February


John is a farmer and his cow is lazy and refuses to follow John's path. John had to find a way

The shortest way. The first half of the question is the same as the POJ1984 "Navigation nightmare". In each group of data

After that is an integer k, and the next K-line is to ask (U,v) the Manhattan distance (U,v is the farm number). Finally output all

Ask the result.

POJ1984 Link: http://poj.org/problem?id=1984

Idea: the input of the subject is somewhat special and gives a point of how far it is in a certain direction (east). Because of the number of inputs

It is very special. All of them have a forward edge, and the structure is not a loop, so it must be a tree. So it can be applied directly according to

The template for the TARJAN-LCA algorithm, taking into account the shortest path calculation for each pair of nodes on the tree. If the root is determined to be 1, there is

Dist (u,v) = Dist (1,u) + Dist (1,v)-2*dist (1,lca (u,v)). A deep search of a traverse to find out each

The distance from the node to the root, and then another LCA, you can get the results.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 80080;const int MAXQ = 20020;int father[maxn],head[maxn],qhead[maxn],dist[maxn];struct EdgeNode{i    NT to;    int next; int LCA;} EDGES[MAXN];    Edgenode qedges[maxn];int Find (int x) {if (x! = Father[x]) father[x] = find (father[x]); return father[x];}    BOOL Vis[maxn];void LCA (int u) {father[u] = u;    Vis[u] = true;  for (int k = Head[u]; K! =-1; k = edges[k].next) {if (!vis[edges[k].to]) {dist[edges[k].to] =            Dist[u] + Edges[k].lca;            LCA (edges[k].to);        Father[edges[k].to] = u; }} for (int k = Qhead[u]; K! =-1; k = qedges[k].next) {if (vis[qedges[k].to]) {//qedge            S[k].lca = Find (qedges[k].to);            Qedges[k].lca = Dist[u] + dist[qedges[k].to]-2*dist[find (qedges[k].to)];        Qedges[k^1].lca = Qedges[k].lca; }}}int Main () {intN,m,k,u,v,w,a,b;    char s;        while (~SCANF ("%d%d", &n,&m)) {memset (father,0,sizeof (father));        memset (head,-1,sizeof (Head));        memset (qhead,-1,sizeof (Qhead));        memset (vis,false,sizeof (VIS));        memset (edges,0,sizeof (Edges));        memset (qedges,0,sizeof (qedges));        memset (dist,0,sizeof (Dist));        int id = 0;            for (int i = 0; i < M; ++i) {scanf ("%d%d%d%c", &u,&v,&w,&s);            Edges[id].to = v;            Edges[id].lca = W;            Edges[id].next = Head[u];            Head[u] = id++;            edges[id].to = u;            Edges[id].lca = W;            Edges[id].next = Head[v];        HEAD[V] = id++;        } scanf ("%d", &k);        int iq = 0;            for (int i = 0; i < K; ++i) {scanf ("%d%d", &a,&b);            Qedges[iq].to = b;            Qedges[iq].next = Qhead[a];            Qhead[a] = iq++;            Qedges[iq].to = A; QEDGES[IQ].Next = Qhead[b];        QHEAD[B] = iq++;        } LCA (1);    for (int i = 0; i < IQ; i+=2) printf ("%d\n", QEDGES[I].LCA); } return 0;}



POJ1986 Distance Queries "Recent public ancestor" "Tarjan-lca algorithm"

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.