hdu3078Network

Network

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


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!
思路:对于查询第k个权的操作,先求出a,b的lca,然后将a到b路径上的点权都保存在一个数组中,然后降序排序,输出第k个元素即可
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>

using namespace std;
const int inf=0x3f3f3f3f;
const int M=2000005;
const int N=80005;
int head[N],h[N],dis[N],f[N],a[N],k[N],pre[N];
bool vis[N];
int cnt,num,tt;
int n,m,z;
bool cmp(int a,int b)
{
    return a>b;
}
struct node
{
    int u,v,w,next;
}e[N<<1],q[N<<1];
void addedge(int u,int v)
{
    e[num].v=v;
    e[num].next=head[u];
    head[u]=num++;
}
void addque(int u,int v,int w)
{
    q[tt].u=u;
     q[tt].v=v;
     q[tt].w=w;
     q[tt].next=h[u];
    h[u]=tt++;
    q[tt].u=v;
    q[tt].v=u;
     q[tt].w=w;
     q[tt].next=h[v];
    h[v]=tt++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(h,-1,sizeof(h));
    memset(vis,false,sizeof(vis));
    cnt=num=tt=0;
}
int findx(int x)
{
    if(x==f[x])return x;
    return f[x]=findx(f[x]);
}
void unio(int x,int y)
{
    int xx=findx(x);
    int yy=findx(y);
    if(yy!=xx)f[yy]=xx;

}
void tarjan(int u)
{
    vis[u]=true;
    f[u]=u;
    int i;
    for(i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].v;
        if(!vis[v])
        {
            pre[v]=u;
            tarjan(v);
            unio(u,v);
        }
    }
    for(i=h[u];i!=-1;i=q[i].next)
    {
        int v=q[i].v;
        if(vis[v])a[q[i].w]=findx(v);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        int i;
        int u,v,w;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&dis[i]);
        }
        for(i=0;i<n-1;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&w,&u,&v);
            if(w==0)
            {
                dis[u]=v;
            }
            else
            {
                addque(u,v,i);
                k[i]=w;
            }
        }
        tarjan(1);
        for(i=0;i<tt;i+=2)
        {
            vector<int>ch;
            u=q[i].u;
            v=q[i].v;
            w=q[i].w;
            int t=a[w];
            while(u!=t)ch.push_back(dis[u]),u=pre[u];
            while(v!=t)ch.push_back(dis[v]),v=pre[v];
            ch.push_back(dis[t]);
            if(ch.size()<k[w])
            {
                printf("invalid request!\n");
                continue;
            }
            else
            {
                sort(ch.begin(),ch.end(),cmp);
                printf("%d\n",ch[k[w]-1]);
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值