HDU - 3078 - Network(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!
 

题意:

有n个路由器,每个路由器有各自的延时,它们由n-1根光纤连接起来,然后有Q个询问,每个询问包含三个整数k,a,b

1、k=0 时,将路由器a的延时变为b

2、k>0时,输出路由器a到b的整个路线中第k大的延时输出,若没有则输出invalid request!

思路:

n个路由器的连接就是一个生成树,然后我们要找到a->b的路径,然后再将路径中的第k大的数输出

找路径可以先找到ab的最近公共祖先,然后两边同时向上到祖先,中间经过的点就是路径中的点

然后存一下,排序一下,输出第k个


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int n,q,a[N],x,y,k;
int v[N],nt[N],ft[N],sz = 0;
int dep[N],fa[N][20];
int cnt,b[N];
void dfs(int pos,int deep){
    dep[pos] = deep;
    for(int i=ft[pos]; i; i=nt[i]){
        if(dep[v[i]]) continue;
        dfs(v[i], deep+1);
        fa[v[i]][0] = pos;
    }
}
void init_fa(){
    for(int i=1;i<=log2(n);i++){
        for(int j=1;j<=n;j++){
            fa[j][i] = fa[fa[j][i-1]][i-1];
        }
    }
}
int slove(int a,int b){
    if(dep[a]<dep[b]) swap(a,b);
    for(int i=log2(n);i>=0;i--){
        if(dep[fa[a][i]]>=dep[b]) a = fa[a][i];
    }
    if(a==b) return a;
    for(int i=log2(n);i>=0;i--){
        if(fa[a][i]!=fa[b][i]){
            a = fa[a][i];
            b = fa[b][i];
        }
    }
    return fa[a][0];
}
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        v[++sz] = y; nt[sz] = ft[x]; ft[x] = sz;
        v[++sz] = x; nt[sz] = ft[y]; ft[y] = sz;
    }
    dfs(1,1);
    init_fa();
    for(int i=0;i<q;i++){
        scanf("%d%d%d",&k,&x,&y);
        if(k==0){
            a[x] = y;
            continue;
        }
        int faa = slove(x, y);
        if(dep[x]-dep[faa]+dep[y]-dep[faa]+1<k){
            printf("invalid request!\n");
            continue;
        }
        cnt = 0;
        while(x!=faa){
            b[cnt++] = a[x];
            x = fa[x][0];
        }
        while(y!=faa){
            b[cnt++] = a[y];
            y = fa[y][0];
        }
        b[cnt++] = a[x];
        sort(b,b+cnt);
        printf("%d\n",b[cnt-k]);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/w326159487/article/details/79684643