SPOJ 913 QTREE系列- Query on a tree II (倍增LCA)

來源:互聯網
上載者:User

標籤:acm   演算法   編程   lca   

題目地址:QTREE2 - Query on a tree II
LCA學了離線與線上轉RMQ方法後就去做這道題,於是想了好長時間也沒想到怎麼做。看了題解都是用的倍增LCA。。於是又去學了下倍增法求LCA,這才發現用倍增法做簡直是水題。。。因為求路徑的第k個點可以轉化成求第k個父節點,然而倍增法的原理就是根據的父節點,於是這題就很容易解決了。。
求距離很好求。關鍵是求路徑第k個點,顯然這個點要麼是u的第k個父節點,要麼是v的第k個父節點,於是乎,先求一次LCA,判斷是u還是v的,然後用倍增法找到那個點即可。
代碼如下:

#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 int mod=1e7+3;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=10000+10;int dep[MAXN], dis[MAXN], dp[MAXN][30];int head[MAXN], cnt, n;struct node{        int u, v, w, next;}edge[MAXN<<1];void add(int u, int v, int w){        edge[cnt].v=v;        edge[cnt].w=w;        edge[cnt].next=head[u];        head[u]=cnt++;}void dfs(int u, int fa){        for(int i=head[u];i!=-1;i=edge[i].next){                int v=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;        }}struct BZ{        int i, j;        void init()        {                for(j=1;(1<<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];                        }                }        }        int Query(int u, int v)        {                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<<j)>=dep[v])                                u=dp[u][j];                }                if(u==v) return u;                for(j=i;j>=0;j--){                        if(dp[u][j]!=dp[v][j]){                                u=dp[u][j];                                v=dp[v][j];                        }                }                return dp[u][0];        }        int kth(int u, int v, int k)        {                int root=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<<j)>=ans)                                        u=dp[u][j];                        }                        return u;                }                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<<j)>=ans)                                        v=dp[v][j];                        }                        return v;                }        }}bz;void init(){        memset(head,-1,sizeof(head));        memset(dep,0,sizeof(dep));        memset(dis,0,sizeof(dis));        memset(dp,-1,sizeof(dp));        cnt=0;}int main(){        int T, i, u, v, w, k;        char s[10];        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系列- Query on a tree II (倍增LCA)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.