HDU 3078 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!

Source

2009 Multi-University Training Contest 17 - Host by NUDT

Recommend

lcy   |   We have carefully selected several similar problems for you:  3071 3070 3072 3073 3074

题目大意:

给出一棵有n个节点的无根树

给出每个节点的权值

有m次询问

每次三个数x,y,z

若x==0 将y位置的数修改为z

若x!=0 询问y节点到z节点的权值总和

这道题数据规模比较小&&HDOJ的评测机速度还算快,求出LCA打暴力其实可以水过

当然如果你不介意的话可以用树链剖分+线段树+平衡树&&二分

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 using namespace std;
  5 const int MAXN=2*1e5+10;
  6 inline int read()
  7 {
  8     char c=getchar();int x=0,f=1;
  9     while(c<'0'||c>'9')    {if(c=='-')    f=-1;c=getchar();}
 10     while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*f;
 11 }
 12 int n,m,S=1; 
 13 int f[MAXN][21],deep[MAXN],g[MAXN];
 14 struct node
 15 {
 16     int u,v,w,nxt;
 17 }edge[MAXN];
 18 int head[MAXN];
 19 int num=1;
 20 int a[MAXN];
 21 inline void add_edge(int x,int y,int z)
 22 {
 23     edge[num].u=x;
 24     edge[num].v=y;
 25     edge[num].w=z;
 26     edge[num].nxt=head[x];
 27     head[x]=num++;
 28 }
 29 void dfs(int now)
 30 {
 31     for(int i=head[now];i!=-1;i=edge[i].nxt)
 32         if(deep[edge[i].v]==0)
 33         {
 34             deep[edge[i].v]=deep[now]+1;
 35             f[edge[i].v][0]=now;
 36             g[edge[i].v]=g[now]+edge[i].w;
 37             dfs(edge[i].v);
 38         }
 39             
 40 }
 41 inline void pre()
 42 {
 43     for(int i=1;i<=19;i++)
 44         for(int j=1;j<=n;j++)
 45             f[j][i]=f[f[j][i-1]][i-1];
 46 }
 47 inline int LCA(int x,int y)
 48 {
 49     if(deep[x]<deep[y])    swap(x,y);
 50     for(int i=19;i>=0;i--)
 51         if(deep[f[x][i]]>=deep[y])
 52             x=f[x][i];
 53     if(x==y)    return x;
 54 
 55     for(int i=19;i>=0;i--)
 56         if(f[x][i]!=f[y][i])
 57             x=f[x][i],y=f[y][i];
 58     return f[x][0];
 59 }
 60 int tot=0;
 61 int ans[MAXN];
 62 int comp(const int &a,const int &b)
 63 {
 64     return a>b;
 65 }
 66 int work(int k,int x,int y)
 67 {
 68     tot=0;
 69     int lca=LCA(x,y);
 70     while(x!=lca)    ans[++tot]=a[x],x=f[x][0];ans[++tot]=a[x];
 71     while(y!=lca)    ans[++tot]=a[y],y=f[y][0];
 72     sort(ans+1,ans+tot+1,comp);
 73     if(k>tot)    printf("invalid request!\n");
 74     else         printf("%d\n",ans[k]);
 75 }
 76 int main()
 77 {
 78     int T=1;
 79     while(T--)
 80     {
 81         n=read();m=read();
 82         for(int i=1;i<=n;i++)    a[i]=read();
 83         memset(head,-1,sizeof(head));num=1;
 84         memset(f,0,sizeof(f));
 85         memset(deep,0,sizeof(deep));
 86         for(int i=1;i<=n-1;i++)
 87         {
 88             int x=read(),y=read();
 89             add_edge(x,y,0);
 90             add_edge(y,x,0);
 91         }
 92         deep[S]=1;
 93         dfs(S);pre();
 94         while(m--)
 95         {
 96             
 97             int x=read(),y=read(),z=read();
 98             if(x==0)    a[y]=z;
 99             else work(x,y,z);
100         }    
101     }
102     
103     return 0;
104 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • cf547D. Mike and Fish(欧拉回路)

    attack
  • 2016计蒜之道复赛 百度地图的实时路况(Floyd 分治)

    首先一个结论:floyd算法的正确性与最外层\(k\)的顺序无关(只要保证是排列即可)

    attack
  • P1983 车站分级

    题目描述 一条单向的铁路线上,依次有编号为 1, 2, …, n 的 n 个火车站。每个火车站都有一个级别,最低为 1 级。现有若干趟车次在这条线路上行驶,每一...

    attack
  • Lake Counting (POJ No.2386)

    题意:有一个M*N的圈子,雨后有积水,然后八个方位相联通的被认为是连接在一起的。请求出圈子里共有多少个水洼。

    用户7727433
  • POJ-1088 滑雪 (记忆化搜索,dp)

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86318 ...

    ShenduCC
  • cf547D. Mike and Fish(欧拉回路)

    attack
  • hdu1078

    @坤的
  • POJ-3866-Exclusive Access 2

    ACM模版 描述 ? ? ? 题解 这绝对是我做过最长的题,也是最难理解的题,翻译成中文都很难理解。 简单的说,就是安排任务使用两个资源的顺序,使最坏情况下,执...

    f_zyj
  • cf1000F. One Occurrence(线段树 set)

    首先把询问离线,预处理每个数的\(pre, nxt\),同时线段树维护\(pre\)(下标是\(pre\),值是\(i\)),同时维护一下最大值

    attack
  • 计算矩阵中全1子矩阵的个数

    最近被我大哥安利了一道算法题, 这道题说难, 还不至于我做不出来, 说简单吧, 我还想不到最优解, 等把最优解告诉我之后, 我还正好能理解. 我甚至曾经怯怯的认...

    烟草的香味

扫码关注云+社区

领取腾讯云代金券