hdu3087 LCA + 暴力

时间:2023-03-09 06:07:01
hdu3087 LCA + 暴力

Network

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

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!
题意:
n个点的树,2中操作,如果k为0,修改a点的值为b,如果K > 0,求[x,y]的第k大值。
思路:
其实一开始我没有很好的想法。因为如果这是一条链,并且每次操作都是查询[1,n]的第k大,那么这样的写法会超时!
求x,y的lca,然后分别从x,y找到祖先,记录排序,找到第k大值。
/*
* Author: sweat123
* Created Time: 2016/7/13 14:46:25
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int next;
}edge[MAXN*];
int dp[MAXN*][],ind,pre[MAXN],a[MAXN],first[MAXN],rev[MAXN*],tot,dfn[MAXN*],vis[MAXN],fa[MAXN],n,m;
void add(int x,int y){
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
bool cmp(int x,int y){
return x > y;
}
void dfs(int rt,int deq,int pa){
vis[rt] = ;
rev[++tot] = rt;
fa[rt] = pa;
dfn[tot] = deq;
first[rt] = tot;
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
dfs(t,deq+,rt);
rev[++tot] = rt;
dfn[tot] = deq;
}
}
}
int b[MAXN],cnt;
void rmq(){
for(int i = ; i <= tot; i++){
dp[i][] = i;
}
for(int i = ; i < ; i++){
for(int j = ; j + ( << i) - <= tot; j++){
int x = dp[j][i-];
int y = dp[j+(<<(i-))][i-];
if(dfn[x] > dfn[y]){
dp[j][i] = y;
} else{
dp[j][i] = x;
}
}
}
}
int lca(int x,int y){
x = first[x];
y = first[y];
if(x > y)swap(x,y);
int k = (int)(log(y - x + ) * 1.0 / log(2.0));
int l = dp[x][k];
int r = dp[y - (<<k) + ][k];
if(dfn[l] > dfn[r]){
return r;
} else {
return l;
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i = ; i <= n; i++)scanf("%d",&a[i]);
ind = ;
memset(pre,-,sizeof(pre));
for(int i = ; i < n; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
tot = ;
memset(vis,,sizeof(vis));
dfs(,,-);
rmq();
while(m --){
int k,x,y;
scanf("%d%d%d",&k,&x,&y);
if(k == ){
a[x] = y;
} else{
if(y > n){
printf("invalid request!\n");
continue;
}
cnt = ;
int tp = rev[lca(x,y)];
b[cnt++] = a[tp];
while(x != tp){
b[cnt++] = a[x];
x = fa[x];
}
while(y != tp){
b[cnt++] = a[y];
y = fa[y];
}
sort(b,b+cnt,cmp);
if(k > cnt) printf("invalid request!\n");
else printf("%d\n",b[k-]);
}
}
}
return ;
}