Query on a tree II

Query on a tree II

You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length.
We will ask you to perfrom some instructions of the following form:
DIST a b : ask for the distance between node a and node b
KTH a b k : ask for the k-th node on the path from node a to node b
Example:
N = 6
1 2 1 // edge connects node 1 and node 2 has cost 1
2 4 1
2 5 2
1 3 1
3 6 2

Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3)

The first line of input contains an integer t, the number of test cases (t <= 25). t test cases follow.

For each test case:
In the first line there is an integer N (N <= 10000)
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 100000)
The next lines contain instructions "DIST a b" or "KTH a b k"
The end of each test case is signified by the string "DONE".
There is one blank line between successive tests.

For each "DIST" or "KTH" operation, write one integer representing its result.
Print one blank line after each test.

Input:

1
6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE

Output:

5
3
```
树上倍增即可
同时倍增父亲和距离
注意计算中的+1 -1

#include <bits/stdc++.h>
using namespace std;
#define maxn (int)(1e5+10)
#define LL long long

inline int read(){
    int rtn=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))rtn=(rtn<<1)+(rtn<<3)+ch-'0',ch=getchar();
    return rtn*f;
}

struct node{
    int a,b,nt,w;
}e[maxn];

LL w[maxn][21];
int fa[maxn][21],dep[maxn],p[maxn],cnt;

inline void add(int x,int y,int z){
    e[++cnt].a=x;e[cnt].b=y;e[cnt].w=z;
    e[cnt].nt=p[x];p[x]=cnt;
}

inline void dfs(int k){
    for(int i=1;i<=20;i++)fa[k][i]=fa[fa[k][i-1]][i-1];
    for(int i=1;i<=20;i++)w[k][i]=w[fa[k][i-1]][i-1]+w[k][i-1];
    for(int i=p[k];i;i=e[i].nt){
        int kk=e[i].b;
        if(kk==fa[k][0])continue;
        fa[kk][0]=k;dep[kk]=dep[k]+1;w[kk][0]=e[i].w;
        dfs(kk);
    }
}

inline int lca(int x,int y){
    if(dep[x]<dep[y])swap(x,y);
    for(int i=20;i>=0;i--){
        if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
    } 
    if(x==y)return y;
    for(int i=20;i>=0;i--){
        if(fa[x][i]!=fa[y][i])
            x=fa[x][i],y=fa[y][i];
    }return fa[x][0];
}

inline LL dis(int x,int y){
    LL rtn=0;
    int l=lca(x,y);
    for(int i=20;i>=0;i--){
        if(dep[fa[x][i]]>=dep[l])
            rtn+=w[x][i],x=fa[x][i];
    }
    for(int i=20;i>=0;i--){
        if(dep[fa[y][i]]>=dep[l])
            rtn+=w[y][i],y=fa[y][i];
    }return rtn;
}

inline int kth(int x,int y,int k){
    int l=lca(x,y);
    if(k<=dep[x]-dep[l]){
        k-=1;
        for(int i=20;i>=0;i--){
            if(1<<i<=k)x=fa[x][i],k-=1<<i;
        }
        return x;
    }
    else if(k>dep[x]-dep[l]){
        k=(dep[y]-dep[l]-(k-(dep[x]-dep[l])))+1;
        for(int i=20;i>=0;i--){
            if(1<<i<=k)y=fa[y][i],k-=1<<i;
        }
        return y;
    }
}

int main(){
    int T=read();
    while(T--){
        int n=read();cnt=0;
        memset(p,0,sizeof(p));
        memset(w,0,sizeof(w));
        memset(fa,0,sizeof(fa));
        for(int i=1;i<n;i++){
            int a=read(),b=read(),w=read();
            add(a,b,w);add(b,a,w);
        }
        dfs(1);
        while(true){
            char ch[10];scanf("%s",ch);
            if(ch[1]=='O')break;
            else if(ch[1]=='T'){
                int x=read(),y=read(),k=read();
                printf("%d\n",kth(x,y,k));
            }
            else if(ch[1]=='I'){
                int x=read(),y=read();
                printf("%lld\n",dis(x,y));
            }
        }
    }
    return 0;
} 

转载于:https://www.cnblogs.com/DexterYsw/p/7954838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值