Network
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 692 Accepted Submission(s): 280
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.
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.
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
Recommend
题目大意:一个n个节点,q个操作。后边n-1条边,操作数位0是,把a的点权改成b,否则输出a到b第k大的点权数,k是操作数
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define min(a,b) (a>b?b:a)
using namespace std;
#define N 80080
int fa[N],vis[N],val[N],p[N];
int first[N*2],node[N*2],deep[N*2],minv[N<<1][25];
int n,q,cnt;
int head[N];
struct s
{
int u,v,next;
}edge[N<<1];
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int tot;
void dfs(int u,int pre,int dep)
{
tot++;
fa[u]=pre;
node[tot]=u;
deep[tot]=dep;
vis[u]=1;
first[u]=tot;
int i;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
dfs(v,u,dep+1);
tot++;
node[tot]=u;
deep[tot]=dep;
}
}
}
void init(int n)
{
int i,j,k;
for(i=1;i<=n;i++)
{
minv[i][0]=i;
}
for(j=1;(1<<j)<=n;j++)
{
for(k=1;k+(1<<j)-1<=n;k++)
{
if(deep[minv[k][j-1]]>deep[minv[k+(1<<(j-1))][j-1]])
minv[k][j]=minv[k+(1<<(j-1))][j-1];
else
minv[k][j]=minv[k][j-1];
}
}
}
int q_min(int l,int r)
{
int k=(int)(log((double)(r-l+1))/(log(2.0)));
if(deep[minv[l][k]]>deep[minv[r-(1<<k)+1][k]])
return minv[r-(1<<k)+1][k];
else
return minv[l][k];
}
int lca(int a,int b)
{
int x=first[a];
int y=first[b];
int k;
if(x>y)
{
int t=x;
x=y;
y=t;
}
k=q_min(x,y);
return node[k];
}
int cmp(int a,int b)
{
return a>b;
}
int path(int tot,int u,int pre)
{
while(u!=pre)
{
p[tot++]=val[u];
u=fa[u];
}
p[tot++]=val[u];
return tot;
}
void solve(int k,int u,int v)
{
int lc=lca(u,v);
int tot=0;
tot=path(tot,u,lc);
tot=path(tot,v,lc);
tot--;
if(tot<k)
{
printf("invalid request!\n");
return;
}
sort(p,p+tot,cmp);
printf("%d\n",p[k-1]);
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
int i;
for(i=1;i<=n;i++)
scanf("%d",&val[i]);
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
tot=0;
memset(vis,0,sizeof(vis));
dfs(1,-1,1);
init(tot);
while(q--)
{
int op,a,b;
scanf("%d%d%d",&op,&a,&b);
if(!op)
{
val[a]=b;
}
else
{
solve(op,a,b);
}
}
}
}