LCA of POJ 1330 Nearest Common Ancestors multiplication algorithm

Source: Internet
Author: User

POJ 1330 Nearest Common Ancestors

Test instructions: Nude topic of the recent public ancestor

Idea: LCA and St We are already familiar with, but here f[i][j] have similar but different meanings. F[I][J] Represents the number of 2j fathers of the I node

This code is not mine, transfer from Bingbin Blog

1 /* ***********************************************2 Author:kuangbin3 Created time:2013-9-5 9:45:174 File NAME:F:\2013ACM exercises \ Thematic Learning \lca\poj1330_3.cpp5 ************************************************ */6 7#include <stdio.h>8#include <string.h>9#include <iostream>Ten#include <algorithm> One#include <vector> A#include <queue> -#include <Set> -#include <map> the#include <string> -#include <math.h> -#include <stdlib.h> -#include <time.h> + using namespacestd; - /* + * POJ 1330 A * LCA Online algorithm at */ - Const intMAXN =10010; - Const intDEG = -; -  - structEdge - { in     intto, next; -}EDGE[MAXN *2]; to intHEAD[MAXN], tot; + voidAddedge (intUintv) - { theEdge[tot].to =v; *Edge[tot].next =Head[u]; $Head[u] = tot++;Panax Notoginseng } - voidInit () the { +tot =0; AMemset (Head,-1,sizeof(head)); the } + intFA[MAXN][DEG];//Fa[i][j] Represents the first 2^j ancestor of node I - intDEG[MAXN];//Depth Array $  $ voidBFS (introot) - { -queue<int>que; theDeg[root] =0; -fa[root][0] =Root;Wuyi Que.push (root); the      while(!que.empty ()) -     { Wu         intTMP =Que.front (); - Que.pop (); About          for(inti =1; i < DEG; i++) $Fa[tmp][i] = fa[fa[tmp][i-1]][i-1]; -          for(inti = head[tmp]; I! =-1; i =edge[i].next) -         { -             intv =edge[i].to; A             if(v = = fa[tmp][0])Continue; +DEG[V] = deg[tmp] +1; thefa[v][0] =tmp; - Que.push (v); $         } the  the     } the } the intLCA (intUintv) - { in     if(Deg[u] >Deg[v]) Swap (U, v); the     intHu = Deg[u], HV =Deg[v]; the     intTu = u, TV =v; About      for(intDet = Hv-hu, i =0; Det Det >>=1, i++) the     if(Det &1) theTV =Fa[tv][i]; the     if(TU = TV)returntu; +      for(inti = DEG-1; I >=0; i--) -     { the         if(Fa[tu][i] = =Fa[tv][i])Bayi             Continue; theTu =Fa[tu][i]; theTV =Fa[tv][i]; -     } -     returnfa[tu][0]; the } the BOOLFLAG[MAXN]; the intMain () the { -Freopen ("In.txt","R", stdin); the     //freopen ("OUT.txt", "w", stdout); the     intT; the     intN;94     intu, v; thescanf"%d", &T); the      while(t--) the     {98scanf"%d", &n); About init (); -memset (Flag,false,sizeof(flag));101          for(inti =1; I < n; i++)102         {103scanf"%d%d", &u, &v);104 Addedge (U, v); the Addedge (V, u);106FLAG[V] =true;107         }108         intRoot;109          for(inti =1; I <= N; i++) the         if(!Flag[i])111         { theRoot =i;113              Break; the         } the BFS (root); thescanf"%d%d", &u, &v);117printf"%d\n", LCA (U, v));118     }119     return 0; -}

LCA of POJ 1330 Nearest Common Ancestors multiplication 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.