# SPOJ 913 Qtree Series-Query on a tree II (multiply LCA)

Source: Internet
Author: User

Title Address: Qtree2-query on a tree II
LCA learned off-line and on-line RMQ method to do this problem, so think for a long time also did not think how to do. See the puzzle is used to multiply LCA. So again to learn the next multiplication method to seek LCA, this only to find the multiplication method is simply water problem ... Because the K-point of the path can be converted into the first K-parent, however, the principle of multiplication is based on the parent node, so the problem is easy to solve.
Ask for a good distance. The key is to seek the K-point of the path, it is clear that this point is either the K-parent node of u, or the K-parent node of V, so that the first time LCA, the judge is U or V, and then the multiplication method to find the point.
The code is as follows:

``#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL Long Long#define PI ACOs ( -1.0)Const intMod=1e7+3;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=10000+Ten;intDEP[MAXN], DIS[MAXN], dp[maxn][ -];intHEAD[MAXN], CNT, n;structnode{intU, V, W, next;} edge[maxn<<1];voidAddintUintVintW) {edge[cnt].v=v;        Edge[cnt].w=w;        Edge[cnt].next=head[u]; head[u]=cnt++;}voidDfsintUintFA) { for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(V==FA)Continue; dep[v]=dep[u]+1;                DIS[V]=DIS[U]+EDGE[I].W;                DFS (V,U); dp[v][0]=u; }}structbz{intI, J;voidInit () { for(j=1;(1&LT;&LT;J) <=n;j++) { for(i=1; i<=n;i++) {if(dp[i][j-1]!=-1) dp[i][j]=dp[dp[i][j-1]][j-1]; }                }        }intQuery (intUintV) {if(Dep[u]<dep[v]) swap (U,V); for(i=0;(1<<i) <=dep[u];i++); i--; for(j=i;j>=0; j--) {if(Dep[u]-(1&LT;&LT;J) >=dep[v]) u=dp[u][j]; }if(U==V)returnU for(j=i;j>=0; j--) {if(Dp[u][j]!=dp[v][j])                                {U=DP[U][J];                        V=DP[V][J]; }                }returndp[u][0]; }intKthintUintVintK) {intRoot=query (U,v), ans;if(dep[u]-dep[root]+1>=k) {ans=dep[u]-k+1; for(i=0;(1<<i) <=dep[u];i++); i--; for(j=i;j>=0; j--) {if(Dep[u]-(1&LT;&LT;J) >=ans) u=dp[u][j]; }returnU }Else{ans=dep[root]+k-(dep[u]-dep[root]+1); for(i=0;(1<<i) <=dep[v];i++); i--; for(j=i;j>=0; j--) {if(Dep[v]-(1&LT;&LT;J) >=ans) v=dp[v][j]; }returnV }}}BZ;voidInit () {memset(head,-1,sizeof(head));memset(DEP,0,sizeof(DEP));memset(Dis,0,sizeof(dis));memset(dp,-1,sizeof(DP)); Cnt=0;}intMain () {intT, I, U, V, W, K;Chars[Ten];scanf("%d", &t); while(t--) {scanf("%d", &n); Init (); for(i=1; i<n;i++) {scanf("%d%d%d", &u,&v,&w);                        Add (U,V,W);                Add (V,U,W); } DFS (1,-1); Bz.init (); while(scanf('%s ', s)!=eof&&s[1]!=' O '){if(s[1]==' I '){scanf("%d%d", &u,&v);printf("%d\n", dis[u]+dis[v]-2*dis[bz.                        Query (U,V)]); }Else{scanf("%d%d%d", &u,&v,&k);printf("%d\n", bz.kth (u,v,k)); }                }        }return 0;}``

SPOJ 913 Qtree Series-Query on a tree II (multiply LCA)

Related Keywords:

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.