zoukankan      html  css  js  c++  java
  • Network(lca暴力)

    Network

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
    Total Submission(s) : 24   Accepted Submission(s) : 5
    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
    2009 Multi-University Training Contest 17 - Host by NUDT
     
    找lca路中第k大的数,0开头代表改权值
    #include <iostream>
    #include<cstring>
    #include <string>
    #include <algorithm>
    using namespace std;
    int f[80005];
    int vis[80005];
    int head[80005];
    int dep[80005];
    int qv[80005];
    int cnt=0;
    int a[80005];
    struct node
    {
       int v;
       int nxt;
    }e[2*80005];
    
    bool cmp(int x,int y)
    {
        return x>y;
    }
    
    void add(int u,int v)  
    {  
         e[++cnt].nxt=head[u];  
         e[cnt].v = v;  
         head[u]=cnt;  
         return;
     }  
    
    void dfs(int u,int ff)
    {
        f[u]=ff;
        dep[u]=dep[ff]+1;
        for(int i=head[u];i!=-1;i=e[i].nxt)
        {
            if(e[i].v!=ff)//要防止重复搜索,tla了多次
            {
                dfs(e[i].v,u);
            }
        }
    }
    
    int kk;
    void lca(int x,int y)
    {
        //逻辑关系要理清
        kk=1;
       if(dep[x]<dep[y]) 
       {
           swap(x,y);
       }
       while(dep[x]>dep[y])
       {
           a[kk++]=qv[x];
           x=f[x];
       }
       if(x==y) 
       {
           a[kk++]=x;
           return;
       }
       if(f[x]==f[y])
       {
           a[kk++]=qv[x];
           a[kk++]=qv[y];
           a[kk++]=qv[f[x]];
           return;
       }
       while(f[x]!=f[y])//直接暴力
       {
           a[kk++]=qv[x];
           a[kk++]=qv[y];
           x=f[x];
           y=f[y];
       }
       a[kk++]=qv[x];
       a[kk++]=qv[y];
       a[kk++]=qv[f[x]];
       return;
    }
    
    int main()
    {
        int n,m;
        scanf("%d %d",&n,&m);
        memset(vis,0,sizeof(vis));  
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&qv[i]);
        }
        for(int i=1;i<=n-1;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            add(u,v);
            add(v,u);
        }
        f[0]=-1;
        dep[0]=0;
        dfs(1,0);
        for(int i=1;i<=m;i++)
        {
            int u,v,p;
            scanf("%d %d %d",&p,&u,&v);
            if(p!=0)
            {
               
                lca(u,v);
                if(p>kk-1) cout<<"invalid request!"<<endl;
                else 
                {
                    sort(a+1,a+kk,cmp);//要从大到小排
                    cout<<a[p]<<endl;
                }
            }
            else qv[u]=v;
        }
        return 0;
    
    }
     
  • 相关阅读:
    android AndroidManifest.xml package名
    java 空字条串空判断 效率
    ant
    android post 提交数据
    BI 商务智能软件 数据分析
    swfupdate flash上传工具
    log4j 使用方法
    java vc vb 项目结构
    时间单位
    iphone命令行编译xcodebuild
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8627781.html
Copyright © 2011-2022 走看看