动态树LCT(SPOJ QTREE2 - Query on a tree II)

QTREE2 - Query on a tree II

no tags 

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


有两种操作:DIST:询问两点之间的距离,KTH:询问u->v第k个是谁

这里要好好理解一下那个Qth函数,其实就是lca的变形

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=300010;
const int INF=1000000000;
int N;
struct Edge
{
    int v,next,w;
}edge[maxn*2];
int head[maxn],tot;
void add_edge(int u,int v,int w)
{
    edge[tot].v=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;
}


struct LCT
{
    int ch[maxn][2],pre[maxn],key[maxn];
    int rev[maxn],sum[maxn];
    bool rt[maxn];
    int size[maxn];
    void dfs(int u,int f)
    {
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v==f)continue;
            pre[v]=u;
            key[v]=edge[i].w;
            dfs(v,u);
        }
    }
    void update_rev(int r)
    {
        if(!r)return;
        swap(ch[r][0],ch[r][1]);
        rev[r]^=1;
    }
    void pushdown(int r)
    {
        if(rev[r])
        {
            update_rev(ch[r][1]);
            update_rev(ch[r][0]);
            rev[r]=0;
        }
    }
    void pushup(int r)
    {
        sum[r]=sum[ch[r][0]]+sum[ch[r][1]]+key[r];
        size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
    }
    void rotate(int x)
    {
        int y=pre[x],kind=ch[y][1]==x;
        ch[y][kind]=ch[x][!kind];
        pre[ch[y][kind]]=y;
        pre[x]=pre[y];
        pre[y]=x;
        ch[x][!kind]=y;
        if(rt[y])rt[y]=false,rt[x]=true;
        else ch[pre[x]][ch[pre[x]][1]==y]=x;
        pushup(y);
    }
    //将根节点到r的路径上的所有及诶单的标记下方
    void P(int r)
    {
        if(!rt[r])P(pre[r]);
        pushdown(r);
    }
    void Splay(int r)
    {
        P(r);
        while(!rt[r])
        {
            int f=pre[r],ff=pre[f];
            if(rt[f])rotate(r);
            else if((ch[ff][1]==f)==(ch[f][1]==r))
                rotate(f),rotate(r);
            else rotate(r),rotate(r);
        }
        pushup(r);
    }
    int Access(int x)
    {
        int y=0;
        for(;x;x=pre[y=x])
        {
            Splay(x);
            rt[ch[x][1]]=true,rt[ch[x][1]=y]=false;
            pushup(x);
        }
        return y;
    }
     int getroot(int x)
     {
        Access(x);
        Splay(x);
        while (ch[x][0])
            x = ch[x][0];
        return x;
    }
    //判断是否同根
    bool judge(int u,int v)
    {
        while(pre[u])u=pre[u];
        while(pre[v])v=pre[v];
        return u==v;
    }
    //将r变成他所在根
    void mroot(int r)
    {
        Access(r);
        Splay(r);
        update_rev(r);
    }
    //调用后u是原来u和v的lca,v和ch[u][1]分别存折lca的两个儿子
    int lca(int &u,int &v)
    {
        Access(v),v=0;
        int ans=0;
        while(u)
        {
            Splay(u);
            if(!pre[u])return sum[ch[u][1]]+sum[v];
            rt[ch[u][1]]=true;
            rt[ch[u][1]=v]=false;
            pushup(u);
            u=pre[v=u];
        }
    }
    //将u合并到v上
    void link(int u,int v)
    {
        mroot(u);
        pre[u]=v;
    }
    //将v和他的父节点分离
    void cut(int v)
    {
        //mroot(v);
        Access(v);
        Splay(v);
        pre[ch[v][0]]=0;
        pre[v]=0;
        rt[ch[v][0]]=true;
        ch[v][0]=0;
        pushup(v);
    }
    void init()
    {
        memset(head,-1,sizeof(head));
        memset(pre,0,sizeof(pre));
        memset(ch,0,sizeof(ch));
        memset(rev,0,sizeof(rev));
        memset(sum,0,sizeof(sum));
        tot=0;
        for(int i=0;i<=N;i++)rt[i]=true,size[i]=1;
        size[0]=0;
    }
    int get_kth(int r,int k)
    {
        while(size[ch[r][0]]+1!=k)
        {
            if(k<size[ch[r][0]]+1)r=ch[r][0];
            else
            {
                k-=size[ch[r][0]]+1;
                r=ch[r][1];
            }
        }
        return r;
    }
    //ch[v][1]是左孩子,u树右孩子
    int QKth(int u,int v,int K)
    {
        Access(u),u=0;
        int ans=0;
        while(v)
        {
            Splay(v);
            if(!pre[v])
            {
                if(size[ch[v][1]]+1==K)return v;
                else if(size[ch[v][1]]+1>K)
                    return get_kth(ch[v][1],size[ch[v][1]]+1-K);
                else return get_kth(u,K-(size[ch[v][1]]+1));
            }
            rt[ch[v][1]]=true;
            rt[ch[v][1]=u]=false;
            pushup(v);
            v=pre[u=v];
        }
    }
}tree;

int main()
{
    int T;
    scanf("%d",&T);
    int u,v,c;
    char op[10];
    while(T--)
    {
        scanf("%d",&N);
        tree.init();
        for(int i=1;i<N;i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            add_edge(u,v,c);
            add_edge(v,u,c);
        }
        tree.dfs(1,0);
        while(scanf("%s",op)!=EOF)
        {
            if(op[1]=='O')break;
            if(op[0]=='D')
            {
                scanf("%d%d",&u,&v);
                printf("%d\n",tree.lca(u,v));
            }
            else
            {
                int K;
                scanf("%d%d%d",&u,&v,&K);
                printf("%d\n",tree.QKth(u,v,K));
            }
        }
    }
    return 0;
}







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值