| ||||||||||
NetworkTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 415 Accepted Submission(s): 158
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
Sample Output
|
思路:先在线求出两个节点的LCA,并记录下每个节点的父节点,然后询问的时候,沿着u->lca,v->lca走一遍,保存下节点权值,然后排序,找到第k长的权值
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=80010;
int pre[maxn],E[maxn*2],dep[maxn*2],pos[maxn],vis[maxn];
int d[maxn*2][30],val[maxn],num,path[maxn];
int N,Q;
vector<int> g[maxn];
void init()
{
for(int i=0;i<=N;i++)g[i].clear();
num=0;
memset(pos,-1,sizeof(pos));
memset(vis,0,sizeof(vis));
}
void dfs(int u,int depth)
{
E[++num]=u,dep[num]=depth;
if(pos[u]==-1)pos[u]=num;
vis[u]=1;
int len=g[u].size();
for(int i=0;i<len;i++)
{
int v=g[u][i];
if(vis[v])continue;
pre[v]=u;
dfs(v,depth+1);
E[++num]=u,dep[num]=depth;
}
}
void initRMQ(int n)
{
for(int i=0;i<=n;i++)d[i][0]=i;
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)<=n;i++)
{
int x=d[i][j-1],y=d[i+(1<<(j-1))][j-1];
if(dep[x]<dep[y])d[i][j]=x;
else d[i][j]=y;
}
}
int LCA(int u,int v)
{
int x=pos[u],y=pos[v];
if(x>y)swap(x,y);
int k=0;
while((1<<(k+1))<=y-x+1)k++;
int a=d[x][k],b=d[y-(1<<k)+1][k];
if(dep[a]<dep[b])return E[a];
else return E[b];
}
void solve(int k,int u,int v)
{
int fa=LCA(u,v);
int cnt=0;
while(u!=fa){path[cnt++]=val[u];u=pre[u];}
while(v!=fa){path[cnt++]=val[v];v=pre[v];}
path[cnt++]=val[fa];
sort(path,path+cnt,greater<int>());
if(cnt<k)printf("invalid request!\n");
else printf("%d\n",path[k-1]);
}
int main()
{
while(scanf("%d%d",&N,&Q)!=EOF)
{
for(int i=1;i<=N;i++)scanf("%d",&val[i]);
init();
for(int i=1;i<N;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1,1);
initRMQ(num);
while(Q--)
{
int k,a,b;
scanf("%d%d%d",&k,&a,&b);
if(!k)val[a]=b;
else solve(k,a,b);
}
}
return 0;
}