天天看點

hdu 3078(LCA的線上算法)

Network

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 847    Accepted Submission(s): 347

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!

題意:給出n個點,m條詢問,依次輸入n個點都有權值,以及n-1條邊,接下來m條詢問每次輸入 k a b如果 k=0 ,則更新a點的權值為b,如果k>0,則輸出a-b這條路徑上權值第K大的點,如果沒有第K大點,輸出invalid request!

今天做這個題,正好借這個題總結一下LCA的線上算法。

hdu 3078(LCA的線上算法)

上圖為測試用例:

LCA的線上算法,先要對此樹進行預處理(為以後的RMQ做準備),即進行深搜。

我們設立三個數組:ver,first,deep ver數組儲存樹的前序周遊結點的結果,first儲存結點第一次出現的位置,deep儲存每個節點出現的深度

對此樹進行先序周遊:

ver: 1 2 1 3 4 3 5 3 1

deep:1 2 1 2 3 2 3 2 1

first:1 2 4 5 7

從這裡可以知道 ver和deep都要開兩倍大小。

然後如果查詢結點 2 5,先找到2以及5第一次出現的位置,first[2] = 2,first[5] = 7

那麼2和5的最近公共祖先必定在ver[2]-ver[7]區間中。那麼接下來,我們隻要拿出這段區間内深度最小的點,那麼這個點就肯定是2 5的最近公共祖先了

是以這裡找區間最小值我們就可以利用RMQ算法了(這裡RMQ的數組儲存的是下标,因為deep和ver數組是通過下标聯系起來的)。找到了2 1 2 3 2 3中

深度最小的是deep[3],對應ver中的ver[3]=1,是以2 和 5的最近公共祖先是1

RMQ算法:http://dongxicheng.org/structure/lca-rmq/

了解了過程,代碼也就能夠寫出來了。

然後關于這題:我們要找a - b中的第K大點,那麼我們記錄一個父親數組,u->lca,v->lca反着找就OK。

/*
5 5
5 1 2 3 4 3 1 2 1 4 3 5 3
2 4 5
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
#define N 80005
using namespace std;

struct Edge{
    int u,v,next;
}edge[2*N];
int head[N];
int deep[2*N];
int vis[N];
int first[N];
int ver[2*N];
int father[N];
int dp[2*N][25];
int value[N];
int path[N];
int tot;


void add_edge(int u,int v,int &k){  ///Á´Ê½Ç°ÏòÐÇ
    edge[k].u = u,edge[k].v = v;
    edge[k].next = head[u];head[u] = k++;
}
void dfs(int u,int dep,int pre){
    vis[u]=true,ver[++tot]=u,first[u]=tot,deep[tot]=dep,father[u]=pre;
    for(int k=head[u];k!=-1;k=edge[k].next){
        if(!vis[edge[k].v]){
            dfs(edge[k].v,dep+1,u);
            ver[++tot] = u,deep[tot]=dep;
        }
    }
}
int MIN(int i,int j){
    if(deep[i]<deep[j]) return i;
    return j;
}
void init_RMQ(int n){
    for(int i=1;i<=n;i++) dp[i][0]=i;
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            dp[i][j] = MIN(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
}
int RMQ(int L,int R){
    int k = (int)(log(R-L+1.0)/log(2.0));
    int a = dp[L][k];
    int b = dp[R-(1<<k)+1][k];
    return MIN(a,b);
}
int LCA(int a,int b){
    int x = first[a],y = first[b];
    //printf("%d %d\n",deep[x],deep[y]);
    if(x>y) swap(x,y);
    //printf("RMQ(x,y): %d\n",RMQ(x,y));
    return ver[RMQ(x,y)];
}
int cmp(int a,int b){
    return a>b;
}
void solve(int k,int u,int v){
    int lca = LCA(u,v);
    tot=0;
    while(u!=lca){
        path[tot++] = value[u];
        u = father[u];
    }
    while(v!=lca){
        path[tot++] = value[v];
        v = father[v];
    }
    //printf("lca = %d\n",lca);
    path[tot++] = value[lca];
    //printf("path[tot-1] = %d\n",path[tot-1]);
    if(k>tot){
        printf("invalid request!\n");
        return;
    }
    sort(path,path+tot,cmp);
    //for(int i=0;i<tot;i++) printf("%d ",path[i]);
    printf("%d\n",path[k-1]);
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(head,-1,sizeof(head));
        memset(father,-1,sizeof(father));
        for(int i=1;i<=n;i++){
            scanf("%d",&value[i]);
        }
        tot = 0;
        for(int i=1;i<n;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v,tot);
            add_edge(v,u,tot);
        }
        tot = 0;
        dfs(1,1,-1);
        init_RMQ(2*n-1);
        while(m--){
            int k,a,b;
            scanf("%d%d%d",&k,&a,&b);
            if(k==0){
                value[a]=b;
            }else{
                solve(k,a,b);
            }
        }
    }
    return 0;
}