HDU 3078 Network LCA水题

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!

给出一棵树,和树上各点的权值

然后有q个询问,每个询问输入k a b

若k==0  则要求把a点的权值改为b

若k>0   则要求输出a,b路径上的点中,权值第k大的点。

若没有的话,输出invalid request!

思路:直接把a,b路径上的点的权值存进一个数组里面,然后对数组大到小排序,输出第k个。

这么暴力竟然可以过,还是140ms。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4
  5 using namespace std;
  6
  7 const int maxn=80000+5;
  8
  9 struct Edge
 10 {
 11     int to,next;
 12 }edge[maxn<<1];
 13
 14 int head[maxn];
 15 int tot;
 16 int w[maxn];
 17 int dep[maxn];
 18 int ans[1000];
 19 int p[maxn][25];
 20
 21 void init()
 22 {
 23     memset(dep,0,sizeof(dep));
 24     dep[1]=1;
 25     memset(p,-1,sizeof(p));
 26     memset(w,-1,sizeof(w));
 27     memset(head,-1,sizeof(head));
 28     tot=1;
 29 }
 30
 31 void addedge(int u,int v)
 32 {
 33     edge[tot].to=v;
 34     edge[tot].next=head[u];
 35     head[u]=tot++;
 36 }
 37
 38 void dfs(int u)
 39 {
 40     for(int i=head[u];~i;i=edge[i].next)
 41     {
 42         int v=edge[i].to;
 43         if(!dep[v])
 44         {
 45             dep[v]=dep[u]+1;
 46             p[v][0]=u;
 47             dfs(v);
 48         }
 49     }
 50 }
 51
 52 void init_lca(int n)
 53 {
 54     for(int j=1;(1<<j)<=n;j++)
 55     {
 56         for(int i=1;i<=n;i++)
 57         {
 58             if(p[i][j-1]!=-1)
 59                 p[i][j]=p[p[i][j-1]][j-1];
 60         }
 61     }
 62 }
 63
 64 bool cmp(int a,int b)
 65 {
 66     return a>b;
 67 }
 68
 69 int solve(int n,int a,int b,int k)
 70 {
 71     if(dep[a]<dep[b])
 72         swap(a,b);
 73
 74     int init_a=a;
 75     int init_b=b;
 76
 77     int cnt;
 78
 79     for(cnt=0;(1<<cnt)<=dep[a];cnt++)
 80         ;
 81     cnt--;
 82
 83     for(int j=cnt;j>=0;j--)
 84     {
 85         if(dep[a]-(1<<j)>=dep[b])
 86             a=p[a][j];
 87     }
 88
 89     int lca;
 90
 91     if(a==b)
 92     {
 93         lca=b;
 94         if(dep[init_a]-dep[lca]+1<k)
 95             return -1;
 96     }
 97
 98     else
 99     {
100         for(int j=cnt;j>=0;j--)
101         {
102             if(p[a][j]!=-1&&p[a][j]!=p[b][j])
103             {
104                 a=p[a][j];
105                 b=p[b][j];
106             }
107         }
108         lca=p[a][0];
109         if(dep[init_a]+dep[init_b]-2*dep[lca]+1<k)
110             return -1;
111     }
112
113     tot=1;
114
115     for(int i=dep[init_a];i>=dep[lca];i--)
116     {
117         ans[tot++]=w[init_a];
118         init_a=p[init_a][0];
119     }
120
121     for(int i=dep[init_b];i>dep[lca];i--)
122     {
123         ans[tot++]=w[init_b];
124         init_b=p[init_b][0];
125     }
126
127     sort(ans+1,ans+tot,cmp);
128
129     return ans[k];
130
131
132 }
133
134 int main()
135 {
136     int n,Q;
137     scanf("%d%d",&n,&Q);
138
139     init();
140
141     for(int i=1;i<=n;i++)
142         scanf("%d",&w[i]);
143
144     for(int i=1;i<n;i++)
145     {
146         int a,b;
147         scanf("%d%d",&a,&b);
148         addedge(a,b);
149         addedge(b,a);
150     }
151
152     dfs(1);
153
154     init_lca(n);
155
156     for(int i=0;i<Q;i++)
157     {
158         int k,a,b;
159         scanf("%d%d%d",&k,&a,&b);
160         if(k==0)
161         {
162             w[a]=b;
163         }
164         else
165         {
166             int ans=solve(n,a,b,k);
167             if(ans==-1)
168                 printf("invalid request!\n");
169             else
170                 printf("%d\n",ans);
171         }
172     }
173     return 0;
174 }

140ms

时间: 2024-08-07 00:17:58

HDU 3078 Network LCA水题的相关文章

hdu 3078 Network lca

题目链接 给一棵树, m个操作, 一共两种操作, 将第x个点的权值改为y, 询问x->y路径上权值第k大的点的权值. 暴力的找..... 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map>

简单的dp hdu 数塔(水题)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21314    Accepted Submission(s): 12808 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少

hdu 2053 Switch Game 水题一枚,鉴定完毕

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10200    Accepted Submission(s): 6175 Problem Description There are many lamps in a line. All of them are off at first. A series of op

HDU 2090 算菜价 --- 水题

/* HDU 2090 算菜价 --- 水题 */ #include <cstdio> int main() { char s[105]; double a, b, sum = 0; while (scanf("%s", s)==1){ scanf("%lf%lf", &a, &b); a *= b; sum += a; } printf("%.1f\n", sum); return 0; }

HDU 2091 空心三角形 --- 水题

/* HDU 2091 空心三角形 --- 水题 */ #include <cstdio> int main() { int kase = 0; char ch; int h, t; //h表示高 while (scanf("%c", &ch) == 1 && ch != '@'){ scanf("%d", &h); if (kase++){ printf("\n"); } getchar(); if

hdu 2212 DFS(水题)

DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4923    Accepted Submission(s): 3029 Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit

hdu 5007(字符串水题)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 327    Accepted Submission(s): 253 Problem Description DT is a big fan of digital

HDOJ/HDU 2560 Buildings(嗯~水题)

Problem Description We divide the HZNU Campus into N*M grids. As you can see from the picture below, the green grids represent the buidings. Given the size of the HZNU Campus, and the color of each grid, you should count how many green grids in the N

HDU 3316 爆搜水题

爆搜水题 模拟扫雷,规则和扫雷一样 给出原图,求在X,Y位置点一下以后的图形,没有弹出的点输出-1,弹出的点输出这个点的数字 从起始点DFS一下即可 #include "stdio.h" #include "string.h" int dir[8][2]={ {-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1} }; int n; int hash[110][110]; char str[110][110]; i