Network(HDU3078,LCA朴素算法应用)

本文解析了HDU3078 Network问题,介绍了一种在树形网络中进行点权修改和查询第k大点权的算法。通过树链剖分和主席树维护,或使用朴素算法直接找出路径上的所有点并排序找到第k大值。

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

HDU3078

Network

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2505 Accepted Submission(s): 1092

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!

Source

HDU3078

分析:

在一棵树上,有两个操作,一个是修改点权,一个是询问u到v上第k大的点权。如果数据范围比较大,可以树链剖分+主席树维护第k大。但是本题直接把路径上的点求出来,在排序找第k大就能过,既然要找出所以路径上的点,我们就可以用朴素算法来求。

参考代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 80005
using namespace std;

struct Edge {
    int to,next;
}edge[MAXN*2];
int head[MAXN],tot,cnt;
int deep[MAXN];
int fa[MAXN];
int t[MAXN];
int w[MAXN];
void init() {
    tot=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v) {
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
// 求深度和父结点
void dfs(int u,int pre,int d) {
    deep[u]=d;
    fa[u]=pre;
    for (int i=head[u];i!=-1;i=edge[i].next) {
        int v=edge[i].to;
        if (v!=pre) dfs(v,u,d+1);
    }
}
int getans(int u,int v,int k) {
    cnt=0;
    // 朴素算法
    while(u!=v) {
        if (deep[u]>deep[v])
            t[++cnt]=w[u],u=fa[u];
        else
            t[++cnt]=w[v],v=fa[v];
    }
    t[++cnt]=w[u];
    // k大于总个数时返回-1
    if (cnt<k) return -1;
    sort(t+1,t+cnt+1,greater<int>());
    return t[k];
}

int main() {
    int N,Q,u,v,k,a,b,ans;
    init();
    scanf("%d%d",&N,&Q);
    for (int i=1;i<=N;i++)
        scanf("%d",&w[i]);
    for (int i=1;i<=N-1;i++) {
        scanf("%d%d",&u,&v);
        addedge(u,v);
        addedge(v,u);
    }
    dfs(1,1,1);
    for (int i=1;i<=Q;i++) {
        scanf("%d%d%d",&k,&a,&b);
       	// 修改点权
        if (k==0) w[a]=b;
        else {
            ans=getans(a,b,k);
            if (ans==-1) printf("invalid request!\n");
            else printf("%d\n",ans);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值