樹上倍增 hdu 2586

來源:互聯網
上載者:User

標籤:style   lca   位置   部落格   標記   scanf   include   art   ++   

參考部落格:

代碼:

#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>#include<string.h>using namespace std;const int max_=8e4+1;int depth[max_];//節點深度int gw[max_][25];//第 i 點到2^j父親的距離int fa[max_][25];//第 i 點的2^j父親bool vis[max_];//標記數組int tot,N;struct Tree//樹{     int to;     int w;     int next;}a[max_*2];int edge[max_*2];void add_edge(int x,int y,int w)//建樹{    a[tot].to=y;    a[tot].w=w;    a[tot].next=edge[x];    edge[x]=tot++;}void dfs(int now,int deep)//dfs預先處理{    if(vis[now]==0)    {        vis[now]=1;        depth[now]=deep;//記錄節點的深度    }    for(int i=edge[now];i!=-1;i=a[i].next)    {        int to=a[i].to;        if(vis[to])        continue;        int w=a[i].w;        gw[to][0]=w;//to到父親的距離        fa[to][0]=now;//to的父親節點        dfs(to,deep+1);    }}void LCA_init(int n)//預先處理{    for(int i=1;i<=n;i++)        for(int j=1;j<=N;j++)    {        fa[i][j]=fa[fa[i][j-1]][j-1];        gw[i][j]=gw[fa[i][j-1]][j-1]+gw[i][j-1];    }}int LCA_Q(int u,int v){    int ans=0;//距離    if(depth[u]<depth[v])        swap(u,v);//保證大減小    int k;        k=depth[u]-depth[v];    for(int i=0;(1<<i)<=k;i++)    {        if((1<<i)&k)           ans+=gw[u][i],u=fa[u][i];    }        if(u==v)            return ans;//找到了就退出        for(int i=N;i>=0;i--)        {            if(fa[u][i]!=fa[v][i])            {                ans+=gw[u][i],                ans+=gw[v][i];                u=fa[u][i],                v=fa[v][i];            }        }        return ans+=gw[v][0]+gw[u][0];}void init(int n)//初始化{    memset(vis,0,sizeof(vis));    tot=0;    N=(int)(log(n+0.0)/log(2));    memset(edge,-1,sizeof(edge));}int main(){    int t;    cin>>t;    while(t--)    {        int n,m;        scanf("%d %d",&n,&m);        init(n);        for(int i=0;i<n-1;i++)        {            int u,v,w;            scanf("%d %d %d",&u,&v,&w);            add_edge(u,v,w);            add_edge(v,u,w);        }        dfs(1,0);        LCA_init(n);        while(m--)        {            int u,v;            scanf("%d %d",&u,&v);            int L=LCA_Q(u,v);            printf("%d\n",L);        }       /* for(int i=1;i<=n;i++)            printf("%d ",fa[i][2]);*/    }}/*13 21 2 11 3 11 4 12 5 13 6 16 10 16 11 110 13 14 7 14 8 14 9 18 12 1*/  /*  for(int x=max0;x>=0;x--)     //注意!此處迴圈必須是從大到小!因為我們應該越提越“精確”,        if(fa[u][x]!=fa[v][x])   //如果從小到大的話就有可能無法提到正確位置,自己可以多想一下        {            u=fa[u][x];            v=fa[v][x];        }    return fa[u][0];    //此時u、v的第一個父親就是LCA。*/

 

樹上倍增 hdu 2586

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.