SPOJ Query on a tree II (倍增LCA)

Time Limit: 433MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu

Status

Description

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
    or
  • 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)

Input

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.

Output

For each "DIST" or "KTH" operation, write one integer representing its result.

Print one blank line after each test.

Example

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

【题意】

           给了一颗n-1条边的树,有两种询问1:u,v的距离。2:u,v之间的第k个点。前面求一下lca就可以了,后面直接倍增搞一下就好啦。

【AC 代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=10010;
const int maxm=15;
int n;
int dp[maxn],dep[maxn],p[maxm][maxn];
struct edge{
    int v,w,next;
    edge(){}
}E[maxn*2];
int head[maxn],tot;
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}
void addedge(int u,int v,int w)
{
    E[tot].v=v,E[tot].w=w,E[tot].next=head[u],head[u]=tot++;
}
void dfs(int u,int f,int d)
{
    dep[u]=d;
    p[0][u]=f;
    for(int i=head[u]; ~i; i=E[i].next)
    {
        int v=E[i].v;
        if(v==f) continue;
        dp[v]=dp[u]+E[i].w;
        dfs(v,u,d+1);
    }
}
void build()
{
    dfs(1,-1,0);
    for(int i=0; i+1<maxm; i++)
    {
        for(int v=1; v<=n; v++)
        {
            if(p[i][v]<0) p[i+1][v]=-1;
            else p[i+1][v]=p[i][p[i][v]];
        }
    }
}
int lca(int u,int v)
{
    if(dep[u]>dep[v]) swap(u,v);
    for(int i=0; i<maxm; i++)
    {
        if((dep[v]-dep[u])>>i&1)
            v=p[i][v];
    }
    if(u==v) return u;
    for(int i=maxm-1; ~i; --i)
    {
        if(p[i][u]!=p[i][v])
        {
            u=p[i][u];
            v=p[i][v];
        }
    }
    return p[0][u];
}
int get(int u,int k)
{
    for(int i=0; i<maxm; i++)
        if(k>>i&1) u=p[i][u],k^=(1<<i);
    return u;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        int u,v,w;
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        build();
        char op[10];
        while(1)
        {
            scanf("%s",op);
            if(op[1]=='O') break;
            scanf("%d%d",&u,&v);
            if(op[1]=='I')
            {
                int _lca=lca(u,v);
                printf("%d\n",dp[u]+dp[v]-2*dp[_lca]);
            }
            else
            {
                int k;
                scanf("%d",&k);
                k--;
                int _lca=lca(u,v);
                int l=dep[u]-dep[_lca];
                int d=dep[u]+dep[v]-2*dep[_lca];
                if(k==0) printf("%d\n",u);
                else if(k==l) printf("%d\n",_lca);
                else if(k<l)  printf("%d\n",get(u,k));
                else if(k==d) printf("%d\n",v);
                else{
                    k=d-k;
                    printf("%d\n",get(v,k));
                }
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值