zoukankan      html  css  js  c++  java
  • HDU 3078 Network

    Network




    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!
     
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int N=8e4+5;
    vector<int>G[N];
    int f[N][32],d[N],w[N],n,q;
    bool cmp(int a,int b)
    {
        return a>b;
    }
    void dfs(int u,int fa,int dep)
    {
        d[u]=dep;
        f[u][0]=fa;
        int len=G[u].size();
        for(int i=0;i<len;i++)
        {
            if(G[u][i]==fa)continue;
            dfs(G[u][i],u,dep+1);
        }
    }
    void bz()
    {
        for(int j=1;j<=30;j++)
            for(int i=1;i<=n;i++)
                f[i][j]=f[f[i][j-1]][j-1];
    }
    int query(int u,int v)
    {
        if(d[u]<d[v])swap(u,v);
        int dc=d[u]-d[v];
        for(int i=0;i<30;i++)
            if(dc&(1<<i))
                u=f[u][i];
        if(u==v)return u;
        for(int i=30;i>=0;i--)
            if(f[u][i]!=f[v][i])
                u=f[u][i],v=f[v][i];
        return f[u][0];
    }
    int main()
    {
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(int i=0;i<n-1;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs(1,0,0);
        bz();
        for(int i=0;i<q;i++)
        {
            int k,a,b;
            scanf("%d%d%d",&k,&a,&b);
            if(k==0)w[a]=b;
            else
            {
                int lca=query(a,b);
                vector<int>rout;
                while(a!=lca)
                {
                    rout.push_back(w[a]);
                    a=f[a][0];
                }
                while(b!=lca)
                {
                    rout.push_back(w[b]);
                    b=f[b][0];
                }
                rout.push_back(w[lca]);
                if(rout.size()<k)
                {
                    puts("invalid request!");
                    continue;
                }
                sort(rout.begin(),rout.end(),cmp);
                printf("%d
    ",rout.at(k-1));
            }
        }
        return 0;
    }
  • 相关阅读:
    2020软件工程最后一次作业 it
    javaArrayList it
    js二进制数据转图片 it
    002addTwoNumbers it
    1.1选择select it
    js保存图片至本地 it
    npm ERR! code E404 it
    2020软件工程第三次作业 it
    2020软件工程第二次作业 it
    react打包后找不到静态文件 it
  • 原文地址:https://www.cnblogs.com/homura/p/5719734.html
Copyright © 2011-2022 走看看