SPOJ QTREE2 - Query on a tree II LCA+思维

https://www.spoj.com/problems/QTREE2/en/

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个节点,n-1条边,问树上两个节点的距离或者两个节点的路径上第k个点。

思路:倍增LCA即可。距离问题就不说了,说一下路径上第k个点。倍增求u、v的LCA时,首先要把u和v跳到同一深度,我们不妨设这个函数为skip,skip(u,leve)表示把节点u往上爬level层。(内部的思想是二进制 因此是log(level)的复杂度)我们先拿u到LCA(u,v)的深度之差与k做比较,若小雨等于k,u直接用skip向上爬即可。否则我们让u爬到LCA(u,v),同时k减去这个深度的贡献,现在我们反向思维,让u从LCA(u,v)向下爬不就等同于让v向LCA(u,v)爬吗,我们根据深度算出v应该爬的高度再利用skip函数就行了。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;

const int maxn=1e4+5;

struct edge
{
    int to,nxt,dis;
}Edge[maxn<<1];

int n,m,tot=0;
int head[maxn];
int deep[maxn];
int cost[maxn];
int dist[maxn];
int fa[maxn][30];
int bs[30];
char op[10];

inline void addedge(int u,int v,int dis)
{
    Edge[++tot].to=v,Edge[tot].nxt=head[u],Edge[tot].dis=dis,head[u]=tot;
    Edge[++tot].to=u,Edge[tot].nxt=head[v],Edge[tot].dis=dis,head[v]=tot;
}

void dfs(int cur,int father)
{
    deep[cur]=deep[father]+1;
    fa[cur][0]=father;
    for(int i=1;i<=20;i++)
        fa[cur][i]=fa[fa[cur][i-1]][i-1];
    for(int i=head[cur];i;i=Edge[i].nxt)
        if(Edge[i].to!=father)
            dist[Edge[i].to]=dist[cur]+Edge[i].dis,cost[Edge[i].to]=cost[cur]+1,
            dfs(Edge[i].to,cur);
}

inline int skip(int x,int level)
{
    for(int i=20;i>=0;i--)
    {
        if(bs[i]&level)
            x=fa[x][i];
    }
    return x;
}

inline int LCA(int u,int v)
{
    if(deep[u]<deep[v])
        swap(u,v);
    u=skip(u,deep[u]-deep[v]);
    if(u==v)
        return u;
    for(int i=20;i>=0;i--)
        if(fa[u][i]!=fa[v][i])
            u=fa[u][i],v=fa[v][i];
    return fa[u][0];
}

int main()
{
    for(int i=0;i<=20;i++)
        bs[i]=1<<i;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        tot=0;
        memset(head,0,sizeof(head));
        scanf("%d",&n);
        int u,v,dis;
        for(int i=1;i<n;i++)
        {
            scanf("%d %d %d",&u,&v,&dis);
            addedge(u,v,dis);
        }
        dfs(1,0);
        int grand,tmp;
        while(~scanf("%s",op)&&op[1]!='O')
        {
            if(op[0]=='D')
            {
                scanf("%d %d",&u,&v);
                grand=LCA(u,v);
                printf("%d\n",dist[u]-2*dist[grand]+dist[v]);
            }
            else
            {
                scanf("%d %d %d",&u,&v,&dis);
                --dis;
                grand=LCA(u,v);
                tmp=cost[u]-cost[grand];
                if(dis<=tmp)
                    printf("%d\n",skip(u,dis));
                else
                {
                    dis-=tmp;
                    tmp=cost[v]-cost[grand];
                    tmp-=dis;
                    printf("%d\n",skip(v,tmp));
                }
            }
        }
        if(t>0)
            printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值