hdu3078(带RMQ的在线LCA)

Problem Description

The ALPC company is now working on his own network system, which is connecting all N ALPC department. To economize on spending, the backbone network has only one router for each department, and N-1 optical fiber in total to connect all routers.
The usual way to measure connecting speed is lag, or network latency, referring the time taken for a sent packet of data to be received at the other end.
Now the network is on trial, and new photonic crystal fibers designed by ALPC42 is trying out, the lag on fibers can be ignored. That means, lag happened when message transport through the router. ALPC42 is trying to change routers to make the network faster, now he want to know that, which router, in any exactly time, between any pair of nodes, the K-th high latency is. He needs your help.

Input

There are only one test case in input file.
Your program is able to get the information of N routers and N-1 fiber connections from input, and Q questions for two condition: 1. For some reason, the latency of one router changed. 2. Querying the K-th longest lag router between two routers.
For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000.
Then n integers in second line refer to the latency of each router in the very beginning.
Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y.
Then q lines followed to describe questions, three numbers k, a, b for each line. If k=0, Telling the latency of router a, Ta changed to b; if k>0, asking the latency of the k-th longest lag router between a and b (include router a and b). 0<=b<100000000.
A blank line follows after each case.

Output

For each question k>0, print a line to answer the latency time. Once there are less than k routers in the way, print "invalid request!" instead.

Sample Input

5 5 5 1 2 3 4 3 1 2 1 4 3 5 3 2 4 5 0 1 2 2 2 3 2 1 4 3 3 5

Sample Output

3 2 2 invalid request!

思路很简单,找到两个点的最近公共祖先,然后记录father,挨个找,排个序然后输出,注意细节。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=8e4;
int ver[maxn*2-1],deep[maxn*2-1];
int first[maxn],father[maxn],latency[maxn];
int dp[maxn*2-1][25];
bool vis[maxn];
int n,cnt;
struct Edge
{
    int u,v,val;
};
vector<Edge> edges;
vector<int> object[maxn];
void AddEdge(int u,int v);
void dfs(int now,int bef,int dep);
void st();
int rmq(int l,int r);
int comp(int k,int a,int b,int fa);
int main()
{
    int q,u,v,k,a,b;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        cnt=0;
        edges.clear();
        for(int i=0;i<n;i++) scanf("%d",&latency[i]),object[i].clear();
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d",&u,&v);
            u--,v--;
            AddEdge(u,v),AddEdge(v,u);
        }
        memset(vis,0,sizeof(vis));
        dfs(0,-1,0);
        st();
        for(int i=0;i<q;i++)
        {
            scanf("%d%d%d",&k,&a,&b);
            if(k==0) latency[a-1]=b;
            else
            {
                a--,b--;
                int fa=rmq(a,b);
                int ans=comp(k,a,b,fa);
                if(ans==-1)
                    printf("invalid request!\n");
                else
                    printf("%d\n",ans);
            }
        }
    }
    return 0;
}
void AddEdge(int u,int v)
{
    edges.push_back(Edge{u,v,0});
    object[u].push_back(edges.size()-1);
}
void dfs(int now,int bef,int dep)
{
    ver[cnt]=now,deep[cnt]=dep,first[now]=cnt;
    vis[now]=true;
    father[now]=bef;
    cnt++;
    for(int i=0;i<object[now].size();i++)
    {
        Edge &e=edges[object[now][i]];
        if(!vis[e.v])
        {
            dfs(e.v,now,dep+1);
            ver[cnt]=now,deep[cnt]=dep;
            cnt++;
        }
    }
}
void st()
{
    int all=2*n-1;
    for(int i=0;i<all;i++) dp[i][0]=i;
    for(int j=1;(1<<j)<=all;j++)
    for(int i=0;i+(1<<j)-1<all;i++)
    {
        int l=dp[i][j-1],r=dp[i+(1<<(j-1))][j-1];
        dp[i][j]=deep[l]<deep[r]?l:r;
    }
}
int rmq(int l,int r)
{
    int a=first[l],b=first[r];
    if(a>b) swap(a,b);
    int k=log(b-a+1)/log(2);
    int ll=dp[a][k],rr=dp[b-(1<<k)+1][k];
    return ver[deep[ll]<deep[rr]?ll:rr];
}
int comp(int k,int a,int b,int fa)
{
    vector<int> temp;
    if(fa==a&&fa==b)
        temp.push_back(latency[fa]);
    else if(fa==a||fa==b)
    {
        if(deep[first[a]]<deep[first[b]]) swap(a,b);
        temp.push_back(latency[a]);
        temp.push_back(latency[b]);
        while(father[a]!=b)
        {
            a=father[a];
            temp.push_back(latency[a]);
        }
    }
    else
    {
        temp.push_back(latency[a]);
        temp.push_back(latency[b]);
        temp.push_back(latency[fa]);
        while(father[a]!=fa)
        {
            a=father[a];
            temp.push_back(latency[a]);
        }
        while(father[b]!=fa)
        {
            b=father[b];
            temp.push_back(latency[b]);
        }
    }
    sort(temp.begin(),temp.end());
    if(k>temp.size())
        return -1;
    return temp[temp.size()-k];
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值