HDU-3078 Network (LCA)

本文介绍了一种算法,用于查找给定树结构中从节点u到节点v路径上的第K大节点。通过使用双亲节点乘法技巧在线找到最近公共祖先(LCA),并详细展示了如何遍历树以收集必要的节点信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

you can submit here
this question ask you to find the k-th biggest node in the path u->v.
every two node in a tree has only one path from one to another,and this path must past the LCA of u and v. so it’s wise to get all nodes follow : u->lca and lca->v.if there is no more than k node in the path,remember to print “invalid request!”.

this time I use multiplication algorithm by double the father node of a node to find the lca .it’s online.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN = 80010;
const int DEG = 30;
struct Edge
{
    int to,next;
} edge[MAXN*2];
int head[MAXN],tot;
void addedge(int u,int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;

}
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}
int fa[MAXN][DEG];//fa[i][j]表示结点i的第2^j个祖先
int pre[MAXN];
int deg[MAXN];//深度数组
void BFS(int root)
{
    queue<int>que;
    deg[root] = 0;
    fa[root][0] = root;
    pre[root] == root;
    que.push(root);
    while(!que.empty())
    {
        int tmp = que.front();
        que.pop();
        for(int i = 1; i < DEG; i++)
            fa[tmp][i] = fa[fa[tmp][i-1]][i-1];
        for(int i = head[tmp]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if(v == fa[tmp][0])continue;
            deg[v] = deg[tmp] + 1;
            fa[v][0] = tmp;
            que.push(v);
        }
    }
}
int LCA(int u,int v)
{
    if(deg[u] > deg[v])swap(u,v);
    int hu = deg[u], hv = deg[v];
    int tu = u, tv = v;
    for(int det = hv-hu, i = 0; det ; det>>=1, i++)
        if(det&1)
            tv = fa[tv][i];
    if(tu == tv)return tu;
    for(int i = DEG-1; i >= 0; i--)
    {
        if(fa[tu][i] == fa[tv][i])
            continue;
        tu = fa[tu][i];
        tv = fa[tv][i];
    }
    return fa[tu][0];
}
bool flag[MAXN];
int path[MAXN];
int ans[MAXN];
int n,Q;
int main()
{
    while(scanf("%d%d",&n,&Q) != EOF)
    {
        init();
        memset(flag,0,sizeof(flag));
        for(int i = 1; i<=n; i++)
        {
            scanf("%d",&path[i]);
        }

        for(int i = 0; i<n-1; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            flag[v] = 1;
            addedge(u,v);
            addedge(v,u);
        }
        int root;
        for(int i = 1; i<=n; i++)
        {
            if(!flag[i])
            {
                root=  i;
                break;
            }
        }
        BFS(root);
        for(int i = 0 ; i<Q; i++)
        {
            int op,x,y;
            scanf("%d%d%d",&op,&x,&y);
            if(op == 0)
            {
                path[x] = y;
            }
            else
            {
                int lca = LCA(x,y);

                //printf("lca :%d\n",lca);

                int cnt = 0;
                //memset(ans,0,sizeof(ans));
                while(x != lca)
                {
                    ans[cnt++] = path[x];
                    x = fa[x][0];
                }
                while(y != lca)
                {
                    ans[cnt++] = path[y];
                    y = fa[y][0];
                }
                ans[cnt++] = path[lca];
                if(cnt < op)
                {
                    printf("invalid request!\n");
                }
                else
                {
                    sort(ans,ans+cnt,greater<int>());
                    printf("%d\n",ans[op-1]);
                }
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值