[Rmq + LCA] poj 1986

Source: Internet
Author: User

The data of OJ May be faulty. It seems that the connectivity and disconnectivity are always possible. To be conservative, use the query set to determine whether the connection is successful. Add the virtual root to form a tree and use the offline rmq.

# Define n 40010 struct edge {int V; int Len; int next;} e [2 * n]; int ecnt; int head [N]; bool vis [N]; int N; // point int R [N]; // the first occurrence of the I point subscript int P [N * 2]; // record path point number int dis [N]; // distance from the root int Dep [2 * n]; // depth int DP [20] [2 * n]; // The closest common ancestor subscript of the 2 ^ I path starting from J. Here !!! The second dimension must be 2 times the size !! Int num; int Fa [N]; void Init () {memset (Head,-1, sizeof (head); memset (R,-1, sizeof (R )); memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; I ++) Fa [I] = I; ecnt = 0 ;} void add (int u, int V, int Len) {e [ecnt]. V = V; E [ecnt]. len = Len; E [ecnt]. next = head [u]; head [u] = ecnt ++; E [ecnt]. V = u; E [ecnt]. len = Len; E [ecnt]. next = head [v]; head [v] = ecnt ++;} int find (int x) {If (Fa [x]! = X) Fa [x] = find (Fa [x]); Return Fa [X];} void uni (int u, int V) {int A = find (U); int B = find (V); if (a = B) return; Fa [a] = B;} void DFS (INT U, int depth) {vis [u] = 1; p [++ num] = u; Dep [num] = depth; For (INT I = head [u]; I! =-1; I = E [I]. Next) {int v = E [I]. V; If (! Vis [v]) {dis [v] = dis [u] + E [I]. len; DFS (v, depth + 1); P [++ num] = u; Dep [num] = depth ;}} void init_rmq () {int I, J; for (I = 1; I <= num; I ++) {If (R [p [I] =-1) {R [p [I] = I ;}} for (I = 1; I <= num; I ++) {DP [0] [I] = I ;} int t = (INT) (log (Num * 1.0)/log (2.0); for (I = 1; I <= T; I ++) {for (j = 1; j + (1 <(I-1) <= num; j ++) {int A = DP [I-1] [J], B = DP [I-1] [J + (1 <(I-1)]; If (DEP [a] <= Dep [B]) {DP [I] [J] = A;} else DP [I] [J] = B ;}}} Int rmq (int u, int v) {// returns the number of the nearest common ancestor, if (R [u]> = R [v]) Swap (u, v ); int S = R [u], t = R [v]; int K = (INT) (log (t-s + 1) * 1.0) /log (2.0); int A = DP [k] [s], B = DP [k] [T-(1 <k) + 1]; if (DEP [a] <= Dep [B]) return P [a]; else return P [B];} int main () {int M, C; while (scanf ("% d", & N, & M )! =-1) {int I, j; Init (); int x, y, z; char STR [3]; while (M --) {scanf ("% d % s", & X, & Y, & Z, STR); add (x, y, z); UNI (X, y);} for (I = 1; I <= N; I ++) {If (Fa [I] = I) {Add (I, 0, 0 ); // Add virtual root 0 to change forest to tree} num = 0; DIS [0] = Dep [0] = 0; DFS (); init_rmq (); scanf ("% d", & C); While (c --) {scanf ("% d", & X, & Y); int F = rmq (X, y); If (! F) puts ("0"); else printf ("% d \ n", DIS [x] + dis [y]-2 * Dis [f]);} return 0 ;}

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.