hdu3078

Network

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


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!
 

此题直接暴力,用一个pre数组记录父亲,然后直接利用这个数组走到LCA,将中途的数据排序一下即可。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#define Maxn 80010
#define Maxm 30010
using namespace std;

struct edge{
    int fr,to,w,lca,next;
}p[Maxn<<1],ask[Maxm<<1];
int head[Maxn],ah[Maxn];
int tot,tot1;
void addedge(int a,int b){
    p[tot].to=b;
    p[tot].next=head[a];
    head[a]=tot++;
}
void addedge1(int a,int b,int c){
    ask[tot1].fr=a;
    ask[tot1].to=b;
    ask[tot1].w=c;
    ask[tot1].next=ah[a];
    ah[a]=tot1++;
}
int fa[Maxn];
int findset(int x){
    return fa[x]==x?x:(fa[x]=findset(fa[x]));
}
void unionset(int a,int b){
    fa[findset(a)]=findset(b);
}
int vis[Maxn];
int anc[Maxn];
int cur[Maxn];
int pre[Maxn];
int st[Maxn],top;
void LCA(int x,int n){
    memcpy(cur,head,sizeof head);
    for(int i=1;i<=n;i++) anc[i]=i;
    st[top=1]=x;
    pre[x]=-1;
    while(top){
        int u=st[top];
        bool flag=false;
        while(cur[u]!=-1){
            int i=cur[u],v=p[i].to;
            if(v==pre[u]){
                cur[u]=p[i].next;
                continue;
            }
            st[++top]=v;
            pre[v]=u;
            cur[u]=p[i].next;
            flag=true;
            break;
        }
        if(flag) continue;
        vis[u]=1;
        for(int i=ah[u];i!=-1;i=ask[i].next){
            int v=ask[i].to;
            if(vis[v])
                ask[i].lca=ask[i^1].lca=anc[findset(v)];
        }
        top--;
        if(top){
            unionset(u,st[top]);
            anc[findset(u)]=st[top];
        }
    }
}

void init(int n){
    tot=tot1=0;
    memset(head,-1,sizeof head);
    memset(ah,-1,sizeof ah);
    memset(vis,0,sizeof vis);
    for(int i=1;i<=n;i++) fa[i]=i;
}
int val[Maxn];
void solve(int id){
    int fr=ask[id].fr,to=ask[id].to,lca=ask[id].lca;
    top=0;
    while(fr!=lca) st[top++]=val[fr],fr=pre[fr];
    while(to!=lca) st[top++]=val[to],to=pre[to];
    st[top++]=val[lca];
    if(top<ask[id].w) puts("invalid request!");
    else{
        sort(st,st+top);
        printf("%d\n",st[top-ask[id].w]);
    }
}
int main()
{
    int n,m,a,b,c;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",val+i);
    init(n);
    for(int i=1;i<n;i++){
        scanf("%d%d",&a,&b);
        addedge(a,b);
        addedge(b,a);
    }
    while(m--){
        scanf("%d%d%d",&c,&a,&b);
        if(c==0) val[a]=b;
        else{
            addedge1(a,b,c);
            addedge1(b,a,c);
        }
    }
    LCA(1,n);
    for(int i=0;i<tot1;i+=2) solve(i);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值