POJ 1470 Closest Common Ancestors LCA

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1470

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (U,V) The program determines the closest common ancestor of U and V in the tree. The closest common ancestor of the nodes U and V are the node w that's an ancestor of both U and V and have the greatest de PTH in the tree. A node can be it own ancestor (for example in Figure 1 the ancestors of Node 2 is 2 and 5)

Title Description: Give the information between the nodes of a tree, and then give some inquiries, each asking is to find out two nodes of the LCA. Each node is counted as the number of times the LCA is queried and output.

Algorithm analysis: LCA off-line algorithm, from the algorithm thinking and thinking is not difficult, can be said to be a template problem, but the input of the problem is a bit troublesome, processing input can be.

The other ZOJ1141 is this problem, I am on the zoj above AC

But on the POJ WA, and finally processed the input after AC.

Look at the program think, should poj the above shape such as 5: (5) Such input may not be a colon and parentheses, but other symbols bar.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <vector>8 #defineINF 0x7fffffff9 using namespacestd;Ten Const intmaxn= the+Ten; One Const intmax_log_maxn=Ten; A  - intN,root; -vector<int>G[MAXN]; the intFATHER[MAX_LOG_MAXN][MAXN],D[MAXN]; - intVIS[MAXN]; -  - voidDfsintUintPintdepth) + { -father[0][u]=p; +d[u]=depth; A     intnum=g[u].size (); at      for(intI=0; i<num; i++.) -     { -         intv=G[u][i]; -         if(v! = P) Dfs (v,u,depth+1); -     } - } in  - voidInit () to { +DFS (root,-1,0); -      for(intk=0; k +1&LT;MAX_LOG_MAXN; k++) the     { *          for(intI=1; i<=n; i++.) $         {Panax Notoginseng             if(father[k][i]<0) father[k+1][i]=-1; -             Elsefather[k+1][i]=Father[k][father[k][i]]; the         } +father[k][root]=Root; A     } the } +  - intLCA (intUintv) $ { $     if(d[u]>D[v]) swap (U,V); -      for(intk=0; k<max_log_maxn; k++.) -     { the         if((D[v]-d[u]) >>k &1) -v=Father[k][v];Wuyi     } the     if(U==V)returnu; -      for(intk=max_log_maxn-1; k>=0; k--) Wu     { -         if(Father[k][u]! =Father[k][v]) About         { $u=Father[k][u]; -v=Father[k][v]; -         } -     } A     returnfather[0][u]; + } the  - intMain () $ { the      while(SCANF ("%d", &n)! =EOF) the     { the         intu,num,v; the          for(intI=0; i<=n; i++.) g[i].clear (); -memset (Vis,0,sizeof(Vis)); in         Charstr[ -]; thememset (str,0,sizeof(str)); the         Charch[2],ch2[2],ch3[2]; About          for(intI=0; i<n; i++.) the         { the             intu=0, num=0; thescanf"%d%1s%1s%d%1s",&u,ch,ch2,&Num,ch3); + //scanf ("%s", str); - //int u=0,num=0; the //int Len=strlen (str);Bayi //int j=0; the //For (j=0; J<len && str[j]!= ': '; j + +) u=u*10+str[j]-' 0 '; the //For (j=j+2; J<len && str[j]!= ') '; j + +) num=num*10+str[j]-' 0 '; -              while(num--) -             { thescanf"%d",&v); the G[u].push_back (v); thevis[v]=1; the             } -         } the          for(intI=1; i<=n; i++)if(!vis[i]) {root=i; Break; } the init (); thememset (Vis,0,sizeof(Vis));94scanf"%d",&num); thememset (str,0,sizeof(str)); the         //GetChar (); the          for(intj=0; j<num; j + +)98         { Aboutscanf"%1s%d%d%1s",ch,&u,&v,ch2); - //gets (str);101 //int u=0,v=0;102 //int Len=strlen (str);103 //int i=0;104 //For (i=1; i<len && str[i]>= ' 0 ' && str[i]<= ' 9 '; i++) the //u=u*10+str[i]-' 0 ';106 //While (I<len)107 //            {108 //if (str[i]>= ' 0 ' && str[i]<= ' 9 ') break;109 //i + +; the //            }111 //While (I<len) the //            {113 //if (str[i]== ') ') break; the //v=v*10+str[i]-' 0 '; the //i + +; the //            }117             intans=LCA (u,v);118vis[ans]++;119         } -          for(intI=1; i<=n; i++.)121             if(Vis[i]) printf ("%d:%d\n", I,vis[i]);122     }123     return 0;124}

POJ 1470 Closest Common Ancestors LCA

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.