Poj 1986 distance queries

Source: Internet
Author: User

Http://poj.org/problem? Id = 1986

The distance between two points in a tree. (Do not consider disconnections)

Question: LCA template question.

1 # include <iostream> 2 # include <algorithm> 3 # include <cstring> 4 # include <string> 5 # include <cstdio> 6 # include <cmath> 7 # include <queue> 8 # include <map> 9 # include <set> 10 11 # define EPS 1e-5 12 # define maxn 55555 13 # define maxm 111111 14 # define INF 1000000000 15 using namespace STD; 16 int n, m, Q; 17 struct edge 18 {19 int V, next, W; 20} es [maxm]; 21 int head [maxn], E; 22 int IDE, TMP Dfn; 23 int f [2 * maxn], Id [maxn], used [maxn], POS [maxn], DIS [maxn]; 24 int MI [2 * maxn] [18]; 25 26 void Init () 27 {28 memset (Head,-1, sizeof (head); 29 E = 0; 30 ide = tmpdfn = 0; 31 memset (used, 0, sizeof (used); 32 dis [1] = 0; 33} 34 35 void add (int u, int V, int W) 36 {37 es [e]. V = V; 38 es [e]. W = W; 39 es [e]. next = head [u]; 40 head [u] = e ++; 41} 42 void DFS (int u) 43 {44 used [u] = 1; 45 Int TMP = ++ tmpdfn; 46 f [++ Ide] = TMP; 47 ID [TMP] = u; 48 POS [u] = ide; 49 for (INT I = head [u]; I! =-1; I = es [I]. Next) 50 {51 int v = es [I]. V; 52 If (! Used [v]) 53 {54 dis [v] = dis [u] + ES [I]. w; 55 DFS (V); 56 F [++ Ide] = TMP; 57} 58} 59 60} 61 void rmqinit (int n, int * W) 62 {63 for (INT I = 1; I <= N; I ++) MI [I] [0] = W [I]; 64 int M = (INT) (log (N * 1.0)/log (2.0); 65 for (INT I = 1; I <= m; I ++) 66 for (Int J = 1; j <= N; j ++) 67 {68 MI [J] [I] = mi [J] [I-1]; 69 If (J + (1 <(I-1) <= N) MI [J] [I] = min (MI [J] [I], mi [J + (1 <(I-1)] [I-1]); 70} 71} 72 73 int rmqmin (INT L, int R) 74 {75 int M = (INT) (log (R-l + 1) * 1.0)/log (2.0 )); 76 return min (MI [l] [m], MI [R-(1 <m) + 1] [m]); 77} 78 79 int LCA (INT l, int R) 80 {81 If (Pos [l]> POS [R]) Swap (L, R); 82 int ans = rmqmin (Pos [L], pos [R]); 83 return ID [ANS]; 84} 85 int main () 86 {87 // freopen ("/users/Apple/desktop/question/poj 1986_1/poj 1986_1/in", "r", stdin ); 88 scanf ("% d", & N, & M); 89 int U, V, W, L, R; 90 Init (); 91 while (M --) 92 {93 scanf ("% d % * s", & U, & V, & W); 94 add (U, V, W ); 95 Add (v, U, W); 96} 97 DFS (1); 98 rmqinit (IDE, f); 99 scanf ("% d", & Q ); 100 while (Q --) 101 {102 scanf ("% d", & L, & R); 103 printf ("% d \ n ", dis [l] + dis [R]-2 * Dis [LCA (L, R)]); 104} 105 return 0; 106}

 

Poj 1986 distance queries

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.