HDU 3078 - Network (LCA)【tarjan离线/DFS倍增】

该博客主要探讨了HDU 3078网络问题,涉及寻找两个节点a和b到它们最近公共祖先(LCA)的路径上的第k大点值。文章指出,通过直接模拟找LCA的方法在理论上可能无法通过所有测试用例,但因数据较弱,实际可行。

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

Network

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


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 55 1 2 3 43 12 14 35 32 4 50 1 22 2 32 1 43 3 5
 

Sample Output

 
322invalid request!
 

Source
 

找出a,b到他们lca的路径上的第k大点值。


POINT:

找出lca,模拟把每个点找到。这样理论上不能ac。因为数据弱,水过去了。


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 80000+111;
const int inf = 0x3f3f3f3f;
int val[maxn];
int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt=0;
int qhead[maxn],qto[maxn],qflag[maxn],qnxt[maxn],qcnt=0;
int pre[maxn],fa[maxn],vis[maxn];


void add(int u,int v)
{
    to[cnt]=v;
    nxt[cnt]=head[u];
    head[u]=cnt++;
}

void add1(int u,int v,int flag)
{
    qto[qcnt]=v;
    qflag[qcnt]=flag;
    qnxt[qcnt]=qhead[u];
    qhead[u]=qcnt++;
}


struct node
{
    int k,a,b;
    int ans;
}qu[maxn];

int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}

void merge(int x,int y)
{
    int xx=find(x);
    int yy=find(y);
    if(xx!=yy)
        fa[yy]=xx;
}

void tarjan(int u)
{
    vis[u]=1;
    for(int i=head[u];~i;i=nxt[i]){
        int v=to[i];
        if(vis[v]) continue;
        tarjan(v);
        merge(u,v);
        pre[v]=u;
    }
    for(int i=qhead[u];~i;i=qnxt[i]){
        int v=qto[i];
        if(vis[v]){
            qu[qflag[i]].ans=find(v);
        }
    }
}

bool cmd(int a,int b)
{
    return a>b;
}
int main()
{
    int n,q;
    scanf("%d %d",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%d",&val[i]);
        qhead[i]=head[i]=-1;
        fa[i]=i;
    }
    for(int i=1;i<n;i++){
        int u,v;scanf("%d %d",&u,&v);
        add(u,v);add(v,u);
    }
    for(int i=1;i<=q;i++){
        scanf("%d %d %d",&qu[i].k,&qu[i].a,&qu[i].b);
        if(qu[i].k>=1){
            add1(qu[i].a,qu[i].b,i);
            add1(qu[i].b,qu[i].a,i);
        }
    }
    tarjan(1);
    for(int i=1;i<=q;i++){
        vector<int>v;
        int k=qu[i].k;
        int a=qu[i].a;
        int b=qu[i].b;
        if(k==0){
            val[a]=b;
        }else{
            v.clear();
            int now = a;
            while(now!=qu[i].ans){
                v.push_back(val[now]);
                now=pre[now];
            }
            now=b;
            while(now!=qu[i].ans){
                v.push_back(val[now]);
                now=pre[now];
            }
            v.push_back(val[qu[i].ans]);
            if(v.size()<k){
                printf("invalid request!\n");
            }else{
                sort(v.begin(),v.end(),cmd);
                printf("%d\n",v[k-1]);
            }

        }
    }
//    printf("\n");

    
    return 0;
}



#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 80000+5;
int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt=0;
int fa[maxn][17],d[maxn];
int val[maxn],vis[maxn];
int n,Q;
void add(int x,int y)
{
    to[cnt]=y;
    nxt[cnt]=head[x];
    head[x]=cnt++;
}
void dfs(int u)
{
    vis[u]=1;
    for(int i=head[u];~i;i=nxt[i]){
        int v=to[i];
        if(vis[v]) continue;
        d[v]=d[u]+1;
        dfs(v);
        fa[v][0]=u;
    }
}
void init()
{
    for(int i=1;(1<<i)<=n;i++)
    for(int j=1;j<=n;j++){
        fa[j][i]=fa[fa[j][i-1]][i-1];
    }
}

int query(int a,int b)
{
    if(d[a]<d[b]) swap(a, b);
    int dis=d[a]-d[b];
    int c=0;
    while(dis){
        if(dis&1)
            a=fa[a][c];
        dis/=2;
        c++;
    }
    if(a==b) return a;
    else{
        for(int i=(int)log2(n);i>=0;i--){
            if(fa[a][i]!=fa[b][i])
                a=fa[a][i],b=fa[b][i];
        }
        a=fa[a][0];
        return a;
    }
}
bool cmd(int a,int b)
{
    return a>b;
}
int main()
{
    scanf("%d %d",&n,&Q);
    memset(head,-1,sizeof head);
    for(int i=1;i<=n;i++) scanf("%d",&val[i]);
    for(int i=1;i<n;i++){
        int u,v;scanf("%d %d",&u,&v);
        add(u,v);add(v,u);
    }
    d[0]=0;
    dfs(1);
    init();
    while(Q--){
        vector<int>v;
        int k,a,b;
        scanf("%d %d %d",&k,&a,&b);
        if(k==0){
            val[a]=b;
        }else{
            v.clear();
            int now = a;
            int ans=query(a, b);
//            printf("%d\n",ans);
//            int ci=0;//祖先找的不对!!!或者pre数组
            while(now!=ans){
                v.push_back(val[now]);
                now=fa[now][0];
            }
            now=b;
            while(now!=ans){
                v.push_back(val[now]);
                now=fa[now][0];
            }
            v.push_back(val[ans]);
            if(v.size()<k){
                printf("invalid request!\n");
            }else{
                sort(v.begin(),v.end(),cmd);
                printf("%d\n",v[k-1]);
            }

        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值