Network
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 613 Accepted Submission(s): 244
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 , 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个点和每个点的权值,又给出N-1条无向边。现在有Q次如k a b形式的查询 ,当k为0时,代表将a点权值改为b;当k非0时,让你求出a、b最短路径上权值第k大的点权。
无语死了,求权值第k的点权,我却求的是权值排在第k位的点权!WA两次才发现。
思路:
一,k为0时,直接修改点权即可。
二,k不为0时,先求出LCA(a,b),因为最短路径一定经过LCA(a,b)。然后遍历最短路径上所有点并记录点权,最后排序输出第k大的点权即可。
说下:怎么遍历最短路径上所有点并记录点权
首先BFS和DFS 不行!至少我是跪了,已经TLE到死了。
可以直接用一个数组在find_depth过程中(详看代码)记录当前搜索点的前驱。从查询的 a 和 b 向前找,直到找到LCA(a,b)为止,在查找过程中用数组记录点权。
AC代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 80000+100
using namespace std;
struct Edge
{
int from, to, next;
};
Edge edge[MAXN<<1];
int head[MAXN], edgenum;
int depth[MAXN<<1], vs[MAXN<<1];
int id[MAXN];
int pre[MAXN];//记录节点前驱
//int node[MAXN];//记录节点深度
int dfs_clock;
int N, Q;//N个点Q次查询
int v[MAXN];//记录每个节点的权值
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v)
{
Edge E = {u, v, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
void getMap()
{
int a, b;
for(int i = 1; i <= N; i++)
scanf("%d", &v[i]);
for(int i = 1; i < N; i++)
{
scanf("%d%d", &a, &b);
addEdge(a, b);
addEdge(b, a);
}
}
void DFS(int u, int fa, int d)
{
//node[u] = d;
id[u] = dfs_clock;
vs[dfs_clock] = u;
depth[dfs_clock++] = d;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
pre[v] = u;//记录前驱
DFS(v, u, d+1);
vs[dfs_clock] = u;
depth[dfs_clock++] = d;
}
}
void find_depth()
{
dfs_clock = 1;
memset(depth, 0, sizeof(depth));
memset(vs, 0, sizeof(vs));
memset(id, 0, sizeof(id));
memset(pre, 0, sizeof(pre));
//memset(node, 0, sizeof(node));
DFS(1, -1, 0);
}
int dp[MAXN<<1][30];
void RMQ_init(int NN)
{
for(int i = 1; i <= NN; i++)
dp[i][0] = i;
for(int j = 1; (1<<j) <= NN; j++)
{
for(int i = 1; i + (1<<j) - 1 <= NN; i++)
{
int a = dp[i][j-1];
int b = dp[i + (1<<(j-1))][j-1];
if(depth[a] < depth[b])
dp[i][j] = a;
else
dp[i][j] = b;
}
}
}
int query(int L, int R)
{
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
int a = dp[L][k];
int b = dp[R - (1<<k) + 1][k];
if(depth[a] < depth[b])
return a;
else
return b;
}
int LCA(int u, int v)
{
int x = id[u];
int y = id[v];
if(x < y)
return vs[query(x, y)];
else
return vs[query(y, x)];
}
int rec[MAXN];//记录路径节点的 权值
int top;
void work(int ex, int s1, int s2)//查找最短路径上的点 并记录点权
{
int next;
next = s1;
top = 0;
while(next != ex)
{
rec[top++] = v[next];
next = pre[next];
}
next = s2;
while(next != ex)
{
rec[top++] = v[next];
next = pre[next];
}
rec[top++] = v[ex];//记录ex节点
}
//void work(int sx, int e1, int e2)//搜索查找TLE到死
//{
// queue<int> Q;
// Q.push(sx);
// top = 1;
// rec[top++] = v[sx];
// while(!Q.empty())
// {
// int u = Q.front();
// Q.pop();
// if(u == e1 || u == e2) continue;
// for(int i = head[u]; i != -1; i = edge[i].next)
// {
// Edge E = edge[i];
// if(node[E.to] < node[u]) continue;//深度须大于当前点的深度
// rec[top++] = v[E.to];
// Q.push(E.to);
// }
// }
//}
void solve()
{
int k, a, b;
while(Q--)
{
scanf("%d%d%d", &k, &a, &b);
if(k == 0)//修改权值
v[a] = b;
else
{
if(a == b)
{
if(k == 1)//注意这里
printf("%d\n", v[a]);
else//k为非1的值 时 不存在
printf("invalid request!\n");
}
else
{
//work(LCA(a, b), a, b);
//int p = LCA(a, b);
work(LCA(a, b), a, b);
// printf("路径%d : " ,top);
// for(int i = 1; i < top; i++)
// printf("%d ", rec[i]);
// printf("\n");
if(top < k)
printf("invalid request!\n");
else
{
sort(rec, rec + top);
printf("%d\n", rec[top - k]);
}
}
}
}
}
int main()
{
scanf("%d%d", &N, &Q);
init();
getMap();
find_depth();
RMQ_init(dfs_clock - 1);
solve();
return 0;
}