ZOJ 3195 Design the city LCA to RMQ

Source: Internet
Author: User

Given n vertices, n-1-1 rows of u, v, and dis indicate an undirected edge and edge weight. Here a undirected tree is given.

The following m indicates m queries. Ask u v n, which is the shortest distance.

Typical conversion from LCA to RMQ

 

 

# Include <stdio. h> # include <string. h> # include <math. h ># define N 100000 # define INF 1 <29 # define Logo 17 using namespace std; inline int Min (int a, int B) {return a> B? B: a;} struct node {int f, to, dis, nex;} edge [N]; int edgenum, head [N], dis [N]; int E [N * 2], R [N], D [N * 2], en; // LCAint ST [N * 2] [Logo]; void addedge (int u, int v, int dis) {edge [edgenum]. f = u; edge [edgenum]. to = v; edge [edgenum]. dis = dis; edge [edgenum]. nex = head [u]; head [u] = edgenum ++;} void makeRmqIndex (int n, int B []) // returns the subscript {int I, j; for (I = 0; I <n; I ++) ST [I] [0] = I; for (j = 1; (1 <j) <= n; j ++) for (I = 0; I + (1 <j)-1 <n; I ++) ST [I] [j] = B [ST [I] [J-1] <B [ST [I + (1 <(J-1)] [J-1]? ST [I] [J-1]: ST [I + (1 <(J-1)] [J-1];} int LCA (int s, int v, int B []) // returns the subscript in D of the minimum value (same as the subscript in E) {s = R [s], v = R [v]; int k; if (s> v) {k = s; s = v; v = k;} k = (int) (log (v-s + 1) * 1.0) /log (2.0); return B [ST [s] [k] <B [ST [v-(1 <k) + 1] [k]? E [ST [s] [k]: E [ST [v-(1 <k) + 1] [k];} void DFS (int x, int deep) {E [en] = x; D [en] = deep; R [x] = en ++; for (int I = head [x]; I! =-1; I = edge [I]. nex) {int v = edge [I]. to; if (R [v] =-1) {dis [v] = dis [x] + edge [I]. dis; DFS (v, deep + 1); E [en] = x; D [en ++] = deep ;}} void Input (int n) {memset (head,-1, sizeof (head); edgenum = 0; while (-- n) {int u, v, dis; scanf ("% d", & u, & v, & dis); addedge (u, v, dis); addedge (v, u, dis );} memset (R,-1, sizeof (R); en = 0; dis [0] = 0;} int main () {int n, I, que, first = 0; while (~ Scanf ("% d", & n) {if (first ++) printf ("\ n"); Input (n); DFS (0, 0); makeRmqIndex (en, d); scanf ("% d", & que); while (que --) {int a, B, c; scanf ("% d ", & a, & B, & c); int ans = dis [a] + dis [B] + dis [c]-(dis [LCA (a, c, D)] + dis [LCA (B, c, D)] + dis [LCA (a, B, D)]); printf ("% d \ n", ans );}} return 0 ;}

 

Related Article

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.